springcloud?nacos動態(tài)線程池Dynamic?tp配置接入實戰(zhàn)詳解
一、接入步驟
最近業(yè)務上需要把項目組幾個微服務接入動態(tài)線程池框架Dynamic-tp監(jiān)控,同時支持webhook機器人接受變更通知、容量報警、活性報警、拒絕報警、任務超時報警等通知,方便項目組同事企業(yè)微信群實時監(jiān)控線程池狀態(tài),動態(tài)調整線程參數(shù)。
接手開始上手,一看網(wǎng)上教程和demo很多啊,心中竊喜,一頓ctrl+c+v大發(fā),卻發(fā)現(xiàn)告警不通知,配置線程池不生效,數(shù)著承諾的交付時間,開始發(fā)慌;幾經(jīng)周折,才定,斯文于記。
1、引入POM文件
spring-cloud場景下的nacos應用接入用此依賴:
<dependency> <groupId>cn.dynamictp</groupId> <artifactId>dynamic-tp-spring-cloud-starter-nacos</artifactId> <version>1.0.8</version> </dependency>
非spring-cloud 場景下的nacos應用接入用此依賴:
<dependency> <groupId>cn.dynamictp</groupId> <artifactId>dynamic-tp-spring-boot-starter-nacos</artifactId> <version>1.0.8</version> </dependency>
2、啟動類加 @EnableDynamicTp 注解
3、配置中心配置線程池yml實例
spring: dynamic: tp: enabled: true enabledBanner: true # 是否開啟banner打印,默認true enabledCollect: true # 是否開啟監(jiān)控指標采集,默認false collectorTypes: micrometer,logging # 監(jiān)控數(shù)據(jù)采集器類型(logging | micrometer | internal_logging),默認micrometer logPath: /home/logs # 監(jiān)控日志數(shù)據(jù)路徑,默認 ${user.home}/logs,采集類型非logging不用配置 monitorInterval: 5 # 監(jiān)控時間間隔(報警判斷、指標采集),默認5s nacos: # nacos配置,不配置有默認值(規(guī)則appname-dev.yml這樣),cloud應用不需要配置 dataId: dynamic-tp-demo-dev.yml group: DEFAULT_GROUP apollo: # apollo配置,不配置默認拿apollo配置第一個namespace namespace: dynamic-tp-demo-dev.yml configType: yml # 配置文件類型,非cloud nacos 和 apollo需配置,其他不用配 platforms: # 通知報警平臺配置 - platform: wechat urlKey: 3a700-127-4bd-a798-c53d8b69c # 替換 receivers: test1,test2 # 接受人企微名稱 - platform: ding urlKey: f80dad441fcd655438f4a08dcd6a # 替換 secret: SECb5441fa6f375d5b9d21 # 替換,非sign模式可以沒有此值 receivers: 18888888888 # 釘釘賬號手機號 - platform: lark urlKey: 0d944ae7-b24a-40 # 替換 receivers: test1,test2 # 接受人飛書名稱/openid tomcatTp: # tomcat webserver線程池配置 corePoolSize: 100 maximumPoolSize: 200 keepAliveTime: 60 jettyTp: # jetty weberver線程池配置 corePoolSize: 100 maximumPoolSize: 200 undertowTp: # undertow webserver線程池配置 corePoolSize: 100 maximumPoolSize: 200 keepAliveTime: 60 hystrixTp: # hystrix 線程池配置 - threadPoolName: hystrix1 corePoolSize: 100 maximumPoolSize: 200 keepAliveTime: 60 dubboTp: # dubbo 線程池配置 - threadPoolName: dubboTp#20880 # 名稱規(guī)則:dubboTp + "#" + 協(xié)議端口 threadPoolAliasName: 測試線程池 # dubbo線程池 corePoolSize: 100 maximumPoolSize: 200 keepAliveTime: 60 notifyItems: # 報警項,不配置自動會按默認值配置(變更通知、容量報警、活性報警) - type: capacity # 報警項類型,查看源碼 NotifyTypeEnum枚舉類 enabled: true threshold: 80 # 報警閾值 platforms: [ding,wechat] # 可選配置,不配置默認拿上層platforms配置 interval: 120 # 報警間隔(單位:s) rocketMqTp: # rocketmq 線程池配置 - threadPoolName: group1#topic1 # 名稱規(guī)則:group + "#" + topic corePoolSize: 200 maximumPoolSize: 200 keepAliveTime: 60 executors: # 動態(tài)線程池配置 - threadPoolName: dtpExecutor1 threadPoolAliasName: 測試線程池 # 線程池別名 executorType: common # 線程池類型common、eager:適用于io密集型 corePoolSize: 6 maximumPoolSize: 8 queueCapacity: 200 queueType: VariableLinkedBlockingQueue # 任務隊列,查看源碼QueueTypeEnum枚舉類 rejectedHandlerType: CallerRunsPolicy # 拒絕策略,查看RejectedTypeEnum枚舉類 keepAliveTime: 50 allowCoreThreadTimeOut: false # 是否允許核心線程池超時 threadNamePrefix: test # 線程名前綴 waitForTasksToCompleteOnShutdown: false # 參考spring線程池設計,優(yōu)雅關閉線程池 awaitTerminationSeconds: 5 # 單位(s) preStartAllCoreThreads: false # 是否預熱所有核心線程,默認false runTimeout: 200 # 任務執(zhí)行超時閾值,目前只做告警用,單位(ms) queueTimeout: 100 # 任務在隊列等待超時閾值,目前只做告警用,單位(ms) taskWrapperNames: ["ttl"] # 任務包裝器名稱,集成TaskWrapper接口 notifyItems: # 報警項,不配置自動會按默認值配置(變更通知、容量報警、活性報警、拒絕報警、任務超時報警) - type: capacity # 報警項類型,查看源碼 NotifyTypeEnum枚舉類 enabled: true threshold: 80 # 報警閾值 platforms: [ding,wechat] # 可選配置,不配置默認拿上層platforms配置的所以平臺 interval: 120 # 報警間隔(單位:s) - type: change enabled: true - type: liveness enabled: true threshold: 80 - type: reject enabled: true threshold: 1 - type: run_timeout enabled: true threshold: 1 - type: queue_timeout enabled: true threshold: 1
服務啟動時會根據(jù)配置中心的配置[dtpExecutor1]動態(tài)注冊到Spring容器中。
4、配置線程池使用
@Resource或@Autowired進行依賴注入,或通過 DtpRegistry.getDtpExecutor("name")獲取。
@Resource private ThreadPoolExecutor dtpExecutor1;
DtpExecutor dtpExecutor = DtpRegistry.getDtpExecutor("dtpExecutor1"); dtpExecutor.execute(() -> System.out.println("test"));
以上4步,順利話可以接入使用了:
| __ \ (_) |__ __| | | | |_ _ _ __ __ _ _ __ ___ _ ___| |_ __ | | | | | | | '_ \ / _` | '_ ` _ | |/ __| | '_ \ | |__| | |_| | | | | (_| | | | | | | | (__| | |_) | |_____/ __, |_| |_|__,_|_| |_| |_|_|___|_| .__/ __/ | | | |___/ |_| :: Dynamic Thread Pool :: DynamicTp register dtpExecutor, source: beanPostProcessor, executor: DtpMainPropWrapper(dtpName=dynamic-tp-test-1, corePoolSize=6, maxPoolSize=8, keepAliveTime=50, queueType=VariableLinkedBlockingQueue, queueCapacity=200, rejectType=RejectedCountableCallerRunsPolicy, allowCoreThreadTimeOut=false) DtpRegistry initialization end, remote dtpExecutors: [dtpExecutor1, dtpExecutor2], local dtpExecutors: [ioIntensiveExecutor], local commonExecutors: [commonExecutor]
啟動日志出現(xiàn), remote dtpExecutors便是配置注冊的線程池了,那么恭喜。
當然我們也可以在程序中定義線程池代替配置文件,但是官方不推薦,官方推薦的配置文件配置。普通 JUC線程池想要被監(jiān)控,可以@Bean定義時加 @DynamicTp 注解。
* 1、 (ThreadPoolExecutor) Executors.newFixedThreadPool(1)加@DynamicTp("commonExecutor") * 2、ThreadPoolCreator.createDynamicFast("dynamic-tp-test-1"); * 3、ThreadPoolBuilder.newBuilder()
二、避坑指南
1、yaml配置單獨文件,不同的nacos配置可能會有差異,以nacos 2.0.2為例: 配置文件添加后綴.yml或者.yaml
2、阻塞隊列只有VariableLinkedBlockingQueue類型可以修改capacity,該類型功能和 LinkedBlockingQueue相似,只是capacity不是final類型,可以修改。
3、配置企微機器人地址KEY
platform: wechat urlKey: 3a700-127-4bd-a798-c53d8b69c # 機器人地址后的key receivers: test1 # 指定接收人企微名稱,默認@所有人
機器人配置參考企微webhook
4、重要類DtpBeanDefinitionRegistrar配置轉換
以上就是springcloud nacos動態(tài)線程池Dynamic tp配置接入實戰(zhàn)詳解的詳細內容,更多關于springcloud nacos配置接入Dynamic tp的資料請關注腳本之家其它相關文章!
相關文章
Spring Security基于json登錄實現(xiàn)過程詳解
這篇文章主要介紹了Spring Security基于json登錄實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08SpringBoot+Shiro學習之密碼加密和登錄失敗次數(shù)限制示例
本篇文章主要介紹了SpringBoot+Shiro學習之密碼加密和登錄失敗次數(shù)限制示例,可以限制登陸次數(shù),有興趣的同學可以了解一下。2017-03-03SpringBoot集成Spring Security用JWT令牌實現(xiàn)登錄和鑒權的方法
這篇文章主要介紹了SpringBoot集成Spring Security用JWT令牌實現(xiàn)登錄和鑒權的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05