•由于断路器功能太过强大,这里仅仅举一个栗子说明:高并发限流系统/限流组件应用.
•在断路器的配置限流策略中,execution.isolation.stragegy
有两种方式进行处理:THREAD 通过现程池隔离的策略,它会独立在一个线程上执行,并且他的并发量受线程池中的数量所限制。SEMAPHONE 它则实现在调用的线程上,通过信号量的方式进行隔离,这种则是类似java中的限流了,受到信号量计数器的限制所约束。
一、线程的策略方式:
commandKey-->每一个Hystrix的注解都应该对应一个commandKey,做为唯一标示;
threadPoolKey-->使用线程测试,需要定义一个线程的唯一标示;
threadPoolProperties-->属性配置,一贯的Hystrix风格,即@HystrixP[roperty的Key和Value形式;
coreSize=10代表设定并发最大10个线程,如果超出会放入Queue中;
maxQueueSize=50,maxQueueSize是固定的值,一旦设定不可(动态)更改,其底层实现是有界队列ArrayBlockingQueue;
queueSizeRejectionThreshold=30,此属性为阀值,约束当maxQueueSize中有30个等待时,启动降级,调用fallbackMethod;(queueSizeRejectionThreshold提供了动态调整功能,可以将maxQueueSize设置足够的,然后由queueSizeRejectionThreshold动态调整)
fallbackMethod='threadFailback'--->降级策略
模拟:外部请求调用consumer,然后consumer启动HelloService的限流方法,即在消费者端加限流,图标示位置为UpStream上游服务限流模拟(实际工作中限流组件一般都放置在靠近前端入口处);
package com.cc.springcloud.api;
@RestController
public class ProviderController {
//用于限流的方法
@RequestMapping(value="/thread")
public String thread() throws Exception {
System.out.println("provider-1:------> thread");
//模拟访问耗时
Thread.sleep(RandomUtils.nextInt(1500, 2000));
return "provider-1";
}
}
-------------------------------------------------------------------------------------------------------------
package com.cc.springcloud.service;
import java.util.List;
import java.util.concurrent.Future;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.cc.springcloud.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
/**
* Hystrix实现限流机制:使用Thread方式:线程池的方式进行隔离限流策略
* 注意:配置属性的Key要和方法名一致,实现见名知意,例如方法名称为getUser,
* 那么commandKey要设置为getUserKey1,threadPoolKey要设置为getUserthreadPoolKey1;
*/
//动态调整阈值
//@Value("${consumer.queuesize}")
//private String queueSizeRT;
@HystrixCommand(
commandKey = "threadKey1",
threadPoolKey = "threadPoolKey1",
threadPoolProperties = {
//核心线程数,最大的并发线程数量
@HystrixProperty(name="coreSize",value="10"),
//底层机制使用有界队列,有界队列必须要指定队列的容量上限,当一旦指定容量上限后就不能进行动态调整
@HystrixProperty(name="maxQueueSize",value="2000"),
//使用THREAD方式:关键的服务降级指标,设置拒绝的阈值(可以动态调整),此属性为真正控制限流策略的阈值(超出启动降级策略)
//@HystrixProperty(name="queueSizeRejectionThreshold",value="${queueSizeRT}")
@HystrixProperty(name="queueSizeRejectionThreshold",value="30")
},
//降级方法
fallbackMethod = "threadFailback"
)
public String thread() {
String ret = restTemplate.getForObject("http://provider-service/thread", String.class);
System.out.println("输出调用结果:"+ret);
return ret;
}
//方法必须带有返回值,否则出现异常
public String threadFailback() {
System.out.println("-------thread限流,降级策略!-----------------");
return "-------thread限流,降级策略!-----------------";
}
}
--------------------------------------------------------------------------------------------------------------
package com.cc.springcloud.api;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cc.springcloud.entity.User;
import com.cc.springcloud.service.HelloService;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
@RestController
public class ConsumerController {
@Autowired
private HelloService helloService;
//访问限流接口
@RequestMapping(value="/hystrix-thread")
public String thread() throws Exception {
return helloService.thread();
}
}
使用JMeter模拟压力测试:
consumer控制台在一定压力下出现:调用降级方法。