目录
服务熔断:(break)
类似于保险丝,达到最大服务访问后,直接拒绝访问,直到失败率低于指定值,关闭保险丝
以下的配置在 HystrixCommandProperties.class找
@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") int id){
if (id<0){
throw new RuntimeException("id无法为负数");
}
String s = IdUtil.simpleUUID();
return Thread.currentThread().getName()+"请求成功:"+"______"+s;
}
//服务熔断
public String paymentCircuitBreaker_fallBack(@PathVariable("id") int id){
return id+"请求失败,请稍后重试";
}
图形化界面
pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
</dependency>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
yml
server: port: 9001
主启动类:加注解
@EnableHystrixDashboard
被监控的主启动类
@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:图形化端口/hystrix
http://localhost:被监视的端口/hystrix.stream