项目作者: EZLippi

项目描述 :
从Hystrix核心代码中提取出来的线程池隔离的代码,可以非常方便的在Web应用中实现线程池隔离
高级语言: Java
项目地址: git://github.com/EZLippi/isolation-threadpool.git
创建时间: 2018-06-06T14:40:57Z
项目社区:https://github.com/EZLippi/isolation-threadpool

开源协议:Apache License 2.0

下载


isolation-threadpool

从Hystrix核心代码中提取出来的线程池隔离的代码,可以非常方便的在Web应用中实现线程池隔离

使用场景

我们的应用在使用Jetty服务时,多个HTTP服务会共享同一个线程池,当其中一个服务依赖的其他服务响应慢时造成服务响应时间增加,大多数线程阻塞等待数据响应返回,新的请求无法建立SSL连接,导致整个Jetty线程池都被
该服务占用,最终拖垮了整个Jetty,因此我们有必要能把不同HTTP服务隔离到不同的线程池中,即使其中某个HTTP服务的线程池满了也不会影响其他的服务

如何使用

  1. 通过Builder创建命令参数,可以配置任务执行的时间,超时后执行线程会被中断
  1. IsolationCommandProperties.Builder builder1 = new IsolationCommandProperties.Builder()
  2. .withExecutionTimeoutEnabled(true)
  3. .withExecutionTimeoutInMilliseconds(1000)
  4. .withExecutionIsolationThreadInterruptOnTimeout(true);
  1. 创建线程池的核心参数
  1. IsolationThreadPoolProperties.Builder threadPoolBuilder1 = new IsolationThreadPoolProperties.Builder()
  2. .withCoreSize(1)
  3. .withMaximumSize(1)
  4. .withKeepAliveTimeMinutes(2)
  5. .withMaxQueueSize(10);
  1. 创建一个IsolationCommand,一个IsolationCommand对应一个任务的执行,比如第三方服务的调用,需要指定commandName和threadPoolKey,不同的threadPoolKey会创建不同线程池
  1. IsolationCommand<String> command1 = new IsolationCommand<String>("command1", "pool1",
  2. builder1, threadPoolBuilder1, new DefaultExecutionHook()) {
  3. @Override
  4. protected String run() throws Exception {
  5. return "hello";
  6. }
  7. };
  8. IsolationCommand<String> command2 = new IsolationCommand<String>("command2", "pool2",
  9. builder1, threadPoolBuilder1, new DefaultExecutionHook()) {
  10. @Override
  11. protected String run() throws Exception {
  12. return "isolation";
  13. }
  14. };
  15. //将任务提交给线程池处理,queue()方法返回一个Future,调用者可以通过轮询或者阻塞获取返回值
  16. command1.queue();
  17. //调用线程同步获取返回值
  18. command2.execute();