javascript
Spring Cloud【Finchley】-09Feign使用Hystrix
文章目錄
- 版本說明
- 新建子module
- application.yml中開啟Hystrix
- 修改Feign接口
- 測(cè)試
- 代碼
版本說明
先說下使用的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的內(nèi)容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 ,請(qǐng)?jiān)O(shè)置 feign.hystrix.enabled=true
修改Feign接口
使用fallback屬性指定回退類
回退類 也需要實(shí)現(xiàn)上面的接口,同時(shí)需要標(biāo)注@Component讓其成為受spring管理的bean
測(cè)試
訪問http://localhost:8761/ 確認(rèn)下服務(wù)已經(jīng)注冊(cè)成功。
訪問 http://localhost:7901/movie/1
{"id":1,"username":"artisan1","name":"小工匠一","age":10,"balance":100.00}功能正常,OK。
現(xiàn)在停掉micorservice-provider-user
訪問 http://localhost:7901/movie/1 ,進(jìn)入了回退方法
{"id":1,"username":"默認(rèn)用戶","name":null,"age":null,"balance":null}再次啟動(dòng) 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
總結(jié)
以上是生活随笔為你收集整理的Spring Cloud【Finchley】-09Feign使用Hystrix的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud【Finchle
- 下一篇: Spring Cloud【Finchle