用Hystrix Dashboard和Turbine實現(xiàn)數(shù)據(jù)的可視化監(jiān)控

Hystrix提供了實時監(jiān)控箩溃,可以記錄執(zhí)行信息,QPS拄丰,請求中成功和失敗的數(shù)量等蝇棉。

Hystrix Dashboard的可視化監(jiān)控

單個應用的熔斷監(jiān)控

1. 添加依賴和配置

添加Eureka、Hystrix澜倦、Hystrix-dashboard聚蝶、Actuator、Feign的依賴

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</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-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2. 添加配置

spring.application.name=spring-cloud-consumer-hystrix
server.port=9001
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3. 添加配置啟動類和Servlet

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SpringCloudConsumerHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConsumerHystrixApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

注意: springboot2.0需要配置Servlet才會顯示監(jiān)控數(shù)據(jù)藻治。

4. 查看監(jiān)控數(shù)據(jù)

Hystrix Dashboard + Turbine的可視化監(jiān)控

在分布式系統(tǒng)中验靡,相同服務實例經(jīng)常需要部署上百甚至上千個倍宾,很多時候需要把服務實例的狀態(tài)以集群方式展現(xiàn)出來,這樣就可以更好的查看系統(tǒng)狀態(tài)胜嗓。凿宾,為此Netflix提供了一個開源項目(Turbine)來提供把多個hystrix.stream的內(nèi)容聚合為一個數(shù)據(jù)源供Dashboard展示。

1. 添加依賴并啟動支持

需要添加Turbine兼蕊、Hystrix-dashboard初厚、Actuator、Hystrix依賴

 <!--Turbine-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</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-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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列表孙技,表明監(jiān)控哪些服務
  • turbine.aggregator.clusterConfig :指定聚合哪些集群产禾,多個使用”,”分割,默認為default牵啦⊙乔椋可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問
  • turbine.clusterNameExpression : 1. clusterNameExpression指定集群名稱,默認表達式appName哈雏;此時:turbine.aggregator.clusterConfig需要配置想要監(jiān)控的應用名稱楞件;2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫裳瘪,因為默認就是default土浸;3. 當clusterNameExpression: metadata[‘cluster’]時,假設想要監(jiān)控的應用配置了eureka.instance.metadata-map.cluster: ABC彭羹,則需要配置黄伊,

3. 配置啟動支持

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixDashboardTurbineApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardTurbineApplication.class, args);
    }
}

4. 創(chuàng)建多個服務消費者

分別創(chuàng)建子項目spring-cloud-consumer-node1,spring-cloud-consumer-node2
node01添加配置

spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

node02添加配置

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

分別配置feign
node01

@FeignClient(name = "spring-cloud-provider", fallback = HelloHystrix.class)
public interface MyFeignClient {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}

node02

@FeignClient(name = "spring-cloud-provider2", fallback = HelloHystrix.class)
public interface MyFeignClient {
    @RequestMapping(value = "/hello")
    public String hello2(@RequestParam(value = "name") String name);
}

5. 測試

依次啟動spring-cloud-eureka、spring-cloud-consumer-node1派殷、spring-cloud-consumer-node1还最、hystrix-dashboard-turbine(Turbine)
(1)訪問 http://localhost:8001/turbine.stream
返回:

ping
data: {"reportingHostsLast10Seconds":1,"name":"meta","type":"meta","timestamp":1494921985839}

進行圖形化監(jiān)控查看,輸入:http://localhost:8001/hystrix毡惜,返回酷酷的小熊界面拓轻,輸入: http://localhost:8001/turbine.stream,然后點擊 Monitor Stream ,可以看到出現(xiàn)了倆個監(jiān)控列表

參考:
http://www.ityouknow.com/springcloud/2017/05/18/hystrix-dashboard-turbine.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末经伙,一起剝皮案震驚了整個濱河市扶叉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌橱乱,老刑警劉巖辜梳,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異泳叠,居然都是意外死亡作瞄,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門危纫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宗挥,“玉大人乌庶,你說我怎么就攤上這事∑豕ⅲ” “怎么了瞒大?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長搪桂。 經(jīng)常有香客問我透敌,道長,這世上最難降的妖魔是什么踢械? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任酗电,我火速辦了婚禮,結(jié)果婚禮上内列,老公的妹妹穿的比我還像新娘撵术。我一直安慰自己,他們只是感情好话瞧,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布嫩与。 她就那樣靜靜地躺著,像睡著了一般交排。 火紅的嫁衣襯著肌膚如雪划滋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天个粱,我揣著相機與錄音古毛,去河邊找鬼翻翩。 笑死都许,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的嫂冻。 我是一名探鬼主播胶征,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼桨仿!你這毒婦竟也來了睛低?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤服傍,失蹤者是張志新(化名)和其女友劉穎钱雷,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吹零,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡罩抗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了灿椅。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片套蒂。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡钞支,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出操刀,到底是詐尸還是另有隱情烁挟,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布骨坑,位于F島的核電站撼嗓,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏欢唾。R本人自食惡果不足惜静稻,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望匈辱。 院中可真熱鬧,春花似錦亡脸、人聲如沸押搪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽大州。三九已至,卻和暖如春垂谢,著一層夾襖步出監(jiān)牢的瞬間厦画,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工滥朱, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留根暑,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓徙邻,卻偏偏與公主長得像排嫌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子缰犁,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354