在Spring Cloud中構建一個Hystrix Dashboard非常簡單,只需要下面四步:愿意了解源碼的朋友直接求求交流分享技術 一零三八七七四六二六
創建一個標準的Spring Boot工程,命名為:hystrix-dashboard。 編輯pom.xml,具體依賴內容如下:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.SR1</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
復制代碼
為應用主類加上@EnableHystrixDashboard,啟用Hystrix Dashboard功能。
@EnableHystrixDashboard
@SpringCloudApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
復制代碼
根據實際情況修改application.properties配置文件,比如:選擇一個未被占用的端口等,此步非必須。
spring.application.name=hystrix-dashboard
server.port=1301
復制代碼
既然Hystrix Dashboard監控單實例節點需要通過訪問實例的/hystrix.stream接口來實現,自然我們需要為服務實例添加這個端點,而添加該功能的步驟也同樣簡單,只需要下面兩步:
在服務實例pom.xml中的dependencies節點中新增spring-boot-starter-actuator監控模塊以開啟監控相關的端點,并確保已經引入斷路器的依賴spring-cloud-starter-hystrix:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
復制代碼
確保在服務實例的主類中已經使用@EnableCircuitBreaker或@EnableHystrix注解,開啟了斷路器功能。完整項目源碼