文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java动态线程池插件dynamic-tp怎么使用

2023-07-05 08:24

关注

这篇文章主要介绍“Java动态线程池插件dynamic-tp怎么使用”,在日常操作中,相信很多人在Java动态线程池插件dynamic-tp怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java动态线程池插件dynamic-tp怎么使用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

前言

dynamic-tp是一个轻量级的动态线程池插件,它是一个基于配置中心的动态线程池,线程池的参数可以通过配置中心配置进行动态的修改,目前支持的配置中心有Apollo,NacosZookeeper,同时dynamic-tp支持线程池的监控和报警,具体特性如下:

快速开始

pom依赖

dynamic-tp-spring-boot-starter-zookeeper是集成dynamic-tp的starter,这里引入了micrometer-registry-prometheusspring-boot-starter-actuator用于对接Prometheus

    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-logging</artifactId>        </dependency>        <dependency>            <groupId>io.github.lyh300</groupId>            <artifactId>dynamic-tp-spring-boot-starter-zookeeper</artifactId>            <version>1.0.2</version>        </dependency>        <dependency>            <groupId>io.micrometer</groupId>            <artifactId>micrometer-registry-prometheus</artifactId>            <version>1.6.4</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>    </dependencies>

YML配置

server:
  port: 8888

spring:
  application:
    name: dynamic-tp-zookeeper-demo
  # 下面是接入zk配置中心的配置  
  dynamic:
    tp:
      config-type: properties # zookeeper只支持properties配置
      zookeeper:
        config-version: 1.0.0 # 配置版本号
        zk-connect-str: 127.0.0.1:2181 # zk配置中心,如果是集群用逗号分开
        root-node: /configserver/userproject # 项目节点
        node: dynamic-tp-zookeeper-demo # 配置文件节点
# 对接prometheus
management:
  metrics:
    tags:
      application: ${spring.application.name}
  endpoints:
    web:
      exposure:
        include: '*'

配置中心dynamic-tp-zookeeper-demo配置文件配置

注:Zookeeper配置中心只支持properties类型配置,配置示例如下:

spring.dynamic.tp.executors部分是对线程池的配置,该配置是数组类型,可以定义多个线程池

spring.dynamic.tp.executors[1].notifyItems部分配置是对线程池报警平台的配置,可以配置多个报警平台

