在分布式系統(tǒng)中,一個(gè)子節(jié)點(diǎn)的超時(shí)或者故障會(huì)引起關(guān)聯(lián)節(jié)點(diǎn)的故障,從而蔓延到整個(gè)系統(tǒng)中,比如庫存服務(wù)超時(shí)堰塌,商品服務(wù)獲取不到庫存,訂單服務(wù)無法獲取到商品...因此分布式系統(tǒng)中需要一個(gè)容錯(cuò)管理來管理這些容錯(cuò)分衫,SpringCloud提供Hystrix來實(shí)現(xiàn)服務(wù)降級(jí)和熔斷场刑,本文將通過一個(gè)簡單的例子來介紹Hystrix。
系列文章
SpringCloud(一)-手把手教你創(chuàng)建springcloud微服務(wù)父子項(xiàng)目
SpringCloud(二)-手把手教你搭建Eureka Server和Eureka Client
SpringCloud(三)-手把手教你通過Rinbbon實(shí)現(xiàn)客戶端負(fù)載均衡
SpringCloud(四)-手把手教你使用OpenFeign
SpringCloud(五)-手把手教你使用Hystrix配置服務(wù)熔斷和降級(jí)以及Hystrix Dashboard
SpringCloud(六)-手把手教你搭建SpringCloud Config配置中心
SpringCloud(七)-手把手教你使用消息總線Bus實(shí)現(xiàn)動(dòng)態(tài)刷新
SpringCloud(八)-手把手教你使用Stream消息驅(qū)動(dòng)
1. Hystrix簡介
Hystrix提供了服務(wù)降級(jí)蚪战,服務(wù)熔斷牵现,服務(wù)限流等服務(wù)铐懊。
2. 降級(jí)
降級(jí)是指,當(dāng)請(qǐng)求超時(shí)瞎疼、資源不足等情況發(fā)生時(shí)進(jìn)行服務(wù)降級(jí)處理居扒,不調(diào)用真實(shí)服務(wù)邏輯,而是使用快速失敵笊鳌(fallback)方式直接返回一個(gè)托底數(shù)據(jù),保證服務(wù)鏈條的完整瓤摧,避免服務(wù)雪崩竿裂。
2.1 pom.xml引入依賴
找到消費(fèi)者8200項(xiàng)目,修改pom.xml配置
<!--hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.2 主啟動(dòng)類加上@EnableFeignClients注解
package com.elio.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class ProductConsumer8200 {
public static void main(String[] args){
SpringApplication.run(ProductConsumer8200.class, args);
}
}
2.3 controller接口中加上fallback函數(shù)
ProductConsumerController中以selectById方法為例照弥,假設(shè)服務(wù)提供者根據(jù)傳入的id為正則正常腻异,為負(fù)數(shù)拋出一個(gè)異常來模擬。那么在selectById方法上加上@HystrixCommand(fallbackMethod = "getErroInfo")注解这揣,其中g(shù)etErroInfo就是降級(jí)函數(shù)
package com.elio.springcloud.controller;
import com.elio.springcloud.dto.Result;
import com.elio.springcloud.service.ProductService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class ProductConsumerController {
@Resource
RestTemplate restTemplate;
@Resource
ProductService productService;
//public static String url = "http://localhost:8100/";
public static String url = "http://springcloud-product-provider/";
/**
* 查詢
* @return
*/
@GetMapping("product/consumer/get/info")
public Result getServiceInfo(){
return productService.getServiceInfo();
/*return new Result(200, "查詢成功",
restTemplate.getForObject(url+"product/provider/get/info", Result.class));*/
}
/**
* 查詢
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "getErroInfo")
@GetMapping("product/consumer/get/{id}")
public Result selectById(@PathVariable("id") Long id){
return productService.selectById(id);
/* return new Result(200, "查詢成功",
restTemplate.getForObject(url+"product/provider/get/"+id, Result.class));*/
}
public Result getErroInfo(){
return new Result(500, "服務(wù)器內(nèi)部出現(xiàn)錯(cuò)誤", null);
}
}
2.4 測試
啟動(dòng)相應(yīng)服務(wù)后悔常,訪問消費(fèi)者API,結(jié)果出現(xiàn)異常頁面,這不是我們期望的给赞。
查看控制臺(tái)日志机打,發(fā)現(xiàn)降級(jí)函數(shù)要與被處理的函數(shù)參數(shù)一致,這個(gè)也是個(gè)小坑
因此修改上面的getErroInfo加上參數(shù)
package com.elio.springcloud.controller;
import com.elio.springcloud.dto.Result;
import com.elio.springcloud.service.ProductService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class ProductConsumerController {
@Resource
RestTemplate restTemplate;
@Resource
ProductService productService;
//public static String url = "http://localhost:8100/";
public static String url = "http://springcloud-product-provider/";
/**
* 查詢
* @return
*/
@GetMapping("product/consumer/get/info")
public Result getServiceInfo(){
return productService.getServiceInfo();
/*return new Result(200, "查詢成功",
restTemplate.getForObject(url+"product/provider/get/info", Result.class));*/
}
/**
* 查詢
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "getErroInfo")
@GetMapping("product/consumer/get/{id}")
public Result selectById(@PathVariable("id") Long id){
return productService.selectById(id);
/* return new Result(200, "查詢成功",
restTemplate.getForObject(url+"product/provider/get/"+id, Result.class));*/
}
public Result getErroInfo(Long id){
return new Result(500, "服務(wù)器內(nèi)部出現(xiàn)錯(cuò)誤", null);
}
}
最終id傳入-1片迅,和1的結(jié)果如下残邀。我們可以看到,當(dāng)傳入為-1時(shí)柑蛇,參數(shù)不正常芥挣,服務(wù)提供者會(huì)拋出異常,因?yàn)槲覀冊(cè)诜?wù)消費(fèi)者方定義了降級(jí)函數(shù)耻台,所以會(huì)調(diào)用降級(jí)函數(shù)getErroInfo 的邏輯 空免,這比上面我們出錯(cuò)拋出錯(cuò)誤異常也面要友好的多。當(dāng)傳入為1時(shí)盆耽,參數(shù)正常蹋砚,返回結(jié)果也正常執(zhí)行正常邏輯。
3. 使用OpenFegin 處理降級(jí)
通過我們上面的例子我們雖然實(shí)現(xiàn)了服務(wù)的降級(jí)都弹,但是問題就是業(yè)務(wù)代碼和公共代碼耦合在一起了,這對(duì)后續(xù)的闊這和維護(hù)增加了困難匙姜,因此我們需要另外的將降級(jí)邏輯提取出來畅厢。我們上篇文章介紹的OpenFeign其實(shí)就已經(jīng)集成了Hystrix,我們可以直接使用OpenFeign來實(shí)現(xiàn)服務(wù)的降級(jí)氮昧。
3.1 修改application.yml配置文件
server:
port: 8200
spring:
application:
name: springcloud-product-consumer
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8300/eureka/,http://localhost:8301/eureka/
feign:
hystrix:
enabled: true # fegin默認(rèn)關(guān)閉hystrix服務(wù)
3.2 主啟動(dòng)類加上@EnableCircuitBreaker注解
package com.elio.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class})
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class ProductConsumer8200 {
public static void main(String[] args){
SpringApplication.run(ProductConsumer8200.class, args);
}
}
3.3 新增降級(jí)服務(wù)類
因?yàn)槲覀円獙?duì)OpenFeign接口進(jìn)行服務(wù)降級(jí)框杜,所以我們需要新增一個(gè)回調(diào)類實(shí)現(xiàn)ProductService接口浦楣,對(duì)每個(gè)方法進(jìn)行回調(diào)處理。新增ProductFallbackServieImpl.java
package com.elio.springcloud.service.impl;
import com.elio.springcloud.dto.Result;
import com.elio.springcloud.service.ProductService;
import org.springframework.stereotype.Component;
@Component
public class ProductFallbackServieImpl implements ProductService {
public Result getServiceInfo() {
return new Result(500,"服務(wù)器內(nèi)部出現(xiàn)錯(cuò)誤咪辱,導(dǎo)致getServiceInfo接口異常",null);
}
public Result selectById(Long id) {
return new Result(500,"服務(wù)器內(nèi)部出現(xiàn)錯(cuò)誤振劳,導(dǎo)致selectById接口異常",null);
}
}
3.4 商品接口類添加降級(jí)類
我們?cè)赑roductService接口上的@FeignClient注解,指定對(duì)應(yīng)的降級(jí)處理類為ProductFallbackServieImpl
package com.elio.springcloud.service;
import com.elio.springcloud.dto.Result;
import com.elio.springcloud.service.impl.ProductFallbackServieImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(name="springcloud-product-provider", fallback = ProductFallbackServieImpl.class)
public interface ProductService {
/**
* 查詢
* @return
*/
@GetMapping("product/provider/get/info")
public Result getServiceInfo();
/**
* 查詢
* @param id
* @return
*/
@GetMapping("product/provider/get/{id}")
public Result selectById(@PathVariable("id") Long id);
}
3.5 測試
接下來就是測試了油狂,首先我們注釋掉之前寫的@HystrixCommand 注解
package com.elio.springcloud.controller;
import com.elio.springcloud.dto.Result;
import com.elio.springcloud.service.ProductService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
public class ProductConsumerController {
@Resource
RestTemplate restTemplate;
@Resource
ProductService productService;
//public static String url = "http://localhost:8100/";
public static String url = "http://springcloud-product-provider/";
/**
* 查詢
* @return
*/
@GetMapping("product/consumer/get/info")
public Result getServiceInfo(){
return productService.getServiceInfo();
/*return new Result(200, "查詢成功",
restTemplate.getForObject(url+"product/provider/get/info", Result.class));*/
}
/**
* 查詢
* @param id
* @return
*/
//@HystrixCommand(fallbackMethod = "getErroInfo")
@GetMapping("product/consumer/get/{id}")
public Result selectById(@PathVariable("id") Long id){
return productService.selectById(id);
/* return new Result(200, "查詢成功",
restTemplate.getForObject(url+"product/provider/get/"+id, Result.class));*/
}
public Result getErroInfo(Long id){
return new Result(500, "服務(wù)器內(nèi)部出現(xiàn)錯(cuò)誤", null);
}
}
依次啟動(dòng)8300历恐,8100,8101专筷,8200弱贼,然后測試接口 http://localhost:8200/product/consumer/get/1 ,發(fā)現(xiàn)成功進(jìn)入降級(jí)函數(shù)磷蛹,這比將降級(jí)邏輯和業(yè)務(wù)邏輯混雜在一起好很多吮旅。
4. 熔斷
熔斷很好理解, 當(dāng)Hystrix Command請(qǐng)求后端服務(wù)失敗數(shù)量超過一定比例(默認(rèn)50%), 斷路器會(huì)切換到開路狀態(tài)(Open). 這時(shí)所有請(qǐng)求會(huì)直接失敗而不會(huì)發(fā)送到后端服務(wù). 斷路器保持在開路狀態(tài)一段時(shí)間后(默認(rèn)5秒), 自動(dòng)切換到半開路狀態(tài)(HALF-OPEN). 這時(shí)會(huì)判斷下一次請(qǐng)求的返回情況, 如果請(qǐng)求成功, 斷路器切回閉路狀態(tài)(CLOSED), 否則重新切換到開路狀態(tài)(OPEN). Hystrix的斷路器就像我們家庭電路中的保險(xiǎn)絲, 一旦后端服務(wù)不可用, 斷路器會(huì)直接切斷請(qǐng)求鏈, 避免發(fā)送大量無效請(qǐng)求影響系統(tǒng)吞吐量, 并且斷路器有自我檢測并恢復(fù)的能力。熔斷器一般是在服務(wù)提供者配置的味咳,而服務(wù)降級(jí)是在服務(wù)消費(fèi)端使用的,接下來通過一個(gè)簡單的例子來了解熔斷機(jī)制庇勃。
4.1 修改pom.xml引入依賴
找到服務(wù)提供者項(xiàng)目8100,然后找到對(duì)應(yīng)的pom.xml引入Hystrix依賴
<!--hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- actuator監(jiān)控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4.2 主啟動(dòng)類加上注解
我們找到服務(wù)提供者項(xiàng)目8100槽驶,然后添加注解 @EnableCircuitBreaker责嚷,由于我們需要由Hystrix Dashboard實(shí)時(shí)監(jiān)控,所以還需要配置一個(gè)Servlet掂铐。具體儀表盤如何配置再层,請(qǐng)求第五步。
package com.elio.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
@MapperScan("com.elio.springcloud.dao")
@EnableDiscoveryClient
@EnableCircuitBreaker
@Configuration
public class ProductProvider8100 {
public static void main(String[] args){
SpringApplication.run(ProductProvider8100.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;
}
}
4.3 servie配置熔斷
以service實(shí)現(xiàn)類ProductServiceImpl中的selectById方法為例子堡纬,來配置熔斷信息聂受。其中fallbackMethod配置的降級(jí)函數(shù),
- "circuitBreaker.enabled" 配置是否開啟服務(wù)熔斷
- "circuitBreaker.requestVolumeThreshold"為時(shí)間窗口內(nèi)的請(qǐng)求閾值烤镐,只有達(dá)到這個(gè)閾值蛋济,才會(huì)判斷是否打開斷路器。比如配置為10次炮叶,那么在時(shí)間窗口內(nèi)請(qǐng)求9次碗旅,9次都失敗了也不會(huì)打開斷路器
- "circuitBreaker.sleepWindowInMilliseconds"為時(shí)間窗口,當(dāng)斷路器打開后镜悉,會(huì)根據(jù)這個(gè)時(shí)間繼續(xù)嘗試接受請(qǐng)求祟辟,如果請(qǐng)求成功則關(guān)閉斷路器。
- "circuitBreaker.errorThresholdPercentage"為配置的失敗比率侣肄,在時(shí)間窗口內(nèi)請(qǐng)求次數(shù)達(dá)到請(qǐng)求閾值旧困,并且失敗比率達(dá)到配置的50%,才會(huì)打開斷路器。
package com.elio.springcloud.service.impl;
import com.elio.springcloud.dao.ProductMapper;
import com.elio.springcloud.dto.Result;
import com.elio.springcloud.entity.Product;
import com.elio.springcloud.service.ProductService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class ProductServiceImpl implements ProductService{
@Resource
private ProductMapper productMapper;
@HystrixCommand(fallbackMethod = "selectByIdFallbackHandler", commandProperties = {
// 是否啟用服務(wù)熔斷
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
// 請(qǐng)求閾值
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
// 時(shí)間窗口
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
// 錯(cuò)誤比率
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50")
})
public Product selectById(Long id) throws Exception {
if(id < 0){
throw new Exception("id為負(fù)數(shù)");
}
return productMapper.selectById(id);
}
public Product selectByIdFallbackHandler(Long id){
return null;
}
public int deleteById(Long id) {
return productMapper.deleteById(id);
}
public int updateById(Long id, String name) {
return productMapper.updateById(id, name);
}
public int insertOne(Product product) {
return productMapper.insertOne(product);
}
}
4.4 測試
現(xiàn)在配置好了吼具,啟動(dòng)服務(wù)提供者8100項(xiàng)目僚纷,然后不斷地請(qǐng)求http://localhost:8100/product/provider/get/-1接口,這是儀表盤的信息如下:
請(qǐng)求8次拗盒,失敗率為100%,沒有達(dá)到請(qǐng)求閾值怖竭,斷路器還是關(guān)閉狀態(tài)
請(qǐng)求13次,失敗率為100%,達(dá)到請(qǐng)求閾值陡蝇,斷路器處于打開狀態(tài)
5. Hystrix Dashboard
Hystrix 熔斷工作流程就是收集接口的訪問情況痊臭,從而判斷是否打開熔斷來保護(hù)服務(wù)。在開發(fā)或者運(yùn)維的過程中登夫,我們需要一個(gè)圖形界面實(shí)時(shí)觀察這些信息广匙,接下來就通過搭建Hystrix Dashboard來實(shí)時(shí)觀察Hystrix熔斷信息。
5.1 新增springcloud-hystrix-dashboard-8400子項(xiàng)目
新增一個(gè)名為springcloud-hystrix-dashboard-8400的dashboard子項(xiàng)目來進(jìn)行信息監(jiān)控悼嫉。
5.1 修改pom.xml引入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloudtest</artifactId>
<groupId>com.elio.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-hystrix-dashboard</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- Ribbon相關(guān) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- feign相關(guān) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- hystrix和 hystrix-dashboard相關(guān) -->
<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>
</dependencies>
</project>
5.2 新增主啟動(dòng)類
新增主啟動(dòng)類 HystrixDashboard8400,主要是加上@EnableHystrixDashboard這個(gè)注解
package com.elio.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboard8400 {
public static void main(String[] args){
SpringApplication.run(HystrixDashboard8400.class, args);
}
}
5.3 測試
接下倆就是啟動(dòng)項(xiàng)目來判斷dashboard是否搭建成功拼窥,啟動(dòng)后瀏覽器中輸入http://localhost:8400/hystrix
- 第一行輸入欄就是我們要監(jiān)控的微服務(wù)的地址
- delay 參數(shù)用來控制服務(wù)器上輪詢監(jiān)控信息的延遲時(shí)間戏蔑,默認(rèn)是2000毫秒,可以通過配置該屬性來降低客戶端的網(wǎng)絡(luò)和cpu消耗鲁纠。
- title 就是監(jiān)控信息的標(biāo)題总棵,這個(gè)我們一般填寫微服務(wù)名
5.4 消費(fèi)者8200修改配置
因?yàn)?200開啟了熔斷配置,因此我們需要監(jiān)控8200接口的訪問信息改含,此時(shí)我們需要修改8200項(xiàng)目的pom.xml新增以下依賴
<!-- actuator監(jiān)控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
5.5 測試監(jiān)控消費(fèi)者8200的信息
現(xiàn)在消費(fèi)者8200配置好了依賴情龄,然后在dashboard主界面輸入我們需要監(jiān)控的信息,如下圖
點(diǎn)擊 monitor stream按鈕捍壤,結(jié)果出現(xiàn)Unable to connect to Command Metric Stream.報(bào)錯(cuò)信息骤视,但是我們?cè)撆渲玫亩寂渲煤昧耍趺催€會(huì)出現(xiàn)這個(gè)錯(cuò)誤呢鹃觉,原來因?yàn)槲覀兪褂玫腟pringCloud版本是H版本专酗,版本太高了,我們需要配置一個(gè)servlet盗扇。
5.6 配置HystrixMetricsStreamServlet
找到消費(fèi)者端8200的RestConfig配置類祷肯,配置HystrixMetricsStreamServlet這個(gè)Servlet
package com.elio.springcloud.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@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;
}
}
5.7 測試
現(xiàn)在該有的配置都配置好了,接下來就是重啟服務(wù)提供方測試了疗隶,再進(jìn)進(jìn)入dashboard界面佑笋,效果如下
現(xiàn)在我們嘗試著不斷訪問接口,效果如下
儀表盤的數(shù)據(jù)也會(huì)跟著變動(dòng)斑鼻,出現(xiàn)的錯(cuò)誤信息如下蒋纬,出現(xiàn)5個(gè)錯(cuò)誤,100%失敗率。
6 總結(jié)
這篇文章簡單的介紹了如何在消費(fèi)端配置服務(wù)降級(jí)和熔斷颠锉,但是熔斷原理和一些概念性的支持本文沒有涉及到法牲,因?yàn)閮?yōu)點(diǎn)復(fù)雜,需要另開一篇文章來講解了琼掠。