springcloud-feign的hystrix支持
关于hystrix的介绍,可以看我的上篇博客:springcloud-断路器hystrixs
本文主要介绍在feign中,如何使用hystrix
1、pom依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <!-- hystrix 断路器 --> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>com.xwj</groupId> <artifactId>spring-cloud-core</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
2、入口开启feign和hystrix
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients // 开启feign @EnableCircuitBreaker // 开启断路器 public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
3、在yml文件打开feign对hystrix的支持(关于hystrix的配置,本篇不做介绍)
feign: hystrix: enabled: true #打开feign对hystrix的支持
4、修改FeignClient,增加 fallback 参数,这是接口的降级回调类
@FeignClient(name = "service-provider", fallback = UserFallback.class) public interface UserFeignClient { @GetMapping("/find/{id}") UserEntity findById(@PathVariable("id") Long id); // PathVariable必须得设置value }
5、创建降级回调类UserFallback
/** * 错误回调类 */ @Component public class UserFallback implements UserFeignClient { @Override public UserEntity findById(Long id) { UserEntity user = new UserEntity(); user.setId("1000"); user.setAge(12); return user; } }
知识改变世界
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
· 如果单表数据量大,只能考虑分库分表吗?
· 7 个最近很火的开源项目「GitHub 热点速览」
· DeepSeekV3:写代码很强了
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· Visual Studio 2022 v17.13新版发布:强化稳定性和安全,助力 .NET 开发提
· AI浏览器自动化实战