# 开启动态线程池
spring.dynamic.tp.enabled=true
# 打印动态线程池banner
spring.dynamic.tp.enabledBanner=true
# 开启线程池监控指标收集
spring.dynamic.tp.enabledCollect=true
# 线程池监控指标收集类型 logging-日志文件 micrometer-采集到第三方平台 
spring.dynamic.tp.collectorType=logging
# 采集监控数据时间间隔 5s
spring.dynamic.tp.monitorInterval=5
spring.dynamic.tp.executors[0].threadPoolName=dynamic-tp-test-1
spring.dynamic.tp.executors[0].corePoolSize=50
spring.dynamic.tp.executors[0].maximumPoolSize=50
spring.dynamic.tp.executors[0].queueCapacity=3000
spring.dynamic.tp.executors[0].queueType=VariableLinkedBlockingQueue
spring.dynamic.tp.executors[0].rejectedHandlerType=CallerRunsPolicy
spring.dynamic.tp.executors[0].keepAliveTime=50
spring.dynamic.tp.executors[0].allowCoreThreadTimeOut=false
spring.dynamic.tp.executors[0].threadNamePrefix=test1
spring.dynamic.tp.executors[0].notifyItems[0].type=capacity
spring.dynamic.tp.executors[0].notifyItems[0].enabled=false
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].platforms[1]=wechat
spring.dynamic.tp.executors[0].notifyItems[0].interval=120
spring.dynamic.tp.executors[0].notifyItems[1].type=change
spring.dynamic.tp.executors[0].notifyItems[1].enabled=false
spring.dynamic.tp.executors[0].notifyItems[2].type=liveness
spring.dynamic.tp.executors[0].notifyItems[2].enabled=false
spring.dynamic.tp.executors[0].notifyItems[2].threshold=80
spring.dynamic.tp.executors[0].notifyItems[3].type=reject
spring.dynamic.tp.executors[0].notifyItems[3].enabled=false
spring.dynamic.tp.executors[0].notifyItems[3].threshold=1
spring.dynamic.tp.executors[1].threadPoolName=dynamic-tp-test-2
spring.dynamic.tp.executors[1].corePoolSize=20
spring.dynamic.tp.executors[1].maximumPoolSize=30
spring.dynamic.tp.executors[1].queueCapacity=1000
spring.dynamic.tp.executors[1].queueType=VariableLinkedBlockingQueue
spring.dynamic.tp.executors[1].rejectedHandlerType=CallerRunsPolicy
spring.dynamic.tp.executors[1].keepAliveTime=50
spring.dynamic.tp.executors[1].allowCoreThreadTimeOut=false
spring.dynamic.tp.executors[1].threadNamePrefix=test2
spring.dynamic.tp.executors[1].notifyItems[0].type=capacity
spring.dynamic.tp.executors[1].notifyItems[0].enabled=false
spring.dynamic.tp.executors[1].notifyItems[0].threshold=80
spring.dynamic.tp.executors[1].notifyItems[0].platforms[0]=ding
spring.dynamic.tp.executors[1].notifyItems[0].platforms[1]=wechat
spring.dynamic.tp.executors[1].notifyItems[0].interval=120
spring.dynamic.tp.executors[1].notifyItems[1].type=change
spring.dynamic.tp.executors[1].notifyItems[1].enabled=false
spring.dynamic.tp.executors[1].notifyItems[2].type=liveness
spring.dynamic.tp.executors[1].notifyItems[2].enabled=false
spring.dynamic.tp.executors[1].notifyItems[2].threshold=80
spring.dynamic.tp.executors[1].notifyItems[3].type=reject
spring.dynamic.tp.executors[1].notifyItems[3].enabled=false
spring.dynamic.tp.executors[1].notifyItems[3].threshold=1

创建线程池

创建线程池,会注册到dynamic-tp

@Configurationpublic class ThreadPoolConfiguration {    @Bean    public DtpExecutor dtpExecutor() {        return ThreadPoolCreator.createDynamicFast("dynamic-tp-test-1");    }    @Bean    public ThreadPoolExecutor threadPoolExecutor() {        return ThreadPoolBuilder.newBuilder()                .threadPoolName("dynamic-tp-test-2")                .corePoolSize(10)                .maximumPoolSize(15)                .keepAliveTime(15000)                .timeUnit(TimeUnit.MILLISECONDS)                .workQueue(QueueTypeEnum.SYNCHRONOUS_QUEUE.getName(), null, false)                .buildDynamic();    }}

使用

初次使用不注意官网文档的话可能会通过@Autowired的方式使用,目前版本这么使用是不正确的,因为当读取配置文件后,配置文件生成的线程池对象就会把@Bean创建的线程池覆盖了,所以目前版本只能根据名字从DtpRegistry获取线程池对象了,笔者也和项目作者沟通过,项目作者也觉得通过@Autowired方式更好一些,后面版本也会优化。

@Slf4j@RestController@SuppressWarnings("all")public class TestController {    @GetMapping("/dtp-zookeeper-example/test")    public String test() {        new Thread(() -> {            try {                task();            } catch (InterruptedException e) {                e.printStackTrace();            }        }).start();        return "success";    }    public void task() throws InterruptedException {        DtpExecutor dtpExecutor1 = DtpRegistry.getExecutor("dynamic-tp-test-1");        DtpExecutor dtpExecutor2 = DtpRegistry.getExecutor("dynamic-tp-test-2");        for (int i = 0; i < 100; i++) {            Thread.sleep(100);            dtpExecutor1.execute(() -> {                log.info("i am dynamic-tp-test-1 task");            });            dtpExecutor2.execute(() -> {                log.info("i am dynamic-tp-test-2 task");            });        }    }}

