Spring Cloud全解析:熔断之Hystrix隔离策略

2024-09-08  本文已影响0人  墨线宝

Hystrix隔离策略

Hystrix通过隔离限制依赖的并发量和阻塞扩散,Hystrix的隔离策略有两种:

线程隔离

线程隔离主要是指线程池隔离,把请求分类交给不同的线程池处理。当一种业务请求处理发生问题时,不会将故障扩散到其他线程池,从而保证其他服务可用

// 服务分组  配置全局唯一标识服务分组的名称,在进行监控时,相同分组的服务会聚合在一起
protected final HystrixCommandGroupKey groupKey;
// 服务标识,配置全局唯一标识服务的名称,默认是类名
protected HystrixCommandKey commandKey;
// 线程池名称,相同线程池名称的线程池是同一个,如果不配置,默认是分组名,此时一个group共用一个线程池
protected HystrixThreadPoolKey threadPoolKey;
// 命令属性配置
protected HystrixCommandProperties.Setter commandPropertiesDefaults;
// 线程池配置
protected HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults;


public TestCommand(){
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("testGroup"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("command"))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("testGroupPool"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(10).withMaxQueueSize(10)));
    }

信号量隔离

信号量隔离限制总的并发数,服务使用主线程进行同步调用,没有线程池,如果只是想限制某个服务的总并发调用量可以使用信号量来实现

HystrixCommandProperties.Setter().withExecutionIsolationStrategy(
        HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE
).withExecutionIsolationSemaphoreMaxConcurrentRequests(200); // 限制200并发

https://zhhll.icu/2021/框架/微服务/springcloud/熔断/Hystrix断路器/5.Hystrix隔离策略/

上一篇 下一篇

猜你喜欢

热点阅读