一 搭建hystrix dashboard
新建module(Hystrix dashboard )
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>
主启动类开启Hystrix dashboard
application.yml配置Hystrix dashboard微服务监听端口
server:
port: 9001
启动Hystrix dashboard微服务,访问地址localhost:9001/hystrix
二 服务提供方集成hystrix
引入POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
主启动类开启Hystrix注解
@EnableHystrix
@EnableCircuitBreaker
controller开启Hystrix服务降级、熔断
@RestController
@RequestMapping("/ConsumerHystrix")
public class ConsumerHystrixController {
/**
* @HystrixCommand报异常后如何处理:
* 一旦调用服务方法失败并抛出了错误信息后,
* 会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
*
*/
@HystrixCommand(fallbackMethod = "fallback_zs1",commandProperties = {
//设置这个线程的超时时间是3s,3s内是正常的业务逻辑,超过3s调用fallbackMethod指定的方法进行处理
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
@GetMapping("/zs1")
public String zs1(String name){
String test= "ConsumerHystrix-"+"zs1-";
System.out.println(test);
return test+ name;
}
public String fallback_zs1(String name ){
String fallBack="ConsumerHystrix-"+"fallback_zs1-";
return fallBack+name;
}
//服务熔断
@HystrixCommand(fallbackMethod = "fallback_zs2",commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //是否开启断路器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "100"), //请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //时间窗口期
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸
})
@GetMapping("/zs2")
public String zs2(String name){
String test= "ConsumerHystrix-"+"zs2-";
return test+ name;
}
public String fallback_zs2(String name ){
String fallBack="ConsumerHystrix-"+"fallback_zs2-";
return fallBack+name;
}
}
三 Hystrix dashboard观察微服务接口响应情况
启动微服务,dashboard 监控应用微服务地址:localhost:8084/hystrix.stream
点击Monitior Stream 进入dashboard界面
请求应用微服务后台接口
Hystrix dashboard监控信息:
四 使用jmemter压测工具对接口进行压测
对接口1进行压测:
添加线程组: 1000个请求=100并发线程*10次循环
添加Http Request,测试接口zs1
运行jmemter,压测接口,观察Hystrix dashboard监控情况:
100并发,1000次请求情况下:
50并发,1000次请求:
对接口2进行压测:
100并发,1000次请求:
1000并发,10000次请求:
5000并发,20000次请求:此时熔断器仍没有打开,此时应用直接将请求拒绝