本章介绍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;
}
}
3、增加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,详情参考 https://blog.csdn.net/u013408188/article/details/100074111
下图是我集成grafana 、prometheus的监控图 </string,></page