Dynamic-TP 动态线程池

Dynamic-TP 动态线程池

马草原 865 2022-04-16

Dynamic-TP 动态线程池技术

什么是Dynamic-TP

Dynamic-TP是基于配置中心轻量级动态线程池,内置监控告警功能,集成常用中间件线程池管理,可通过 SPI 自定义扩展实现。

官网: https://dynamictp.cn
GitHub:https://github.com/dromara/dynamic-tp

为什么需要动态线程池?

我们常用的线程池ThreadPoolExecutor有以下痛点:

  • 代码中创建了一个ThreadPoolExecutor,但是不知道那几个核心参数设置多少比较合适
  • 凭经验设置参数值,上线后发现需要调整又要改代码重新发布服务,非常麻烦
  • 线程池相对开发人员来说是个黑盒,运行情况不能及时感知到,直到出现问题

因此我们需要动态可监控线程池框架DynamicTP

关于稳定性?

美团技术团队已经线上大规模应用:
https://tech.meituan.com/2020/04/02/java-pooling-pratice-in-meituan.html

怎么用?

这里结合阿里Nacos使用
https://dynamictp.cn/guide/configcenter/nacos.html

1. 添加主pom依赖

<properties>
    <dynamictp.version>1.1.2</dynamictp.version>
</properties>

<dependencyManagement>
    <dependencies>
        <!-- dynamictp with nacos -->
        <dependency>
            <groupId>cn.dynamictp</groupId>
            <artifactId>dynamic-tp-spring-cloud-starter-nacos</artifactId>
            <version>${dynamictp.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

2. 在infrastructure模块去实际引入依赖

<dependencies>
    <!-- dynamictp with nacos -->
    <dependency>
        <groupId>cn.dynamictp</groupId>
        <artifactId>dynamic-tp-spring-cloud-starter-nacos</artifactId>
    </dependency>
</dependencies>

3. bootstrap.properties使用拓展配置

spring.application.name=[your-app-name]
server.port=8080

## DynamicTp @see https://dynamictp.cn/guide/configcenter/nacos-cloud.html
spring.cloud.nacos.config.extension-configs[0].dataId=${spring.application.name}-dtp.properties
spring.cloud.nacos.config.extension-configs[0].group=THREADPOOL
spring.cloud.nacos.config.extension-configs[0].refresh=true

代码

@SpringBootApplication
@EnableDynamicTp // 增加此注解
public class MicroServiceApplication {

    public static void main(String[] args) {
        DoggyApplication doggyApplication = new DoggyApplication();
        doggyApplication.run(args);
    }
}
@Service
public class WhereUseThreadPoolExecutor {

    @Qualifier("preplan-task-executor") // TODO your customize name
    @Autowired
    private final ThreadPoolExecutor preplanTaskExecutor;

}

配置中心配置:

## Dynamic-Tp common config
spring.dynamic.tp.enabled=true
spring.dynamic.tp.enabledBanner=true
spring.dynamic.tp.enabledCollect=true
spring.dynamic.tp.collectorTypes=micrometer
spring.dynamic.tp.monitorInterval=5
spring.dynamic.tp.platforms[0].platform=ding
spring.dynamic.tp.platforms[0].urlKey=[your dingding token]
spring.dynamic.tp.platforms[0].secret=[your dingding secret]
spring.dynamic.tp.platforms[0].receivers=[your phone num at by dingding] ## , 分隔的多个手机号
## Dynamic-Tp config of executors, e.g. @Qualifier("preplan-task-executor") ThreadPoolExecutor
spring.dynamic.tp.executors[0].threadPoolName=preplan-task-executor ## TODO your customize name
spring.dynamic.tp.executors[0].corePoolSize=20
spring.dynamic.tp.executors[0].maximumPoolSize=100
spring.dynamic.tp.executors[0].queueCapacity=1024
spring.dynamic.tp.executors[0].queueType=VariableLinkedBlockingQueue
spring.dynamic.tp.executors[0].rejectedHandlerType=AbortPolicy
spring.dynamic.tp.executors[0].keepAliveTime=60
spring.dynamic.tp.executors[0].allowCoreThreadTimeOut=true
spring.dynamic.tp.executors[0].threadNamePrefix=Preplan-Task-Exec ## TODO your customize threadNamePrefix
### Dynamic-Tp config of notifyItems, 已有默认的告警阈值,通常情况下也不需要自定义~ 故以下仅样例可不配
spring.dynamic.tp.executors[0].notifyItems[0].type=capacity
spring.dynamic.tp.executors[0].notifyItems[0].enabled=true
spring.dynamic.tp.executors[0].notifyItems[0].threshold=80
spring.dynamic.tp.executors[0].notifyItems[0].platforms[0]=ding
spring.dynamic.tp.executors[0].notifyItems[0].interval=120
spring.dynamic.tp.executors[0].notifyItems[1].type=reject
spring.dynamic.tp.executors[0].notifyItems[1].enabled=true
spring.dynamic.tp.executors[0].notifyItems[1].threshold=1
spring.dynamic.tp.executors[0].notifyItems[1].platforms[0]=ding

总结

Dynamic-TP可以无侵入的快速实现动态可监控的线程池,并且开源免费。
下面附上一张Dynamic-TP的技术架构图:

dynamictp