Spring Cloud Hystrix是一個容錯庫,它實現了斷路器模式,使得當服務發生異常時,會自動切斷連接,並將請求引導至預設的回調方法。 服務端 在Spring Tool Suite的文件菜單中,點擊新建Spring Starter Project。建立一個普通的Restful風格的服務。 a ...
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按鈕,監控結果一覽無遺。