hystrix停止更新,理念优秀。
分布式系统面临的问题:
对于复杂的分布式体系,有数十个依赖,依赖不可避免的错误。
服务会出现雪崩,高可用受到破坏。
Hystrix就是用于解决分布式系统延迟和容错的开源库。
保证在一个依赖出现问题,不会导致整体的服务失败,避免级联故障,以提高分布式系统的弹性
如果出现错误,向调用方抛出备选FallBack
停止更新进入维护
服务器出现问题时候,不让客户端持续等待,立刻返回一个友好的提示。fallback.
以下原因会导致服务降级.
当访问达到最大访问(简短可以理解为保险丝),直接拒绝访问,然后调用服务降级方法,返回友好提示。
降级-熔断-恢复
秒杀高并发的操作,严禁一窝蜂过来拥挤,有序进行。
Hystrix一般用于消费端,也就是调用端;
什么时候开启服务降级呢?
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>com.atjianyigroupId>
<artifactId>cloud-api-commonsartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
出现客户端相应缓慢,8001同一层次的其他接口被困死,因为在tomcat线程池当中默认只有10个线程。
客户端服务的核心就是调用远程服务。
使用Jemeter测试,压测timeout方法,发现OK方法也出现了问题,相应变慢,我们应该怎么来改善这一问题,这就引入了我们下面要说的Hystrix;
解决方案:
@HystrixCommand(fallbackMethod)
EnableCirutBreaker
@Override
@HystrixCommand(fallbackMethod = "timeoutHandler" ,commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000") //表示业务处理若超过 3s,则降级,调用方法。
})
public String timeout(String id ) {
int tmm = 3*1000;
try {
Thread.sleep(tmm);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "payment-timeout"+"current Thread "+Thread.currentThread().getId()+" cID="+id;
}
/**
* 降级回调方法
* @param id
* @return
*/
public String timeoutHandler(String id){
return "payment-timeout-handler"+"current Thread "+Thread.currentThread().getId()+" cID="+id;
}
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class HystrixPaymentApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixPaymentApplication.class,args);
}
}
测试结果
由于配置了hystrix容忍 3s 的超时,实际业务需要 5s 所以这里服务进行了降级处理。
现在我们把 timeout() 代码改成如下,在进行测试: 结果显示降级并且调用到了降级方法。
@Override
@HystrixCommand(fallbackMethod = "timeoutHandler" ,commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000") //表示业务处理若超过 3s,则降级,调用方法。
})
public String timeout(String id ) {
//直接报错,观察是否会进行降级
int t = 10/0;
int tmm = 5*1000;
try {
Thread.sleep(tmm);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "payment-timeout"+"current Thread "+Thread.currentThread().getId()+" cID="+id;
}
server:
port: 80
spring:
application:
name: cloud-hystrix-order
eureka:
client:
register-with-eureka: true #表示不向注册中心注册自己
fetch-registry: true #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://localhost:7001/eureka/
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
feign:
hystrix:
enabled: true
ribbon:
ReadTimeout: 5000
ConnectionTimeout: 5000
这里我们将主调用方法的超时容忍设为 1.5s, 而远程调用的长流程业务需要 3s完成,因此,测试一样会降级处理。
@GetMapping("consumer/payment/timeout/{id}")
@HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500") //设置容忍度为1500
})
public R timeout(@PathVariable("id")String id){
R ok = paymentClient.timeout(id);
return ok;
}
public R timeoutHandler(String id){
R<String> r = new R<>();
r.setData("consumer-timeoutHandler id="+id);
return r ;
}
然而,上述代码的写法带来的两个问题
针对上面出现的两个问题,我们改写为下面的方法。
全局服务降级配置,就是针对需要服务降级的进行通用配置 使用 @Defaultproperties()注解进行配置;
再改写消费端如下,配置全局服务降级;
通过上一个配置,我们解决了代码膨胀得问题,接着我们最后写出一套完整通用得服务调用与降级的Demo。
在测试的时候,将 服务端(被调用端)关闭,成功调用降级方法!
这样,我们就解决了代码膨胀和代码混乱的问题;
服务降级 -> 服务熔断 -> 链路恢复
断路器就是保险丝,熔断: 应对服务雪崩效应的链路保护机制,当检测节点微服务调用响应正常后,恢复调用链路。
熔断是> https://martinfowler.com/bliki/CircuitBreaker.html
熔断打开: 请求不再进行调用当前服务
熔断关闭: 熔断关闭,不再对服务进行熔断
熔断半开: 部分请求根据规则调用当前服务,如果请求成功且符合规则,则关闭熔断
设计断路器的三个重要参数; 快照时间窗口、请求总数阀值、错误百分比阀值
快照时间窗口: 统计请求和错误数据,默认为10s内
请求总数阀值: 在快照时间窗内,必须满足请求总数才有资格熔断,默认为20;
错误百分比法制: 当请求总数在快照时间内超过阀值,例如在10s内发生30次调用,有15次失败,则断路器打开。
当熔断开启后,直接调用fallback;
@HystrixCommand(fallbackMethod = "paymentCircuitBreakHandler",commandProperties = {
//开启熔断;
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
//请求次数至少10次
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
//在10s中
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
//错误率 60%
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "20")
})
public String paymentCirumentBreak(Integer id) {
if(id<0){
throw new RuntimeException("不能为负数");
}
//
String rid = UUID.randomUUID().toString().replace("-","");
return "调用成功: 流水号为: "+rid;
}
public String paymentCircuitBreakHandler(Integer id){
return "调用失败: === 降级处理"+id;
}
在Controller当中调用Service,并打开浏览器进行测试;
上述熔断过程就是 closed -> open ->halfOpen -> closed 的过程
Hystrix还提供了一个准实时的调用监控,以报表的方式或者图形化的方式展现给用户。SpringCloud提供了hystrixDashboard的整合,对监控内容转为可视化界面。
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
Server.port=9001
注意只要涉及到监控,就必须要添加 actuator 依赖
再主类当中添加如下代码
在hystrix监控栏目中输入被监控的地址: localhost:8001/hystrix.stream