日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何禁用单个FegionClient的Hystrix的支持

發布時間:2024/4/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何禁用单个FegionClient的Hystrix的支持 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Feign對Hystrix的支持,我們知道Feign Client注解它是支持fallback屬性,這個我們詳細探討一下Feign Hystrix Supporthttps://cloud.spring.io/spring-cloud-openfeign/reference/html/如果Hystrix在classpath下面,默認情況下所有的方法都會被斷路器包裝,你返回com.netflix.hystrix.HystrixCommand這個類也是可以的,這個類我們可能很陌生https://github.com/Netflix/Hystrix/wiki/How-To-Usepublic class CommandHelloFailure extends HystrixCommand<String> {private final String name;public CommandHelloFailure(String name) {super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));this.name = name;}@Overrideprotected String run() {throw new RuntimeException("this command always fails");}@Overrideprotected String getFallback() {return "Hello Failure " + name + "!";} }響應式的模式,rx.java,rx.android,異步的調用使用queue,響應式編程的東西,如果你想讓Feign不支持,If Hystrix is on the classpath and feign.hystrix.enabled=true, Feign will wrap all methods with a circuit breaker. Returning a com.netflix.hystrix.HystrixCommand is also available. This lets you use reactive patterns (with a call to .toObservable() or .observe() or asynchronous use (with a call to .queue()).如果你想單獨的禁用一個Feign Client的話,你可以這么玩@Configuration public class FooConfiguration {@Bean@Scope("prototype")public Feign.Builder feignBuilder() {return Feign.builder();} }Hystrix它支持一種回退的概念,他說當斷路器打開,或者發現錯誤的時候,他就會執行一個默認的代碼路徑,我們之前Ribbon的做法一樣的嗎,無論是請求不成功或者斷路器打開的時候,就會走fallback方法,你可以使用@FeignClient注解,并為他設置fallback的屬性,然后這個類實現FeinClient接口Hystrix supports the notion of a fallback: a default code path that is executed when they circuit is open or there is an error. To enable fallbacks for a given @FeignClient set the fallback attribute to the class name that implements the fallback. You also need to declare your implementation as a Spring bean.就是這個樣子的@FeignClient(name = "hello", fallback = HystrixClientFallback.class) protected interface HystrixClient {@RequestMapping(method = RequestMethod.GET, value = "/hello")Hello iFailSometimes(); }static class HystrixClientFallback implements HystrixClient {@Overridepublic Hello iFailSometimes() {return new Hello("fallback");} }使用fallback屬性,to the class name,他要實現HystrixClient接口microservice-consumer-movie-feign-hystrixlocalhost:8010/movie/1Feign使用Hystrix無效原因及解決方法https://www.cnblogs.com/devzxd/p/feign-hystrix-problem.html<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Brixton.RELEASE</version><type>pom</type><scope>import</scope> </dependency>為什么要默認關閉hystrix呢?請看這里:https://github.com/spring-cloud/spring-cloud-netflix/issues/1277解決方案 如果是yml文件,請在文件中加入:feign:hystrix:enabled: true 如果是properties文件,請在文件中加入:feign.hystrix.enabled=truelocalhost:8010/healthlocalhost:8010/hystrix.streamhttps://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-hystrixFeign Hystrix SupportIf Hystrix is on the classpath and feign.hystrix.enabled=true, Feign will wrap all methods with a circuit breaker. @Configuration public class FooConfiguration {@Bean@Scope("prototype")public Feign.Builder feignBuilder() {return Feign.builder();} } <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version><packaging>pom</packaging><name>microcloud02</name><url>http://maven.apache.org</url><properties><jdk.version>1.8</jdk.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency> <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency><dependency> <!-- SpringCloud離不開SpringBoot,所以必須要配置此依賴包 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.12.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> --></project> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>microservice-consumer-movie-feign-hystrix</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>microservice-simple-consumer-movie</name><description>Demo project for Spring Boot</description><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project> #debug=true server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-consumer-movie-feign-hystrix eureka.instance.prefer-ip-address=true eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} eureka.client.healthcheck.enabled=true spring.redis.host=10.40.8.152 spring.redis.password=1234 spring.redis.port=6379#stores.ribbon.listOfServers=10.40.8.144:7900feign.hystrix.enabled=true package com.learn.cloud.feign;import org.springframework.stereotype.Component;import com.learn.cloud.entity.User;@Component public class HystrixClientFallback implements UserFeignClient {@Overridepublic User findById(Long id) {User user = new User();user.setId(0L);return user;}} package com.learn.cloud.feign;import org.springframework.stereotype.Component;import com.learn.cloud.entity.User;import feign.hystrix.FallbackFactory;@Component public class FeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {@Overridepublic UserFeignClient create(Throwable throwable) {return new UserFeignClient() {@Overridepublic User findById(Long id) {System.out.println("fallback; reason was :"+ throwable);User user = new User();user.setId(-1L);user.setName("默認用戶");return user;}};} } package com.learn.cloud.feign;import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;import com.learn.cloud.entity.User;//@FeignClient(name = "microservice-simple-provider-user", fallback = HystrixClientFallback.class) @FeignClient(name = "microservice-simple-provider-user", fallbackFactory = FeignClientFallbackFactory.class) public interface UserFeignClient {@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)public User findById(@PathVariable("id") Long id); } package com.learn.cloud.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;import com.learn.cloud.entity.User; import com.learn.cloud.feign.UserFeignClient;@RestController public class MovieController {@Autowiredprivate UserFeignClient userFeignClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {return this.userFeignClient.findById(id);}} package com.learn.cloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class ConsumerMovieFeignHystrixApplication {public static void main(String[] args) {SpringApplication.run(ConsumerMovieFeignHystrixApplication.class, args);} }

?

總結

以上是生活随笔為你收集整理的如何禁用单个FegionClient的Hystrix的支持的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。