微服務springcloud—為Feign禁用Hystrix、Hystrix的監控、Feign專案的Hystrix監控
為Feign禁用Hystrix
在Spring Cloud中,只要Hystrix在專案的classpath中,Feign就會使用斷路器包裹Feign客戶端的所有方法。這樣雖然方便,但是很多場景下不需要該功能。
如何為Feign客戶端禁用Hystrix呢?
為指定Feign客戶端禁用Hystrix
藉助Feign的自定義配置,可輕鬆為指定名稱的Feign客戶端禁用Hystrix。
全域性禁用Hystrix
只需要在application.yml中配置feign.hystrix.enabled = false即可。
Hystrix的監控
Feign專案的Hystrix監控
1.啟動microservice-discovery-eureka。
2.啟動microservice-provider-user。
3.啟動microservice-consumer-movie-ribbon-hystrix。
4.訪問http://localhost:8010/actuator/hystrix.stream,可以看到瀏覽器一直處於請求的狀態,頁面空白。這是因為此時專案中註解了@HystrixCommand的方法還沒有被執行,因此也沒有任何的監控資料。
5.訪問http://localhost:8010/user/1後,再次訪問http://loclhost:8010/actuator/hystrix.stream可以看到頁面會重複出現類似於以下的內容。
Feign專案的Hystrix監控
啟動之前的microservice-consumer-movie-feign-hystrix-fallback專案,並使用類似的方式測試,然後訪問http://loclhost:8010/actuator/hystrix.stream發現返回404,這是為什麼呢?
檢視專案的依賴樹就會發現,專案甚至連Hystrix-metrics-event-stream的依賴都沒有,那麼如何解決問題呢?
1.複製專案microservice-consumer-movie-feign-hystrix-fallback,改為microservice-consumer-movie-feign-hystrix-fallback-stream。
2.為專案新增spring-cloud-starter-netflix-hystrix的依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
3.在啟動類上新增@EnableCircuitBreaker,這樣使用/actuator/hystrix.stream端點監控Hystrix了。
本文大部分內容轉載自周立的《Spring Cloud與Docker微服務架構實戰》