当前位置:网站首页>Quarantine and downgrade

Quarantine and downgrade

2022-08-01 22:37:00 Online dating cheated 2,800

隔离和降级:

Although current limiting can avoid service failures caused by high concurrency,但服务还会因为其他原因而故障.而要将这些故障控制在一定范围,避免雪崩,就要靠线程隔离(cabin mode)和熔断降级手段了.

Regardless of thread isolation or fuse degradation,都是对客户端(调用方)的保护.

Feign整合sentinel:

SpringCloud中,微服务调用都是通过Feign来实现的,因此做客户端保护必须整合Feign和Sentinel.

1.Modifications need to be done to protect the clientxxService的application.yml文件,开启Feign的Sentinel功能.

server:
  port: 8088
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false
    username: root
    password: [email protected]1997^05*14
    driver-class-name: com.mysql.jdbc.Driver
  application:
    name: orderservice
  cloud:
    nacos:
      server-addr: localhost:8848 # nacos服务地址
    sentinel:
      transport:
        dashboard: localhost:8080 # sentinel控制台地址
feign:
httpclient:
enabled: true # 支持HttpClient的开关
max-connections: 200 # 最大连接数
max-connections-per-route: 50 # 单个路径的最大连接数
sentinel:
enabled: true # 开启feign对sentinel的支持
 

2.给FeignClientWrite downgrade rules after failure

①方法一:FallBackClass,无法对远程调用的异常做处理

②方法二:FallbackFactory,可以对远程调用的异常做处理

 

步骤一:在feign-apiImplementation-defined classes in the project,实现FallbackFactory

@Slf4j
public class UserClientFallbackFactory implements FallbackFactory<UserClient> {
    @Override
    public UserClient create(Throwable throwable) {
        return new UserClient() {
            @Override
            public User findById(Long id) {
                log.error("查询用户异常", throwable);
                return new User();
            }
        };
    }
}

步骤二:在feign-api项目中的DefaultFeignConfiguration将UserClientFallbackFactory注册为一个bean

public class DefaultFeignConfiguration {
@Bean
public Logger.Level logLevel(){
return Logger.Level.BASIC;
}

@Bean
public UserClientFallbackFactory userClientFallbackFactory(){
return new UserClientFallbackFactory();
}
}

步骤三:在feign-api项目中的UserClient接口中使用UserClientFallbackFactory

@FeignClient(value = "userservice", fallbackFactory = UserClientFallbackFactory.class)
public interface UserClient {

    @GetMapping("/user/{id}")
    User findById(@PathVariable("id") Long id);
}

Sentinel支持的雪崩解决方案:

线程隔离(舱壁模式)

熔断降级

Feign整合Sentinel的过程:

在application.yml中配置 feign.sentinel.enable=true

给FeignClient编写FallbackFactory并注册为bean

将FallbackFactory配置到FeignClient

 

线程隔离

There are two ways to implement thread isolation:

·线程池隔离

在添加限流规则时,Two threshold modes can be selected:

 

QPS:就是每秒的请求数

线程数:是该资源能使用的tomcat线程数的最大值.也就是通过限制线程数量,实现舱壁模式; 

·信号量隔离(Sentinel默认采用)

两者的对比:

 

 优点缺点场景
线程池隔离

支持主动超时

支持异步调用

线程的额外开销比较大低扇出
信号量隔离

轻量级

无额外开销

不支持主动超时

不支持异步调用

高频调用

高扇出

 

熔断降级:

Fusing downgrade is an important means to deal with the avalanche problem.The idea is to count the abnormal proportion of service calls by the circuit breaker、慢请求比例,如果超出阈值则会熔断该服务.即拦截访问该服务的一切请求;而当服务恢复时,断路器会放行访问该服务的请求.

断路器的三种状态:closed,open,half-Open

 

熔断策略:

断路器熔断策略有三种:慢调用、异常比例、异常数

·慢调用:业务响应时长(RT)大于指定时长的请求认定为慢调用请求.在指定时间内,如果请求数量超过设定的最小数量,慢调用比例大于设定的阈值,则触发熔断.例如:

 

 解读:RT超过500ms的调用是慢调用,统计最近10000ms内的请求,If the request volume exceeds ten times,并且慢调用比例不低于0.5,则触发熔断,熔断时长为5s.然后进入half-open状态,放行一次请求做测试.

 

原网站

版权声明
本文为[Online dating cheated 2,800]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/213/202208012222157521.html