springcloud feign集成hystrix方式
更新时间:2023年08月28日 10:17:41 作者: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的监控图


总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
springboot+mybatis快速插入大量数据的具体实现
最近导入表格数据时需要同时插入修改大量数据,下面这篇文章主要给大家介绍了关于springboot+mybatis快速插入大量数据的具体实现,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2023-04-04
详解Java redis中缓存穿透 缓存击穿 雪崩三种现象以及解决方法
缓存穿透是指缓存和数据库中都没有的数据,而用户不断发起请求,如发起为id为“-1”的数据或id为特别大不存在的数据。这时的用户很可能是攻击者,攻击会导致数据库压力过大2022-01-01


最新评论