spring cloud hystrix
hystrix起服务器熔断作用。
具有那几个作用
- 服务降级
- 依赖隔离
- 服务熔断
- 监控等功能
服务降级
优先核心服务,非核心服务不可用或弱用
通过HystrixCommand注解指定
fallbackMethod(回退函数)具体实现降级逻辑
使用
1.先引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.在启动类上加上注解
@EnableCircuitBreaker
3.启动超时配置
//超时配置
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
4.在类上指定,熔断后的回调函数
@DefaultProperties(defaultFallback = "defaultFallback")
编写方法
private String defaultFallback() {
return "默认提示:太拥挤了, 请稍后再试~~";
}
5.或者设置熔断
@HystrixCommand(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"), //错误率 })
6.使用
hystrix-dashboard监控
1.引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2.配置文件配置一下
management:
context-path: /
3.直接访问localhost:8080/hystrix即可到达界面
填入,项目配置和项目名即可。
还没有评论,来说两句吧...