一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringCloud中的斷路器(Hystrix)和斷路器監控(Dashboard)

SpringCloud中的斷路器(Hystrix)和斷路器監控(Dashboard)

2019-07-07 17:07虛無境 Java教程

本篇主要介紹的是SpringCloud中的斷路器(Hystrix)和斷路器指標看板(Dashboard)的相關使用知識,需要的朋友可以參考下

前言

本篇主要介紹的是SpringCloud中的斷路器(Hystrix)和斷路器指標看板(Dashboard)的相關使用知識。

SpringCloud Hystrix

Hystrix 介紹

Netflix創建了一個名為Hystrix的庫,它實現了斷路器模式。主要的目的是為了解決服務雪崩效應的一個組件,是保護服務高可用的最后一道防線。

開發準備

開發環境

•JDK:1.8
•SpringBoot:2.1.1.RELEASE
•SpringCloud:Finchley

注:不一定非要用上述的版本,可以根據情況進行相應的調整。需要注意的是SpringBoot2.x以后,jdk的版本必須是1.8以上!

確認了開發環境之后,我們再來添加相關的pom依賴。

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

注: 實際上這里是不需要Hystrix依賴的,Fegin已經添加了Hystrix依賴。

SpringCloud Hystrix 示例

由于Hystrix機制是在微服務項目上進行的,并且Fegin中包含了該機制。所以這里我們可以把之前的springcloud-feign的項目進行簡單的改造就行了。

服務端

首先是服務端這塊,為了進行區分,創建一個springcloud-hystrix-eureka的項目,用于做注冊中心。 代碼和配置和之前的基本一樣。

application.properties配置信息:

配置信息:

spring.application.name=springcloud-hystrix-eureka-server
server.port=8002
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8002/eureka/

配置說明:

•spring.application.name: 這個是指定服務名稱。
•server.port:服務指定的端口。
•eureka.client.register-with-eureka:表示是否將自己注冊到Eureka Server,默認是true。
•eureka.client.fetch-registry:表示是否從Eureka Server獲取注冊信息,默認為true。
•eureka.client.serviceUrl.defaultZone: 這個是設置與Eureka Server交互的地址,客戶端的查詢服務和注冊服務都需要依賴這個地址。

服務端這邊只需要在SpringBoot啟動類添加@EnableEurekaServer注解就可以了,該注解表示此服務是一個服務注冊中心服務。

代碼示例:

 @EnableEurekaServer
 @SpringBootApplication
 public class HystrixEurekaApplication {
  public static void main(String[] args) {
   SpringApplication.run(HystrixEurekaApplication.class, args);
   System.out.println("hystrix注冊中心服務啟動...");
  }
 }

客戶端

這里我們把之前的springcloud-fegin-consumer項目稍微改造下,項目名改為springcloud-hystrix-consumer。然后在application.properties配置文件新增如下配置, feign.hystrix.enabled配置表示是否啟用熔斷機制。

    feign.hystrix.enabled=true

增加了配置之后,我們在來把之前的fegin進行定義轉發服務的@FeignClient注解進行添加一個回調方法fallback。代碼改造后的實現如下:

 @FeignClient(name= "springcloud-hystrix-consumer2",fallback = HelloRemoteHystrix.class)
 public interface HelloRemote {
  @RequestMapping(value = "/hello")
  public String hello(@RequestParam(value = "name") String name);
 } 

最后新增一個回調類,用于處理斷路的情況。這里我們就簡單的處理下,返回錯誤信息即可!

代碼示例:

 @Component
 public class HelloRemoteHystrix implements HelloRemote{
 
  @Override
  public String hello(@RequestParam(value = "name") String name) {
   return name+", 請求另一個服務失敗!";
  }
 }

其余的代碼就不展示了,和之前的springcloud-fegin-consumer項目中的一致,然后在把之前的springcloud-feign-consumer2進行簡單的改造下,項目名稱改為springcloud-hystrix-consumer2。然后更改下配置的端口。

功能測試

完成如上的工程開發之后,我們依次啟動服務端和客戶端的springcloud-hystrix-eureka、springcloud-hystrix-consumer和springcloud-hystrix-consumer2這三個程序,然后在瀏覽器界面輸入:http://localhost:8002/,即可查看注冊中心的信息。

首先在瀏覽器輸入:

http://localhost:9004/hello/pancm

控制臺打印:

接受到請求參數:pancm,進行轉發到其他服務

瀏覽器返回:

pancm,Hello World

然后再輸入:

http://localhost:9005/hello?name=pancm

瀏覽器返回:

pancm,Hello World

說明程序運行正常,fegin的調用也ok。這時我們在進行斷路測試。

 停止springcloud-hystrix-consumer2這個服務,然后在到瀏覽器輸入:http://localhost:9004/hello/pancm 進行查看信息。

控制臺打印:

接受到請求參數:pancm,進行轉發到其他服務

瀏覽器返回:

pancm, 請求另一個服務失敗!

出現以上結果說明斷路器的功能已經實現了。

