Spring Cloud-09Feign使用Hystrix
阿新 • • 發佈:2018-12-17
文章目錄
版本說明
先說下使用的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: 7901
spring:
application:
name: micorservice-consumer-movie-feign-hystrix
#eureka
eureka:
client:
service-url:
defaultZone: http://artisan:artisan123@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-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
測試
- 啟動microservice-discovery-eureka,註冊中心
- 啟動micorservice-provider-user,服務提供者
- 啟動micorservice-consumer-movie-feign-hystrix,服務消費者開啟了Hystrix
訪問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。