diff --git a/blade-core-launch/src/main/java/org/springblade/core/launch/constant/AppConstant.java b/blade-core-launch/src/main/java/org/springblade/core/launch/constant/AppConstant.java index 427e182..277423d 100644 --- a/blade-core-launch/src/main/java/org/springblade/core/launch/constant/AppConstant.java +++ b/blade-core-launch/src/main/java/org/springblade/core/launch/constant/AppConstant.java @@ -132,6 +132,11 @@ public interface AppConstant { */ String APPLICATION_FILE_NAME =APPLICATION_NAME_PREFIX +"file"; + /** + * 三方服务模块名称 + */ + String APPLICATION_OPENAPI_NAME =APPLICATION_NAME_PREFIX +"openapi"; + /** * 开发环境 diff --git a/blade-starter-threadpool/pom.xml b/blade-starter-threadpool/pom.xml new file mode 100644 index 0000000..ef6290d --- /dev/null +++ b/blade-starter-threadpool/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.springblade + BladeX-Tool + ${revision} + + + blade-starter-threadpool + ${project.artifactId} + ${project.parent.version} + jar + + + + + org.dromara.dynamictp + dynamic-tp-spring-cloud-starter-nacos + 1.1.9.1-3.x + + + org.springblade + blade-core-auto + provided + + + \ No newline at end of file diff --git a/blade-starter-threadpool/src/main/java/org/springblade/core/threadpool/config/ThreadPoolConfiguration.java b/blade-starter-threadpool/src/main/java/org/springblade/core/threadpool/config/ThreadPoolConfiguration.java new file mode 100644 index 0000000..f27ca54 --- /dev/null +++ b/blade-starter-threadpool/src/main/java/org/springblade/core/threadpool/config/ThreadPoolConfiguration.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.threadpool.config; + +import org.dromara.dynamictp.core.executor.DtpExecutor; +import org.dromara.dynamictp.core.executor.OrderedDtpExecutor; +import org.dromara.dynamictp.core.support.DynamicTp; +import org.dromara.dynamictp.core.support.ThreadPoolBuilder; +import org.dromara.dynamictp.core.support.ThreadPoolCreator; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import static org.dromara.dynamictp.common.em.QueueTypeEnum.MEMORY_SAFE_LINKED_BLOCKING_QUEUE; +import static org.dromara.dynamictp.common.em.RejectedTypeEnum.CALLER_RUNS_POLICY; + +/** + * @author Redick01 + */ + +// @Configuration +public class ThreadPoolConfiguration { + + /** + * 通过{@link DynamicTp} 注解定义普通juc线程池,会享受到该框架增强能力,注解名称优先级高于方法名 + * + * @return 线程池实例 + */ + @DynamicTp("jucThreadPoolExecutor") + @Bean + public ThreadPoolExecutor jucThreadPoolExecutor() { + return (ThreadPoolExecutor) Executors.newFixedThreadPool(1); + } + + /** + * 通过{@link DynamicTp} 注解定义spring线程池,会享受到该框架增强能力,注解名称优先级高于方法名 + * + * @return 线程池实例 + */ + @DynamicTp("threadPoolTaskExecutor") + @Bean + public ThreadPoolTaskExecutor threadPoolTaskExecutor() { + return new ThreadPoolTaskExecutor(); + } + + /** + * 通过{@link ThreadPoolCreator} 快速创建一些简单配置的线程池,使用默认参数 + * tips: 建议直接在配置中心配置就行,不用@Bean声明 + * + * @return 线程池实例 + */ + @Bean + public DtpExecutor dtpExecutor0() { + return ThreadPoolCreator.createDynamicFast("dtpExecutor0"); + } + + /** + * 通过{@link ThreadPoolBuilder} 设置详细参数创建动态线程池 + * tips: 建议直接在配置中心配置就行,不用@Bean声明 + * @return 线程池实例 + */ + @Bean + public ThreadPoolExecutor dtpExecutor1() { + return ThreadPoolBuilder.newBuilder() + .threadPoolName("dtpExecutor1") + .threadFactory("test-dtp-common") + .corePoolSize(10) + .maximumPoolSize(15) + .keepAliveTime(40) + .timeUnit(TimeUnit.SECONDS) + .workQueue(MEMORY_SAFE_LINKED_BLOCKING_QUEUE.getName(), 2000) + .buildDynamic(); + } + + /** + * 通过{@link ThreadPoolBuilder} 设置详细参数创建动态线程池 + * eager,参考tomcat线程池设计,适用于处理io密集型任务场景,具体参数可以看代码注释 + * tips: 建议直接在配置中心配置就行,不用@Bean声明 + * @return 线程池实例 + */ + @Bean + public DtpExecutor eagerDtpExecutor() { + return ThreadPoolBuilder.newBuilder() + .threadPoolName("eagerDtpExecutor") + .threadFactory("test-eager") + .corePoolSize(2) + .maximumPoolSize(4) + .queueCapacity(2000) + .eager() + .buildDynamic(); + } + + /** + * 通过{@link ThreadPoolBuilder} 设置详细参数创建动态线程池 + * ordered,适用于处理有序任务场景,任务要实现Ordered接口,具体参数可以看代码注释 + * tips: 建议直接在配置中心配置就行,不用@Bean声明 + * @return 线程池实例 + */ + @Bean + public OrderedDtpExecutor orderedDtpExecutor() { + return ThreadPoolBuilder.newBuilder() + .threadPoolName("orderedDtpExecutor") + .threadFactory("test-ordered") + .corePoolSize(4) + .maximumPoolSize(4) + .queueCapacity(2000) + .buildOrdered(); + } + + /** + * 通过{@link ThreadPoolBuilder} 设置详细参数创建线程池 + * scheduled,适用于处理定时任务场景,具体参数可以看代码注释 + * tips: 建议直接在配置中心配置就行,不用@Bean声明 + * @return 线程池实例 + */ + @Bean + public ScheduledExecutorService scheduledDtpExecutor() { + return ThreadPoolBuilder.newBuilder() + .threadPoolName("scheduledDtpExecutor") + .corePoolSize(2) + .threadFactory("test-scheduled") + .rejectedExecutionHandler(CALLER_RUNS_POLICY.getName()) + .buildScheduled(); + } +} diff --git a/blade-starter-threadpool/src/main/java/org/springblade/core/threadpool/wrapper/CustomTaskWrapper.java b/blade-starter-threadpool/src/main/java/org/springblade/core/threadpool/wrapper/CustomTaskWrapper.java new file mode 100644 index 0000000..256ab03 --- /dev/null +++ b/blade-starter-threadpool/src/main/java/org/springblade/core/threadpool/wrapper/CustomTaskWrapper.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.threadpool.wrapper; + +import lombok.extern.slf4j.Slf4j; +import org.dromara.dynamictp.core.support.task.wrapper.TaskWrapper; +import org.springblade.core.auto.service.AutoService; + +/** + * CustomTaskWrapper related + * + * @author yanhom + * @since 1.1.0 + */ +@AutoService(TaskWrapper.class) +@Slf4j +public class CustomTaskWrapper implements TaskWrapper { + + @Override + public String name() { + return "custom"; + } + + @Override + public Runnable wrap(Runnable runnable) { + return new MyRunnable(runnable); + } + + public static class MyRunnable implements Runnable { + + private final Runnable runnable; + + public MyRunnable(Runnable runnable) { + this.runnable = runnable; + } + + @Override + public void run() { + log.info("before run"); + runnable.run(); + log.info("after run"); + } + } +} diff --git a/blade-starter-threadpool/src/main/resources/dynamic-tp-nacos-cloud-demo-dtp-dev.yml b/blade-starter-threadpool/src/main/resources/dynamic-tp-nacos-cloud-demo-dtp-dev.yml new file mode 100644 index 0000000..7c98d94 --- /dev/null +++ b/blade-starter-threadpool/src/main/resources/dynamic-tp-nacos-cloud-demo-dtp-dev.yml @@ -0,0 +1,94 @@ +# 动态线程池配置文件,建议单独开一个文件放到配置中心,字段详解看readme介绍 +spring: + dynamic: + tp: + enabled: true + enabledBanner: true # 是否开启banner打印,默认true + enabledCollect: false # 是否开启监控指标采集,默认false + collectorTypes: micrometer,logging # 监控数据采集器类型(logging | micrometer | internal_logging),默认micrometer + logPath: ${user.home}/dynamic-tp/logs # 监控日志数据路径,默认 ${user.home}/logs + monitorInterval: 5 # 监控时间间隔(报警判断、指标采集),默认5s + platforms: # 通知报警平台配置 + - platform: wechat + urlKey: 3a7500-1287-4bd-a798-c5c3d8b69c # 替换 + receivers: test1,test2 # 接受人企微名称 + - platform: ding + urlKey: f80dad441fcd655438f4a08dcd6a # 替换 + secret: SECb5441fa6f375d5b9d21 # 替换,非sign模式可以没有此值 + receivers: 15810119805 # 钉钉账号手机号 + - platform: lark + urlKey: 0d944ae7-b24a-40 # 替换 + receivers: test1,test2 # 接受人飞书名称/openid + tomcatTp: # tomcat web server线程池配置 + corePoolSize: 100 + maximumPoolSize: 400 + keepAliveTime: 60 + jettyTp: # jetty web server线程池配置 + corePoolSize: 100 + maximumPoolSize: 400 + undertowTp: # undertow web server线程池配置 + corePoolSize: 100 + maximumPoolSize: 400 + keepAliveTime: 60 + hystrixTp: # hystrix 线程池配置 + - threadPoolName: hystrix1 + corePoolSize: 100 + maximumPoolSize: 400 + keepAliveTime: 60 + dubboTp: # dubbo 线程池配置 + - threadPoolName: dubboTp#20880 + corePoolSize: 100 + maximumPoolSize: 400 + keepAliveTime: 60 + rocketMqTp: # rocketmq 线程池配置 + - threadPoolName: group1#topic1 + corePoolSize: 200 + maximumPoolSize: 400 + keepAliveTime: 60 + executors: # 动态线程池配置,都有默认值,采用默认值的可以不配置该项,减少配置量 + - threadPoolName: dtpExecutor1 + 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线程池设计,优雅关闭线程池 + awaitTerminationSeconds: 5 # 单位(s) + preStartAllCoreThreads: false # 是否预热所有核心线程,默认false + runTimeout: 200 # 任务执行超时阈值,目前只做告警用,单位(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 + - threadPoolName: orderedDtpExecutor + executorType: ordered + corePoolSize: 4 + maximumPoolSize: 6 + queueCapacity: 2000 + queueType: VariableLinkedBlockingQueue + rejectedHandlerType: CallerRunsPolicy + keepAliveTime: 50 + allowCoreThreadTimeOut: false + threadNamePrefix: test diff --git a/pom.xml b/pom.xml index edb9618..358e36b 100644 --- a/pom.xml +++ b/pom.xml @@ -120,6 +120,7 @@ blade-starter-trace blade-starter-transaction blade-starter-xss + blade-starter-threadpool @@ -941,6 +942,11 @@ hutool-all 5.8.27 + + org.springblade + blade-starter-threadpool + ${revision} +