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
- 下一篇: Vue2.x-05 iview的Sele