示例圖:

SpringCloud中的斷路器(Hystrix)和斷路器監控(Dashboard)

SpringCloud Hystrix-Dashboard

Hystrix-Dashboard 介紹

Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等數據。

開發準備

開發環境

•JDK:1.8
•SpringBoot:2.1.1.RELEASE
•SpringCloud:Finchley

注:不一定非要用上述的版本,可以根據情況進行相應的調整。需要注意的是SpringBoot2.x以后,jdk的版本必須是1.8以上!

確認了開發環境之后,我們再來添加相關的pom依賴。

<dependencies>
 <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-openfeign</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.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
</dependencies>

注: spring-boot-starter-actuator這個必須需要要,該jar可以得到SpringBoot項目的各種信息,關于該jar包的使用,可以在springboot-actuator這個項目中進行查看。

SpringCloud Hystrix-Dashboard 示例

這里我們把上面的springcloud-hystrix-consumer項目進行改造下,項目名稱改為springcloud-hystrix-dashboard-consumer。然后在到啟動類新增如下配置。

•EnableCircuitBreaker:表示啟用hystrix功能。
•EnableHystrixDashboard:啟用 HystrixDashboard 斷路器看板 相關配置。

完整的啟動類配置如下:

 @SpringBootApplication
 @EnableDiscoveryClient
 @EnableHystrixDashboard
 @EnableCircuitBreaker
 @EnableFeignClients
 public class HystrixDashboardApplication {
  public static void main(String[] args) {
   SpringApplication.run(HystrixDashboardApplication.class, args);
    System.out.println("hystrix dashboard 服務啟動...");
  }
 }

然后在到application.properties配置文件中新增如下配置:

    management.endpoints.web.exposure.include=hystrix.stream
    management.endpoints.web.base-path=/

該配置的意思是指定hystrixDashboard的訪問路徑,SpringBoot2.x以上必須指定,不然是無法進行訪問的,訪問會出現 Unable to connect to Command Metric Stream 錯誤。

如果不想使用配置的話,也可以使用代碼進行實現。

 實現的代碼如下:

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

功能測試

完成如上的工程改造之后,我們啟動該程序。

 然后在瀏覽器輸入:

http://localhost:9010/hystrix

會出現以下的界面:

SpringCloud中的斷路器(Hystrix)和斷路器監控(Dashboard)

可以通過該界面監控使用了hystrix dashboard的項目。這里我們依照提示在中間的輸入框輸入如下的地址:

http://localhost:9010/hystrix.stream

會出現以下的界面:

SpringCloud中的斷路器(Hystrix)和斷路器監控(Dashboard)

該界面就是Hystrix Dashboard監控的界面了,通過這個界面我們可以很詳細的看到程序的信息。關于這些信息中說明可以用網上找到的一張來加以說明。

SpringCloud中的斷路器(Hystrix)和斷路器監控(Dashboard)

注: 如果界面一直提示loading,那么是因為沒有進行請求訪問,只需在到瀏覽器上輸入:http://localhost:9010/hello/pancm,然后刷新該界面就可以進行查看了。

項目地址

基于SpringBoot2.x、SpringCloud的Finchley版本開發的地址:https://github.com/xuwujing/springcloud-study

基于SpringBoot1.x、SpringCloud 的Dalston版本開發的地址: https://github.com/xuwujing/springcloud-study-old

總結

以上所述是小編給大家介紹的SpringCloud中的斷路器(Hystrix)和斷路器監控(Dashboard),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲va久久久噜噜噜久久狠狠 | 亚洲欧美日韩久久一区 | 色老太bbbbb 色老妇 | 四虎免费在线观看视频 | 成人免费视频一区二区 | 日本春菜花在线中文字幕 | 精品久久综合一区二区 | 精品国产欧美一区二区 | 星星动漫在线观看无删减 | 涩涩成人 | 亚洲精品一二区 | 日韩毛片免费线上观看 | 香蕉精品国产高清自在自线 | 欧美第一视频 | 99视频在线看观免费 | 国产亚洲精品视频中文字幕 | 亚洲国产精品综合久久一线 | 农村妇女野战bbxxx | 色操网| 精品免费久久久久久成人影院 | 欧美一级艳片视频免费观看 | 五月色天在线视频综合观看 | 四虎国产欧美成人影院 | 精品一区二区免费视频蜜桃网 | 99久久精品免费观看区一 | 天天夜夜草草久久伊人天堂 | 妹妹骑上来蹭着蹭着就射了 | 四虎成人永久地址 | 亚洲国产经典 | 扒开双腿猛进入爽爽视频ai | 女人把私密部位张开让男人桶 | 四虎国产精品视频免费看 | 九九影院午夜理论片无码 | 数学老师扒开腿让我爽快 | 大陆男男gayxxxxvideo | 精品国产自在现线拍400部 | x8x8在线永久免费观看 | 亚洲欧美一区二区三区不卡 | 男同志gays| 王的视频vk | 精东影业传媒全部作品 |