一篇文章带你搞懂SpringCloud Hystrix 服务降级、服务熔断、服务监控、JMeter性能压测工具的安装和简单使用。
分布式系统面临的问题
服务雪崩
多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微服务,这就是所谓的“扇出”。
如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的“雪崩效应”.
对于高流量的应用来说,单一的后端依赖可能会导致所有服务 器上的所有资源都在几秒钟内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障。这些都表示需要对故障和延迟进行隔离和管理,以便单个依赖关系的失败,不能取消整个应用程序或系统。
所以,通常当你发现一个模块下的某个实例失败后,这时候这个模块依然还会接收流量,然后这个有问题的模块还调用了其他的模块,这样就
会发生级联故障,或者叫雪崩。
Hystrix简单介绍
Hystrix是一个用于处理分布式系统的延迟和容错的开源库, 在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下, 不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
“断路器”本身是-种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝) .向调用方返回一个符合预期的、可处理的备选响应(FallBack) ,而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
作用
服务降级
服务熔断
接近实时的监控
现在Hystrix已经停止更新了
Hystrix重要的概念
三个核心的概念服务降级(fallback)服务熔断(break)服务限流(flowlimit)
服务降级
服务器忙,请稍后再试,不让客户端等待并立刻返回-个友好提示,fallback
哪些情况会出发降级
-
程序运行异常
-
超时
-
服务熔断触发服务降级
-
线程池/信号量打满也会导致服务降级
服务熔断
2.1 类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示
2.2 就是保险丝
服务的降级->进而熔断->恢复调用链路
服务限流
秒杀高并发等操作,严禁-窝蜂的过来拥挤,大家排队,一秒钟N个,有序进行
Hystrix支付微服务构建
新建一个带有Hystrix的微服务模块新建cloud-provider-hystrix-payment8001
引入pom文件的相关的依赖
与之前的项目相比较来说我们增加了一个新的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependencies>
<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-eureka-client</artifactId>
</dependency>
<!--web包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--通用包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
为了项目的方便测试7002的eureka集群环境关闭掉。我们使用单机版的eureka来进行测试。
eureka:
client:
register-with-eureka: true #表示向注册中心注册自己 默认为true
fetch-registry: true #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
service-url:
defaultZone: http://localhost:7001/eureka
# defaultZone: http://localhost:7001/eureka/,http://localhost:7001/eureka/ # 入驻地址
instance:
instance-id: payment8001
prefer-ip-address: true
配置文件的编写
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-payment
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
之后新建主启动类
package com.dzu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author chenruxu
* @date 1.7
* 主启动类编写
*/
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
}
}
编写完成之后,进行业务类的实现和具体的编写和测试(简单起见只有一个业务层的实现类来进行代替)
package com.dzu.springboot.service;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class PaymentService {
/**
* 正常访问
* @param id
* @return
*/
public String paymentInfo_ok(Integer id){
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_ok, id :"+id+"\t"+"成功";
}
/**
* 可能会出现超时报错的方法
* @param id
* @return
*/
public String paymentInfo_timeout(Integer id){
int time=3;
try {
TimeUnit.SECONDS.sleep(time);
}catch (InterruptedException e){
e.printStackTrace();
}
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_timeout, id :"+id+"\t"+"耗时(秒): "+time;
}
}
控制器的实现
package com.dzu.springboot.controller;
import com.dzu.springboot.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
/**
* 正常访问
* @param id
* @return
*/
@RequestMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_ok(@PathVariable("id") Integer id){
String result=paymentService.paymentInfo_ok(id);
log.info("*******result:" +result);
return result;
}
/**
* 超时
* @param id
* @return
*/
@RequestMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_time(@PathVariable("id") Integer id){
String result=paymentService.paymentInfo_timeout(id);
log.info("*******result:" +result);
return result;
}
}
测试开启带有Hystrix的8001支付微服务,和7001eureka服务配置中心完成测试
此时说明了带有Hystrix的8001服务提供者的自测通过
JMeter进行高并发压测
Jmeter安装使用
官网的下载地址: https://jmeter.apache.org/download_jmeter.cgi
解压文件后配置Jmeter的环境变量
环境变量配置:开始配置环境变量了。在系统变量框,点击“新建”,建立一个变量:JMETER_HOME,值为你解压的jmeter安装路径
配置classpath变量,没有的话也要按照上面步骤进行新建,有的话直接进行选中,点击编辑即可。变量值固定为:%JMETER_HOME%\lib\ext\ApacheJMeter_core.jar;%JMETER_HOME%\lib\jorphan.jar;%JMETER_HOME%\lib/logkit-2.0.jar; 做完之后一定要保存,不确定的话可以直接点击确定按钮直到退到我的电脑页面。
测试使用
基本配置完成,然后验证一下是否配置正确,是否可用。
首先进到你的jmeter安装路径,找到bin文件夹,点击进去,找到jmeter.bat,鼠标右键用管理员方式运行,或者直接双击打开,此时会弹出2个界面:1.个是命令窗口,使用jmeter的时候此命令窗口不能关,你缩小到电脑任务栏即可。2.还有一个界面是jmeter工作页面,你可以在里面进行相关的操作.具体如图
到此时JMeter安装结束进入启动之后的页面
汉化设置
在使用jmeter5.4.1的时候发现每次进入jmeter的时候,它不能保留住上次使用jmeter配置的语言,每次打开都会初始化为英语,感觉很没有体验感。
这里我们只需要修改jmeter 的默认语言就能完美解决了
首先在jmeter 的bin目录下找到配置文件jmeter.properties,打开配置文件以后搜索***#language=en***
这里只需要将这一行的#去掉然后修改为:
保存后再打开就可以默认为简体中文了,不需要再每次设置语言。
同理可以设置为语言为自己需要的语言,只需要将默认语言设置为列表对应的缩写就行。
之后重新启动Jmeter.bat之后配置文件生效。此时出现汉化之后的页面
使用Jmeter对Hystrix的支付微服务进行压测
对整个系统的压测步骤进行简单的说明,最后根据压测的结果得出结论。
添加一个线程组,并设置并发量为20000次。
之后在线程组上添加一个Http的请求测试
添加完成之后使用get请求的方式对超时服务的方法进行压测。
之后启动项目进行压测,控制台这里疯狂的输出线程信息。此时对http://localhost:8001/payment/hystrix/timeout/3的高并发压测完成。
此时在访问正常的方法的时候发现该访问服务的响应变慢了。
在发送20000个请求后,http://localhost:8001/payment/hystrix/timeout/1和http://localhost:8001/payment/hystrix/ok/1两个请求都变慢了
为什么会被拖慢?
tomcat的默认的工作线程数被打满了,没有多余的线程来分解压力和处理。
订单微服务调用支付服务
下面将模拟访问的情况进行进一步的恶化,
创建订单访问的微服务80并对订单访问的微服务进行压测。
新建cloud-consumer-feign-hystrix-order80
修改pom引入需要使用的依赖环境
<dependencies>
<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-eureka-client</artifactId>
</dependency>
<!--web包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--通用包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
之后修改yaml的配置文件
server:
port: 80
eureka:
client:
#表示是否将自己注册进eurekaServer 默认为true
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
创建主启动类并加上启动OpenFegin组件的注解@EnableFeignClients
package com.dzu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
业务类的实现采用注解加接口的形式创建调用服务的service接口,和用来跳转控制的控制类
package com.dzu.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {
@RequestMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_ok(@PathVariable("id") Integer id);
@RequestMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_time(@PathVariable("id") Integer id);
}
控制器类
package com.dzu.springcloud.controller;
import com.dzu.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
/**
* 正常访问
* @param id
* @return
*/
@RequestMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_ok(@PathVariable("id") Integer id){
String result=paymentHystrixService.paymentInfo_ok(id);
return result;
}
/**
* 超时
* @param id
* @return
*/
@RequestMapping("/consumer/payment/hystrix/timeout/{id}")
public String paymentInfo_timeout(@PathVariable("id") Integer id){
String result=paymentHystrixService.paymentInfo_timeout(id);
return result;
}
}
开启这三个微服务来进行自测
自测8001的两个方法测试通过。
在没有压测的情况下通过客户端访问正常8001微服务上正常的方法。测试通过。
开启压测工具进行压测处理
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QyGblxo9-1641639284677)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20220107205743461.png)]
当并发量为2万时有时会出现超时报错的现象。
也有的情况下会正常的访问,但访问的速度变得很慢服务的性能变低。
压测前cpu性能
故障现象和导致原因
8001同一层次的其它接口服务被困死,因为tomcat线程池里面的工作线程已经被挤占完毕
80此时调用8001,客户端访问响应缓慢,转圈圈
降级容错解决的维度要求
解决的要求:
1.超时导致服务器变慢
2.出错(宕机或程序运行出错)
解决
1.对方服务(8001)超时了,调用者(80)不能一-直卡死等待,必须有服务降级
2.对方服务(8001 )down机了,调用者(80)不能一-直卡死等待,必须有服务降级
3.对方服务(8001)OK,调用者(80)自己出故障或有自我要求(自己的等待时间小于服务提供者),自己降级处理
Hystrix服务降级
支付微服务侧fallback
服务降级
降级配置:@ HystrixCommand
在8001微服务的控制器方法(服务上)添加注解@HystrixCommand在编写注解中放置的方法,用来做服务降级的处理。
package com.dzu.springboot.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class PaymentService {
/**
* 正常访问
* @param id
* @return
*/
public String paymentInfo_ok(Integer id){
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_ok, id :"+id+"\t"+"成功";
}
/**
* 可能会出现超时报错的方法
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler")
public String paymentInfo_timeout(Integer id){
int time=5;
try {
TimeUnit.SECONDS.sleep(time);
}catch (InterruptedException e){
e.printStackTrace();
}
return "线程池:"+Thread.currentThread().getName()+" paymentInfo_timeout, id :"+id+"\t"+"耗时(秒): "+time;
}
/**
* 超时或者报错之后的服务降级
* 兜底处理
* @param id
* @return
*/
public String paymentInfo_TimeOutHandler(Integer id){
return "线程池: "+Thread.currentThread().getName()+"id:="+id+"\t"+"服务降级";
}
}
在主启动类上加入注解@EnableCircuitBreaker进行组件的激活。
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
}
}
在刚刚的service业务对象上完善注解。
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
完善完成注解信息之后,开启服务进行测试。(超时之后直接进入服务降级的方法中进行处理)
Hystrix之服务降级订单侧fallback
客户端一侧的降级保护。
80订单微服务,也可以更好的保护自己,进行客户端的降级保护。
客户端的降级处理:修改配置文件开启enable属性
server:
port: 80
#没置feign客户端超时时间(openFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况iE常的情况下两端连接所用的时间
Readtimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
eureka:
client:
#表示是否将自己注册进eurekaServer 默认为true
register-with-eureka: false
service-url:
defaultZone: http://localhost:7001/eureka/
feign:
hystrix:
enabled: true
主启动类的修改和完善加上@EnableHystrix注解
package com.dzu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
实现
/**
* 超时
* @param id
* @return
*/
@RequestMapping("/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
})
public String paymentInfo_timeout(@PathVariable("id") Integer id){
String result=paymentHystrixService.paymentInfo_timeout(id);
return result;
}
public String paymentInfo_TimeOutHandler(@PathVariable("id")Integer id){
return "消费者80对方支付繁忙请稍后在尝试";
}
http://localhost/consumer/hystrix/timeout/3
此时客户端的服务降级完成对应的操作。
Hystrix全局服务降级
存在的问题:
1.每个业务方法对应一个兜底的方法,代码膨胀
2.统一和自定义分开
问题解决
1.@DefaultProperties(defaultFallback = “)
1: 1每个方法配置一个服务降级方法
1: N除了个别重要核心业务有专属,庀普通的可以通过@DefaultProperties(defaultFallback=“”) 统- -跳转到统- 处理结果页面
通用的和独享的各自分开,避免了代码膨胀,合理减少了代码量
Hystrix服务熔断
服务熔断理论
服务熔断
1.类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示
2.就是保险丝 服务的降级->进而熔断->恢复调用链路
熔断机制概述
熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务出错不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的响应信息。
当检测到该节点微服务调用响应正常后,恢复调用链路。
在Spring Cloud框架里,熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,当失败的调用到一定阈值,缺省是5秒内20次调用失败,就会启动熔断机制。熔断机制的注解是@HystrixCommand.
相关论文:
https://martinfowler.com/bliki/CircuitBreaker.html
服务熔断实际操作
简单期间只修改cloud-provider-hystrix-payment8001
在paymentSerivice上增加一个服务熔断的方法
注解的整体含义是在10秒中之内10次请求中有60%的失败率就会开启服务熔断。
如果传递的数是负数则会出错走降级之后的方法,如果传递的数是正数的话则会走正常返回随机流水号的方法
//======================服务熔断===============
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
@HystrixProperty(name="circuitBreaker.enabled",value = "true"),//是否开启断路器
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"),//请求次数
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//时间窗口期
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60"),//失败率达到多少后跳闸
})
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
if(id<0){
throw new RuntimeException("========id不能为负数");
}
String serialNumber = IdUtil.simpleUUID();
return Thread.currentThread().getName()+"调用成功,流水号:"+serialNumber;
}
public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
return "id不能为负数,请稍后再试";
}
在controller上添加上对应的控制器方法之后。完成之后重启8001服务提供者进行指定信息的测试。
//======================服务熔断===============
@GetMapping("/payment/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
String result = paymentService.paymentCircuitBreaker(id);
log.info("******result:"+result);
return result;
}
测试步骤及其服务熔断的含义:当get请求方式携带的参数是负数的时候,会抛出运行异常。此时按照之前测试的结果进行服务的降级,输出id不能为负数,请稍后再试。只会当10s中之内访问负数的次数达到6次以上的时候会进行熔断处理------正确的输出随机id号的服务被禁止就是我们说的服务的熔断,之后如果恢复正常的情况会恢复到最初正常的状态。
服务熔断总结
熔断类型
1.熔断打开
请求不再进行调用当前服务,内部设置时钟一般为MTTR (平均故障处理时间),当打开时长达到所设时钟则进入半熔断状态
2.熔断关闭
熔断关闭不会对服务进行熔断
3.熔断半开
部分请求根据规则调用当前服务,如果请求成功且符合规则则认为当前服务恢复正常,关闭熔断
官网断路器流程图
断路器在什么情况下开始起作用
断路器开启或者关闭的条件
1.当满足一定的阀值的时候(默认10秒内超过20个请求次数)
2.当失败率达到一定的时候(默认10秒内超过50%的请求失败)
3.到达以上阀值,断路器将会开启
4.当开启的时候,所有请求都不会进行转发
5.一段时间之后(默认是5秒) ,这个时候断路器是半开状态,会让其中一个请求进行转发。
6.如果成功,断路器会关闭,若失败,继续开启。重复4和5
断路器开启后
1:再有请求调用的时候,将不会调用主逻辑,而是直接调用降级fallback.通过断路器,实现了自动地发现错误并将降级逻辑切换为主逻辑,减少响应延迟的效果。
2:原来的主逻辑要如何恢复呢?
对于这一问题,hytrix也为我们实现了自动恢复功能。
当断路器打开,对主逻辑进行熔断之后,hystrix会启动一 个休眠时间窗,在这个时间窗内,降级逻辑是临时的成为主逻辑,当休眠时间窗到期,断路器将进入半开状态,释放- -次请求到原来的主逻辑上,如果此次请求正常返回,那么断路器将继续闭合,主逻辑恢复,如果这次请求依然有问题,断路器继续进入打开状态,休眠时间窗重新计时。
服务限流可以使用Ailibaba的Sentine进行实现
服务监控
服务监控hystrixDashboar
概述:
除了隔离依赖服务的调用以外,Hystrix还提供 了准实时的调用监控(Hystrix Dashboard) . Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。
仪表盘9001
1.新建cloud-consumer-hystrix-dashboard9001
2.POM
<?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>springcloud</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--web包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--通用包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
修改yaml配置文件
server:
port: 9001
编写主启动类进行测试出现监控的web界面。启动cloud-consumer-hystrix-dashboard9001该微服务后续将监控微服务8001。测试
地址:http://localhost:9001/hystrix
服务监控实战
通过9001仪表盘来监控8001这个微服务观察会出现怎样的监控页面。
修改8001微服务上的程序代码。
断路器演示(服务监控hystrixDashboard)
1.修改cloud-provider-hystrix-payment8001
注意:新版本Hystrix需要在主启动类MainAppHystrix8001中指定监控路径
在8001的主启动类上添加必要的配置信息
/**
* 此配置是为了服务监控而配置,与服务容错本身无观,springCloud 升级之后的坑
* ServletRegistrationBean因为springboot的默认路径不是/hystrix.stream
* 只要在自己的项目中配置上下面的servlet即可
*
* @return
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
package com.dzu.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
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.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableHystrixDashboard
@EnableCircuitBreaker
public class HystrixDashboardMain9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardMain9001.class,args);
}
/**
* 此配置是为了服务监控而配置,与服务容错本身无观,springCloud 升级之后的坑
* ServletRegistrationBean因为springboot的默认路径不是/hystrix.stream
* 只要在自己的项目中配置上下面的servlet即可
*
* @return
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
监控测试
启动1个eureka或者3个eureka集群均可
观察监控窗口
9001监控8001
地址:http://localhost:8001/hystrix.stream