1. 程式人生 > >Spring Cloud實戰之初級入門(四)— 利用Hystrix實現服務熔斷與服務監控

Spring Cloud實戰之初級入門(四)— 利用Hystrix實現服務熔斷與服務監控

close 哪些 cati status netflix turn 遠程倉庫 monitor mri

目錄

  • 1.環境介紹
  • 2.服務監控
    • 2.1 加入依賴
    • 2.2 修改配置文件
    • 2.3 修改啟動文件
    • 2.4 監控服務
    • 2.5 小結
  • 3. 利用hystrix實現消費服務熔斷
    • 3.1 加入服務熔斷
    • 3.2 測試服務熔斷
  • 4. 利用turbine監控所有應用
    • 4.1 創建工程
    • 4.2 修改配置文件
    • 4.3 修改啟動文件
    • 4.4 啟動
  • 5.一點點重要的事情

1.環境介紹

本篇文章涉及到前面文章的工程,mirco-service-providermirco-service-consumer以及需要另外新建一個工程mirco-service-turbine-hystrix-dashbord

2.服務監控

2.1 加入依賴

為“mirco-service-provider”工程的pom文件中加入

        <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>

2.2 修改配置文件

這裏有點東西需要跟小夥伴們說明,因為上一篇文章SpringCloud實戰之初級入門(三)— spring cloud config搭建git配置中心中,我們將服務提供的工程配置文件上傳到了github上,那麽我們需要修改github上provider.yml,這是第一種方法。第二種方法是直接在bootstrap.yml中加入我們想要的配置,也是一樣的生效,如果不明白我這裏寫了什麽,可以看我的視頻。我們這裏介紹第一種方法:

  1. 在工程目錄打開命令行
    技術分享圖片
  2. 使用vi provider.yml編輯配置文件
    技術分享圖片
  3. 保存配置文件
  4. 使用"git status"查看我們工程中哪些文件進行了修改
  5. 使用"git add provider-test.yml"加入本地倉庫的提交列表
  6. 使用"git commit -m ‘提交說明‘"將要提交的文件提交到本地倉庫
  7. 使用"git push origin master"已經提交到本地倉庫的文件上傳至遠程倉庫。

2.3 修改啟動文件

  1. 在啟動文件中加上"@EnableHystrixDashboard 、@EnableCircuitBreaker"註解,然後啟動工程。

  2. 打開瀏覽器訪問http://localhost:7001/hystrix,可以看到如下圖內容,說明服務調用的監控就搭建完成了。
    技術分享圖片

2.4 監控服務

我在網絡上看了一下,很多教程讓你在輸入框中輸入http://localhost:8001/hystrix.stream然後點擊下面的"monitor stream" 按鈕基本上就完了,但是在我的環境裏面是不行的,可能是spring cloud和spring boot的版本不一樣。下面用步驟講一下怎麽做監控:

  • 在瀏覽器的地址欄中輸入http://localhost:8001/hystrix.stream是無法訪問的,因為我們沒有將“/hystrix.stream”這目錄加入工程的訪問目錄。

  • 修改啟動文件如下:

@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class MircoServiceProviderApplication {

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

    @Bean
    public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
        HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet() ;
        ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>() ;
        servletRegistrationBean.setServlet(hystrixMetricsStreamServlet);
        servletRegistrationBean.setLoadOnStartup(10);
        servletRegistrationBean.addUrlMappings("/hystrix.stream");
        servletRegistrationBean.setName("HystrixMetricsStreamServlet");
        return servletRegistrationBean ;
    }
}
  • 重復第一個步驟,你會看到有一個ping一直在ping,其實就是在ping我們工程中hystrix默認給我們埋下的監控點,這也是為什麽我們要用spring全家桶的原因之一,在其他的框架中像這樣的事情,是需要我們自己去設計,開發的。而spring直接幫我們集成進來了,配置、配置就可以用了。效果如下:
    技術分享圖片

  • 我們再次訪問http://localhost:8001/hystrix,監控http://localhost:8001/hystrix.stream這個地址,你會發現如圖下界面,但是很不幸的告訴你,我們仍然看不到效果,為什麽?賣個關子,請接著往下看。
    技術分享圖片

  • 請使用同樣的方法將"mrico-service-consumer"工程改造,改造完後,我們訪問http://localhost:8002/hystrix,輸入http://localhost:8002/hystrix.stream這個地址進行監控,你會發現奇跡,但是一樣看不到東西,這時候我們訪問一下消費服務的接口http://localhost:8002/consumerHelloWorld?name=rose,然後再返回看一下監控頁面,如果頁面沒有自動刷新,就手動刷一下頁面,你就會看到效果了。這裏面的英文是什麽意思,請自行搜索,效果如下:
    技術分享圖片