监控数据和第三方平台对接

logging方式

spring.dynamic.tp.collectorType=logging,logging方式的监控数据,采集日志文件中,文件的位置可以通过spring.dynamic.tp.logPath配置,默认 ${user.home}/logs,日志内容如下:

2022-03-07 13:48:31.585 INFO [dtp-monitor1:D.M.LOG] {"activeCount":0,"queueSize":0,"largestPoolSize":0,"poolSize":0,"rejectHandlerName":"CallerRunsPolicy","queueCapacity":0,"fair":false,"rejectCount":0,"waitTaskCount":0,"taskCount":0,"queueRemainingCapacity":0,"corePoolSize":40,"queueType":"SynchronousQueue","completedTaskCount":0,"dtpName":"dynamic-tp-test-2","maximumPoolSize":55}
2022-03-07 13:48:31.585 INFO [dtp-monitor1:D.M.LOG] {"activeCount":0,"queueSize":0,"largestPoolSize":0,"poolSize":0,"queueCapacity":2147483647,"fair":false,"rejectCount":0,"waitTaskCount":0,"taskCount":0,"queueRemainingCapacity":2147483647,"corePoolSize":10,"queueType":"TaskQueue","completedTaskCount":0,"dtpName":"tomcatWebServerTp","maximumPoolSize":200}
2022-03-07 13:48:36.590 INFO [dtp-monitor1:D.M.LOG] {"activeCount":0,"queueSize":0,"largestPoolSize":0,"poolSize":0,"rejectHandlerName":"CallerRunsPolicy","queueCapacity":1024,"fair":false,"rejectCount":0,"waitTaskCount":0,"taskCount":0,"queueRemainingCapacity":1024,"corePoolSize":50,"queueType":"VariableLinkedBlockingQueue","completedTaskCount":0,"dtpName":"dynamic-tp-test-1","maximumPoolSize":50}
2022-03-07 13:48:36.590 INFO [dtp-monitor1:D.M.LOG] {"activeCount":0,"queueSize":0,"largestPoolSize":0,"poolSize":0,"rejectHandlerName":"CallerRunsPolicy","queueCapacity":0,"fair":false,"rejectCount":0,"waitTaskCount":0,"taskCount":0,"queueRemainingCapacity":0,"corePoolSize":40,"queueType":"SynchronousQueue","completedTaskCount":0,"dtpName":"dynamic-tp-test-2","maximumPoolSize":55}
2022-03-07 13:48:36.590 INFO [dtp-monitor1:D.M.LOG] {"activeCount":0,"queueSize":0,"largestPoolSize":0,"poolSize":0,"queueCapacity":2147483647,"fair":false,"rejectCount":0,"waitTaskCount":0,"taskCount":0,"queueRemainingCapacity":2147483647,"corePoolSize":10,"queueType":"TaskQueue","completedTaskCount":0,"dtpName":"tomcatWebServerTp","maximumPoolSize":200}

MicroMeter方式

spring.dynamic.tp.collectorType=micrometer 采集数据到第三方平台,这里使用的是Prometheus。可视化展示需要在Grafana平台配置,数据源选择Prometheus,然后创建Dashboard,先要在Dashboard配置动态的参数Variables,如下图:

Java动态线程池插件dynamic-tp怎么使用

配置完参数后,我们创建Panel,可以创建多个Panel,然后配置Panel的数据查询,报警显示等等,如下图:

Java动态线程池插件dynamic-tp怎么使用

最终的Dashboard如下图:

Java动态线程池插件dynamic-tp怎么使用

到此,关于“Java动态线程池插件dynamic-tp怎么使用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