【SpringCloud】(十四):Feign對Hystrix的支援 fallbackFactory
前面的文章中提到了fallback,這裡我們描述一個它的增強版。fallbackFactory。都是實現斷路器的功能。
UserFeignClient
package com.dynamic.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.dynamic.cloud.entity.User;
//@FeignClient(name="microservice-provider-user",fallback =HystrixClientFallback.class,fallbackFactory=HystrixClientFallbackFactory.class)
@FeignClient(name="microservice-provider-user",fallbackFactory=HystrixClientFallbackFactory.class)
public interface UserFeignClient {
@RequestMapping(value="/simple/{id}",method = RequestMethod.GET) //只能是RequestMapping
public User findById(@PathVariable("id") Long id); //@PathVariable需要設定括號中的名稱
}
加上fallbackFactory
對應的類是HystrixClientFallbackFactory
package com.dynamic.cloud.feign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.dynamic.cloud.entity.User;
import feign.hystrix.FallbackFactory;
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<UserFeignClient> {
private static final Logger logger = LoggerFactory.getLogger(HystrixClientFallbackFactory.class);
@Override
public UserFeignClient create(Throwable cause) {
HystrixClientFallbackFactory.logger.info("fallback reason was: {} " ,cause.getMessage());
return new UserFeignClientWithFactory()
{
@Override
public User findById(Long id) {
User user = new User();
user.setId(-1L);
return user;
}
};
}
}
該類中存在UserFeignClientWithFactory,它繼承了UserFeignClient。這裡實現了它的方法。
package com.dynamic.cloud.feign;
public interface UserFeignClientWithFactory extends UserFeignClient {
}
我們把使用者微服務停掉之後,斷路器開啟,執行fallbackfactory 指定的類中方法。HystrixClientFallbackFactory 。
相關文章
- 【SpringCloud】(十三):全域性和單個禁用Feign Client對Hystrix的支援SpringGCCloudclient
- 【SpringCloud】(十二):Fegion的Hystrix支援SpringGCCloud
- SpringCloud微服務(基於Eureka+Feign+Hystrix+Zuul)SpringGCCloud微服務Zuul
- JAVA 多使用者商城系統b2b2c-Feign對Hystrix的支援Java
- SpringCloud之HystrixSpringGCCloud
- SpringCloud Feign的分析SpringGCCloud
- Springcloud(二) feignSpringGCCloud
- SpringCloud-FeignSpringGCCloud
- SpringCloud使用Sentinel 代替 HystrixSpringGCCloud
- SpringCloud(第 017 篇)電影微服務接入Feign,新增 fallbackFactory 屬性來觸發請求進行容災降級...SpringGCCloud微服務
- 【SpringCloud】(九):Feign的自定義配置SpringGCCloud
- springcloud之Hystrix熔斷器SpringGCCloud
- SpringCloud(三)Hystrix斷路器SpringGCCloud
- springcloud(四):熔斷器HystrixSpringGCCloud
- SpringCloud——Feign例項及原理SpringGCCloud
- SpringCloud+Hystrix服務容錯SpringGCCloud
- SpringCloud學習筆記(3)——HystrixSpringGCCloud筆記
- SpringCloud分散式微服務b2b2c電子商務docker-feign-hystrix-ribbon(八)SpringGCCloud分散式微服務Docker
- 分享一個 SpringCloud Feign 中所埋藏的坑SpringGCCloud
- 【SpringCloud】(八):認識Feign及使用SpringGCCloud
- springcloud(五):熔斷監控Hystrix DashboardSpringGCCloud
- SpringCloud分散式微服務b2b2c電子商務分散式微服務-docker-feign-hystrix(七)SpringGCCloud分散式微服務Docker
- SpringCloud微服務治理二(Robbin,Hystix,Feign)SpringGCCloud微服務
- SpringCloud-使用Feign呼叫服務介面SpringGCCloud
- SpringCloud-Hystrix 服務降級、熔斷SpringGCCloud
- 《SpringCloud專題17》-Hystrix熔斷器案例SpringGCCloud
- springcloud之hystrix原理和實踐總結SpringGCCloud
- sb2.0新版springcloud微服務實戰:Consul+Zuul+Feign/Ribbon+Hystrix Turbine+Config+sleuth+zipkinSpringGCCloud微服務Zuul
- 每天學點SpringCloud(十四):Zipkin使用SpringCloud Stream以及ElasticsearchSpringGCCloudElasticsearch
- springcloud中feign檔案上傳、下載SpringGCCloud
- (22)SpringCloud-使用Feign呼叫服務介面SpringGCCloud
- SpringCloud 2020.0.4 系列之 FeignSpringGCCloud
- springcloud學習筆記(四)Spring Cloud HystrixSpringGCCloud筆記
- SpringCloud之斷路器聚合監控(Hystrix Turbine)SpringGCCloud
- SpringCloud原始碼學習之Hystrix熔斷器SpringGCCloud原始碼
- springCloud入門學習--Hystrix狀態監控SpringGCCloud
- SpringCloud 2020.0.4 系列之Hystrix看板SpringGCCloud
- SpringCloud學習筆記:熔斷器Hystrix(5)SpringGCCloud筆記