2.5 小結

經過上面的步驟,我們大致明白了一些問題:

  1. 服務的整個監控是由spring cloud幫我們集成的,而且不需要我們寫任何代碼。
  2. 對於服務監控,監控的是調用方,而不是服務提供方,我個人認為這也是一種思想,不用過度設計,比如你服務方和消費方都去監控。我相信在spring cloud沒有出來之前應該有人這麽做,但我嘗試這種思想後過後覺得意義並不大。那麽剛才我們講的監控埋點,也是埋在了消費方的接口。對此有興趣的小夥伴如果想知道更詳細的內容,可以去百度一下。
  3. 為什麽我第二個視頻SpringCloud實戰之初級入門(二)— 服務註冊與服務調用會講到說,在真實環境中像這種一個提供服務,一個提供調用的單向調用基本不存在,今天兩個工程都加入了服務監控,大家也都看到了,第一個工程根本看不到任何東西,因為它沒有調用其他的服務。
  4. 為什麽先講服務的監控後講熔斷?因為你只有明白了監控是怎麽一過程,你才能更好的了解熔斷,因為熔斷也是在消費方實現的。

3. 利用hystrix實現消費服務熔斷

3.1 加入服務熔斷

技術分享圖片
其實如果你在spring全家桶裏面做集成,做這個熔斷非常簡單,接下來我們寫MyFristConsumerHystrix這個類,如下:

@Component
public class MyFristConsumerHystrix implements MyFristConsumer{
    @Override
    public String helloWorld(String name) {
        return "server is closed , your send failed";
    }
}

3.2 測試服務熔斷

  1. 關掉服務提供工程
  2. 訪問調用接口,你會看如下結果:
    技術分享圖片
  3. 查看監控,你就會發現圖標變紅了。
    技術分享圖片

    3.3 小結

  4. 關於服務熔斷,在真實使用中也不是說所有的服務都需要熔斷,這個要根據實際情況來。
  5. 服務熔斷的目的是為了避免微服務環境下,應用因為網絡抖動或者服務異常的解決方案。如果應用長時間的服務異常,並且服務與服務之間依賴過多,就會引起“級聯”效果,造成服務雪崩。

4. 利用turbine監控所有應用

4.1 創建工程

創建一個名稱"mirco-service-turbine-hystrix-dashboard"的工程,在pom文件中要有以下依賴:

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

因為我們的服務都已經註冊到了eureka上,所以只需要連上eureka,從eureka上找到需要監控的服務就可以了。

4.2 修改配置文件

在application.yml文件中另入以下配置

spring.application.name: turbine-hystrix-dashboard
server.port: 6001

turbine:
  appConfig: service-provider,service-consumer
  clusterNameExpression: new String("default")
  instanceUrlSuffix: hystrix.stream
  aggregator:
    clusterConfig: default

eureka:
  client: 
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/

4.3 修改啟動文件

@EnableDiscoveryClient
@EnableTurbine
@EnableHystrixDashboard
@SpringBootApplication
public class MircoServiceTurbineHystrixDashboardApplication {

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

    @Bean
    public ServletRegistrationBean<TurbineStreamServlet> getServlet() {
        TurbineStreamServlet streamServlet = new TurbineStreamServlet();
        ServletRegistrationBean<TurbineStreamServlet> registrationBean = new ServletRegistrationBean<TurbineStreamServlet>(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/turbine.stream");
        registrationBean.setName("TurbineStreamServlet");
        return registrationBean;
    }     
}

4.4 啟動

  1. 依次啟動“mirco-service-config”、“mirco-service-eureka”、"mirco-service-provider"、"mirco-service-consumer"、“mirco-service-turbine-hystrix-dashboard”這五個工程
  2. 訪問http://localhost:8002/consumerHelloWorld?name=rose
  3. 訪問http://localhost:6001/hystrix,監控http://localhost:6001/turbine.stream,就可以看到剛才在"mirco-service-consumer"監控裏面看到的內容了,如果你想看到多個服務,那麽你寫多幾個服務,只要被調用過都可以看到,親測可用。

5.一點點重要的事情

  • 教程中源代碼地址github-Spring Cloud實戰之初級入門(四)— 利用Hystrix實現服務熔斷與服務監控
  • 教程對應的視頻地址愛奇藝-Spring Cloud實戰之初級入門(四)— 利用Hystrix實現服務熔斷與服務監控

Spring Cloud實戰之初級入門(四)— 利用Hystrix實現服務熔斷與服務監控