java B2B2C springmvc mybatis電子商務平臺原始碼--熔斷監控Turbine

yay10121發表於2018-12-06

在複雜的分散式系統中,相同服務的節點經常需要部署上百甚至上千個,很多時候,運維人員希望能夠把相同服務的節點狀態以一個整體叢集的形式展現出來,這樣可以更好的把握整個系統的狀態。 為此,Netflix提供了一個開源專案(Turbine)來提供把多個hystrix.stream的內容聚合為一個資料來源供Dashboard展示。願意瞭解原始碼的朋友直接求求交流分享技術:二一四七七七五六三三

1、新增依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-turbine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-netflix-turbine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>
</dependencies>
複製程式碼

2、配置檔案

spring.application.name=hystrix-dashboard-turbine
server.port=8001
turbine.appConfig=node01,node02
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
複製程式碼

turbine.appConfig :配置Eureka中的serviceId列表,表明監控哪些服務 turbine.aggregator.clusterConfig :指定聚合哪些叢集,多個使用”,”分割,預設為default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問 turbine.clusterNameExpression : 1. clusterNameExpression指定叢集名稱,預設表示式appName;此時:turbine.aggregator.clusterConfig需要配置想要監控的應用名稱;2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫,因為預設就是default;3. 當clusterNameExpression: metadata[‘cluster’]時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC 3、啟動類 啟動類新增@EnableTurbine,啟用對Turbine的支援

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class DashboardApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DashboardApplication.class, args);
    }
 
}
複製程式碼

到此Turbine(hystrix-dashboard-turbine)配置完成

4、測試 在示例專案spring-cloud-consumer-hystrix基礎上修改為兩個服務的呼叫者spring-cloud-consumer-node1和spring-cloud-consumer-node2

spring-cloud-consumer-node1專案改動如下: application.properties檔案內容

spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true
 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
spring-cloud-consumer-node2專案改動如下: application.properties檔案內容

spring.application.name=node02
server.port=9002
feign.hystrix.enabled=true
 
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
 HelloRemote類修改:

@FeignClient(name= "spring-cloud-producer2", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
 
    @RequestMapping(value = "/hello")
    public String hello2(@RequestParam(value = "name") String name);
 
}
複製程式碼

對應的HelloRemoteHystrix和ConsumerController類跟隨修改,具體檢視程式碼

修改完畢後,依次啟動spring-cloud-eureka、spring-cloud-consumer-node1、spring-cloud-consumer-node1、hystrix-dashboard-turbine(Turbine)

開啟eureka後臺可以看到註冊了三個服務:

java B2B2C springmvc mybatis電子商務平臺原始碼--熔斷監控Turbine

訪問 http:/ /localhost:8001/turbine.stream

返回:

: ping data: {"reportingHostsLast10Seconds":1,"name":"meta","type":"meta","timestamp":1494921985839} 並且會不斷重新整理以獲取實時的監控資料,說明和單個的監控類似,返回監控專案的資訊。進行圖形化監控檢視,輸入:http:/ /localhost:8001/hystrix,返回酷酷的小熊介面,輸入: http:/ /localhost:8001/turbine.stream,然後點選 Monitor Stream ,可以看到出現了倆個監控列表

java B2B2C springmvc mybatis電子商務平臺原始碼--熔斷監控Turbine

整體程式碼結構如下:資料和原始碼來源

java B2B2C springmvc mybatis電子商務平臺原始碼--熔斷監控Turbine

相關文章