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


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

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

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.一點點重要的事情


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM