中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java動態線程池插件dynamic-tp怎么使用

發布時間:2023-03-02 11:54:03 來源:億速云 閱讀:162 作者:iii 欄目:開發技術

這篇文章主要介紹“Java動態線程池插件dynamic-tp怎么使用”,在日常操作中,相信很多人在Java動態線程池插件dynamic-tp怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java動態線程池插件dynamic-tp怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

    前言

    dynamic-tp是一個輕量級的動態線程池插件,它是一個基于配置中心的動態線程池,線程池的參數可以通過配置中心配置進行動態的修改,目前支持的配置中心有Apollo,NacosZookeeper,同時dynamic-tp支持線程池的監控和報警,具體特性如下:

    • 基于Spring框架,現只支持SpringBoot項目使用,輕量級,引入 starter 即可使用

    • 基于配置中心實現線程池參數動態調整,實時生效;集成主流配置中心,已支持 Nacos、Apollo,Zookeeper, 同時也提供 SPI 接口可自定義擴展實現

    • 內置通知報警功能,提供多種報警維度(配置變更通知、活性報警、容量閾值報警、拒絕策略觸發報警), 默認支持企業微信、釘釘報警,同時提供 SPI 接口可自定義擴展實現

    • 內置線程池指標采集功能,支持通過 MicroMeter、JsonLog 日志輸出、Endpoint 三種方式,可通過 SPI 接口自定義擴展實現

    • 集成管理常用第三方組件的線程池,已集成 SpringBoot 內置 WebServer(tomcat、undertow、jetty)的線程池管理

    • Builder 提供TTL包裝功能,生成的線程池支持上下文信息傳遞

    快速開始

    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

    @Configuration
    public 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怎么使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    水富县| 陆河县| 湟中县| 平罗县| 易门县| 合山市| 张家口市| 合阳县| 金山区| 遵义县| 玛沁县| 文水县| 师宗县| 麟游县| 阜城县| 新绛县| 宜春市| 海盐县| 茌平县| 南昌县| 东阳市| 通榆县| 肥城市| 蛟河市| 朝阳市| 古浪县| 黔江区| 东丰县| 库车县| 洛阳市| 东城区| 阳西县| 泰来县| 惠来县| 游戏| 永城市| 资阳市| 望都县| 鱼台县| 通辽市| 成武县|