javascript
Spring Cloud【Finchley】-09Feign使用Hystrix
文章目录
- 版本说明
- 新建子module
- application.yml中开启Hystrix
- 修改Feign接口
- 测试
- 代码
版本说明
先说下使用的spring cloud和spring boot的版本
Disable HystrixCommands For FeignClients By Default
https://github.com/spring-cloud/spring-cloud-netflix/issues/1277
新建子module
父工程microservice-spring-cloud右键新建Maven Module 命名为:micorservice-consumer-movie-feign-hystrix ,为了简单我们把micorservice-consumer-movie-feign的内容copy到该子模块,修改下application.yml中的spring.application.name即可。
application.yml中开启Hystrix
server:port: 7901spring: application:name: micorservice-consumer-movie-feign-hystrix #eureka eureka: client:service-url:defaultZone: http://artisan:artisan123@localhost:8761/eurekainstance:prefer-ip-address: trueinstance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}# Disable HystrixCommands For FeignClients By Default # https://github.com/spring-cloud/spring-cloud-netflix/issues/1277 feign:hystrix:enabled: true如果是application.property ,请设置 feign.hystrix.enabled=true
修改Feign接口
使用fallback属性指定回退类
回退类 也需要实现上面的接口,同时需要标注@Component让其成为受spring管理的bean
测试
访问http://localhost:8761/ 确认下服务已经注册成功。
访问 http://localhost:7901/movie/1
{"id":1,"username":"artisan1","name":"小工匠一","age":10,"balance":100.00}功能正常,OK。
现在停掉micorservice-provider-user
访问 http://localhost:7901/movie/1 ,进入了回退方法
{"id":1,"username":"默认用户","name":null,"age":null,"balance":null}再次启动 micorservice-provider-user
再次访问 http://localhost:7901/movie/1
{"id":1,"username":"artisan1","name":"小工匠一","age":10,"balance":100.00}功能正常,OK。
代码
https://github.com/yangshangwei/SpringCloudMaster/tree/master/micorservice-consumer-movie-fegin-hystrix
总结
以上是生活随笔为你收集整理的Spring Cloud【Finchley】-09Feign使用Hystrix的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Spring Cloud【Finchle
- 下一篇: Spring Cloud【Finchle