springcloud feign集成hystrix方式
作者:lipengxs
这篇文章主要介绍了springcloud feign集成hystrix方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
本章介绍feign集成hystrix
1、增加pom依赖`
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
2、启动类中增加注解
@EnableHystrix
3、增加feign接口fallback以及相关配置
DemoService
@ConditionalOnProperty(name = "app.host.abcurl") @FeignClient(value = "demo-service", url = "${app.host.abcurl}" ,fallback = DemoServiceFallbackImpl .class) public interface DemoService { @GetMapping("/v1/api/getCateData") ApiResponse<Page<Object>> getCateData(@RequestParam Map<String,String> params); @GetMapping("/v1/api/getProductData") ApiResponse<Page<Detail>> getProductData(@RequestParam Map<String,String> params); }
DemoServiceFallbackImpl
@Slf4j @Component public class DemoServiceFallbackImpl implements DemoService { @Override public ApiResponse<Page<Object>> getCateData (Map<String, String> params) { log.warn("DemoServiceFallbackImpl getCateData fail"); return null; } @Override public ApiResponse<Page<Detail>> getProductData(Map<String, String> params) { log.warn("DemoServiceFallbackImpl getProductData fail"); return null; } }
4、增加yml相关配置
feign: httpclient: enabled: true hystrix: enabled: true hystrix: command: default: execution: timeout: enabled: true isolation: thread: timeoutInMilliseconds: 1000 //该配置需要比ribbon超时时间长 circuitBreaker: requestVolumeThreshold: 1000 threadpool: default: coreSize: 60 maxQueueSize: 200 queueSizeRejectionThreshold: 200 ribbon: ReadTimeout: 500 ConnectTimeout: 500 ExecTimeout: 500 MaxAutoRetries: 1 //最好设置超时重试
这里如果需要查看hystrix监控,可以集成Hystrix Dashboard,详情参考
下图是我集成grafana 、prometheus的监控图
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。