試水Spring Cloud Hystrix
Spring Cloud Hystrix是一個容錯庫,它實現了斷路器模式,使得當服務發生異常時,會自動切斷連線,並將請求引導至預設的回撥方法。
服務端
在Spring Tool Suite的檔案選單中,點選新建Spring Starter Project。建立一個普通的Restful風格的服務。

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class SpringcloudHystrixServerApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudHystrixServerApplication.class, args); } @RequestMapping(value = "/message") public String getMessage() { return "Hello World!"; } }
application.properties檔案中配置服務的埠, server.port=8200
。
服務啟動後,可以在瀏覽器檢視相應介面。

客戶端
再建立一個客戶端應用程式,在建立時選擇Hystrix,Hystrix Dashboard,Actuator和Web模組。

專案建立完成後,新增一個Service,其中包括呼叫服務端介面的方法及一個回撥方法。注意這裡@HystrixCommand的用法。
import java.net.URI; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service public class MessageService { private final RestTemplate restTemplate; public MessageService(RestTemplate rest) { this.restTemplate = rest; } @HystrixCommand(fallbackMethod = "reliable") public String getMessage() { URI uri = URI.create("http://localhost:8200/message"); return this.restTemplate.getForObject(uri, String.class); } public String reliable() { return "Woo, something wrong!"; } }
在客戶端的入口方法加上@EnableCircuitBreaker標記,並把它的埠設為 server.port=8300
。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @EnableHystrixDashboard @EnableCircuitBreaker @RestController @SpringBootApplication public class SpringcloudHystrixClientApplication { @Autowired private MessageService messageService; @Bean public RestTemplate rest(RestTemplateBuilder builder) { return builder.build(); } public static void main(String[] args) { SpringApplication.run(SpringcloudHystrixClientApplication.class, args); } @RequestMapping("/message") public String getMessge() { return messageService.getMessage(); } }
啟動客戶端後,如果在瀏覽器裡看到頁面能正常獲取服務端的資料,說明當前客戶端與服務端運作都是正常的。

然後,停止服務端,讓情況出現異常。
重新整理頁面,可以看到這次的結果也在預料之內,當客戶端呼叫服務端失敗後,通過Hystrix的作用,自動切換至呼叫預設的回撥方法。
儀表盤
Hystrix自帶視覺化儀表盤,在上面的客戶端程式碼中,入口方法除了增加了@EnableCircuitBreaker標記外,還有@EnableHystrixDashboard。這樣的設定便可以啟用Hystrix的儀表盤。
不過在application.properties檔案還需要加上以下配置,以避免“Unable to connect to Command Metric Stream”錯誤。
management.endpoints.web.exposure.include=hystrix.stream management.endpoints.web.base-path=/
當客戶端被啟動後,使用 http://localhost:8300/hystrix
路徑可以直接訪問儀表盤。
之後在Hystrix Dashboard下面的位址列內填上 http://localhost:8300/hystrix.stream
,再點選Monitor Stream按鈕,監控結果一覽無遺。