From caf670f9147221bca0f3bee821ab51cef6f1f6f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8D=8E=E6=98=8E?= <32033658@qq.com> Date: Mon, 25 Oct 2021 15:50:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0starter=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xxl-job-boot-starter-executor/pom.xml | 64 +++++++++++++++ .../core/config/ApplicationProperties.java | 24 ++++++ .../job/core/config/JobAutoConfiguration.java | 67 ++++++++++++++++ .../job/core/config/JobConfigProperties.java | 66 ++++++++++++++++ ...itional-spring-configuration-metadata.json | 55 +++++++++++++ .../main/resources/META-INF/spring.factories | 2 + .../pom.xml | 15 +++- .../executor/core/config/XxlJobConfig.java | 78 ------------------- .../service/jobhandler/SampleXxlJob.java | 13 ++-- ...application.properties => application.yml} | 32 ++++---- 10 files changed, 315 insertions(+), 101 deletions(-) create mode 100644 xxl-job-boot-starter-executor/pom.xml create mode 100644 xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/ApplicationProperties.java create mode 100644 xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/JobAutoConfiguration.java create mode 100644 xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/JobConfigProperties.java create mode 100644 xxl-job-boot-starter-executor/src/main/resources/META-INF/additional-spring-configuration-metadata.json create mode 100644 xxl-job-boot-starter-executor/src/main/resources/META-INF/spring.factories delete mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java rename xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/{application.properties => application.yml} (50%) diff --git a/xxl-job-boot-starter-executor/pom.xml b/xxl-job-boot-starter-executor/pom.xml new file mode 100644 index 00000000..0768de93 --- /dev/null +++ b/xxl-job-boot-starter-executor/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + com.xuxueli + xxl-job + 2.3.0 + + xxl-job-boot-starter-executor + ${project.artifactId} + Example executor project for spring boot. + https://www.xuxueli.com/ + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring-boot.version} + pom + import + + + + + + org.springframework.boot + spring-boot-starter-web + true + + + org.springframework.boot + spring-boot-starter-test + true + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.projectlombok + lombok + true + provided + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + com.xuxueli + xxl-job-core + ${project.parent.version} + + + \ No newline at end of file diff --git a/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/ApplicationProperties.java b/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/ApplicationProperties.java new file mode 100644 index 00000000..edd308fd --- /dev/null +++ b/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/ApplicationProperties.java @@ -0,0 +1,24 @@ +package com.xxl.job.core.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +/** + * 读取应用的配置参数 + * @author cqyhm + * + */ +@Setter@Getter@ToString +@ConfigurationProperties(prefix = ApplicationProperties.PREFIX, ignoreInvalidFields = true) +public class ApplicationProperties { + /** + * 配置前缀 + */ + public static final String PREFIX = "spring.application"; + /** + * 应用程序名称 + */ + private String name; +} diff --git a/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/JobAutoConfiguration.java b/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/JobAutoConfiguration.java new file mode 100644 index 00000000..fd90481f --- /dev/null +++ b/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/JobAutoConfiguration.java @@ -0,0 +1,67 @@ +package com.xxl.job.core.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.StringUtils; + +import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; +import com.xxl.job.core.thread.ExecutorRegistryThread; + +import lombok.extern.slf4j.Slf4j; + +/** + * 执行器端自动配置 + * @author cqyhm + */ +@Slf4j +@Configuration() +@ConditionalOnClass(ExecutorRegistryThread.class) +@ConditionalOnProperty(prefix =JobConfigProperties.PREFIX ,name = "enabled", matchIfMissing = true) +@EnableConfigurationProperties(value = {JobConfigProperties.class,ApplicationProperties.class}) +public class JobAutoConfiguration { + + @Bean + public XxlJobSpringExecutor xxlJobExecutor(JobConfigProperties jobConfigProperties, + ApplicationProperties applicationProperties) { + log.info(">>>>>>>>>>> xxl-job config init."); + log.info("读取配置{}",applicationProperties); + log.info("读取配置{}",jobConfigProperties); + XxlJobSpringExecutor JobSpringExecutor = new XxlJobSpringExecutor(); + //通讯令牌 + JobSpringExecutor.setAccessToken(jobConfigProperties.getAccessToken()); + //调度器设置 + JobSpringExecutor.setAdminAddresses(jobConfigProperties.getAdmin().getAddresses()); + //执行器设置 + String name=applicationProperties.getName(); //应用程序的名称 + String appName=jobConfigProperties.getExecutor().getAppname(); //调度器的名称如果没有设置取应用的名称 + JobSpringExecutor.setAppname(StringUtils.hasText(appName) ? name: appName); + JobSpringExecutor.setIp(jobConfigProperties.getExecutor().getIp()); + JobSpringExecutor.setPort(jobConfigProperties.getExecutor().getPort()); + //执行器日志路径 + JobSpringExecutor.setLogPath(jobConfigProperties.getExecutor().getLogpath()); + JobSpringExecutor.setLogRetentionDays(jobConfigProperties.getExecutor().getLogretentiondays()); + + return JobSpringExecutor; + } + /** + * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP; + * + * 1、引入依赖: + * + * org.springframework.cloud + * spring-cloud-commons + * ${version} + * + * + * 2、配置文件,或者容器启动变量 + * spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.' + * + * 3、获取IP + * String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); + */ + + +} \ No newline at end of file diff --git a/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/JobConfigProperties.java b/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/JobConfigProperties.java new file mode 100644 index 00000000..e4c68c5e --- /dev/null +++ b/xxl-job-boot-starter-executor/src/main/java/com/xxl/job/core/config/JobConfigProperties.java @@ -0,0 +1,66 @@ +package com.xxl.job.core.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import lombok.Data; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +/** + * 获取定时任务相关的配置参数 + * @author cqyhm + * + */ +@Setter@Getter@ToString +@ConfigurationProperties(prefix = JobConfigProperties.PREFIX, ignoreInvalidFields = true) +public class JobConfigProperties { + + public static final String PREFIX = "xxl.job"; + /** + * 通讯令牌 + */ + private String accessToken; + /** + * 调度器设置 + */ + private Admin admin; + /** + * 执行器设置 + */ + private Executor executor; + + @Data + public static class Admin { + /** + * 调度器地址端口多个可以用逗号隔开 + */ + private String addresses; + } + @Data + public static class Executor { + /** + * 执行器的名称(唯一编码,默认为当前应用程序的名称作为调度器的名称) + */ + private String appname; + /** + * 执行器运行地址为空=ip:port + */ + private String address; + /** + * 执行器所在的本机地址 + */ + private String ip=""; + /** + * 执行器监控的端口 + */ + private Integer port=0; + /** + * 日志路径 + */ + private String logpath; + /** + * 日志保留天数 + */ + private Integer logretentiondays; + } +} diff --git a/xxl-job-boot-starter-executor/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/xxl-job-boot-starter-executor/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 00000000..c1878997 --- /dev/null +++ b/xxl-job-boot-starter-executor/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,55 @@ +{ + "properties": [ + { + "name": "xxl.job.enabled", + "type": "java.lang.Boolean", + "defaultValue": "true", + "description": "是否启用任务执行器,默认启动true" + }, + { + "name": "xxl.job.accessToken", + "type": "java.lang.String", + "defaultValue": "", + "description": "调度时的通讯令牌." + }, + { + "name": "xxl.job.admin.addresses", + "type": "java.lang.String", + "defaultValue": "http://localhost:8080/xxl-job-admin/", + "description": "执行器连接调度中心的地址" + }, + { + "name": "xxl.job.executor.appname", + "type": "java.lang.String", + "defaultValue": "${spring.application.name}", + "description": "默认是微服务的名称" + }, + { + "name": "xxl.job.executor.logpath", + "type": "java.lang.String", + "defaultValue": "执行器日志输出路径", + "description": "" + }, + { + "name": "xxl.job.executor.logretentiondays", + "type": "java.lang.Integer", + "defaultValue": "7", + "description": "执行器日志保留天数" + }, + { + "name": "xxl.job.executor.port", + "type": "java.lang.Integer", + "description": "执行器监控调度的端口." + }, + { + "name": "xxl.job.executor.ip", + "type": "java.lang.String", + "description": "执行器所在机器的IP地址" + }, + { + "name": "xxl.job.executor.address", + "type": "java.lang.String", + "description": "执行器所在机器的地址,如果为空则使用ip:port" + } + ] +} \ No newline at end of file diff --git a/xxl-job-boot-starter-executor/src/main/resources/META-INF/spring.factories b/xxl-job-boot-starter-executor/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..1d9a6b7c --- /dev/null +++ b/xxl-job-boot-starter-executor/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.xxl.job.core.config.JobAutoConfiguration diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/pom.xml b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/pom.xml index 9782a28d..e119ac5c 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/pom.xml +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/pom.xml @@ -42,14 +42,25 @@ spring-boot-starter-test test - + + org.projectlombok + lombok + provided + + + com.xuxueli + xxl-job-boot-starter-executor + ${project.parent.version} + + + diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java deleted file mode 100644 index bfd80e22..00000000 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/core/config/XxlJobConfig.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.xxl.job.executor.core.config; - -import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * xxl-job config - * - * @author xuxueli 2017-04-28 - */ -@Configuration -public class XxlJobConfig { - private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); - - @Value("${xxl.job.admin.addresses}") - private String adminAddresses; - - @Value("${xxl.job.accessToken}") - private String accessToken; - - @Value("${xxl.job.executor.appname}") - private String appname; - - @Value("${xxl.job.executor.address}") - private String address; - - @Value("${xxl.job.executor.ip}") - private String ip; - - @Value("${xxl.job.executor.port}") - private int port; - - @Value("${xxl.job.executor.logpath}") - private String logPath; - - @Value("${xxl.job.executor.logretentiondays}") - private int logRetentionDays; - - - @Bean - public XxlJobSpringExecutor xxlJobExecutor() { - logger.info(">>>>>>>>>>> xxl-job config init."); - XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); - xxlJobSpringExecutor.setAdminAddresses(adminAddresses); - xxlJobSpringExecutor.setAppname(appname); - xxlJobSpringExecutor.setAddress(address); - xxlJobSpringExecutor.setIp(ip); - xxlJobSpringExecutor.setPort(port); - xxlJobSpringExecutor.setAccessToken(accessToken); - xxlJobSpringExecutor.setLogPath(logPath); - xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); - - return xxlJobSpringExecutor; - } - - /** - * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP; - * - * 1、引入依赖: - * - * org.springframework.cloud - * spring-cloud-commons - * ${version} - * - * - * 2、配置文件,或者容器启动变量 - * spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.' - * - * 3、获取IP - * String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); - */ - - -} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java index 7ea8e52d..49749ffb 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/java/com/xxl/job/executor/service/jobhandler/SampleXxlJob.java @@ -1,11 +1,5 @@ package com.xxl.job.executor.service.jobhandler; -import com.xxl.job.core.context.XxlJobHelper; -import com.xxl.job.core.handler.annotation.XxlJob; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataOutputStream; @@ -15,6 +9,13 @@ import java.net.URL; import java.util.Arrays; import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import com.xxl.job.core.context.XxlJobHelper; +import com.xxl.job.core.handler.annotation.XxlJob; + /** * XxlJob开发示例(Bean模式) * diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.yml similarity index 50% rename from xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties rename to xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.yml index e067db4f..38a21974 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.properties +++ b/xxl-job-executor-samples/xxl-job-executor-sample-springboot/src/main/resources/application.yml @@ -1,26 +1,28 @@ # web port -server.port=8081 +server: + port: 8081 # no web #spring.main.web-environment=false - +spring: application: name: xxl-job-executor-sample # log config -logging.config=classpath:logback.xml - +logging: + config: classpath:logback.xml ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" -xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin - -### xxl-job, access token -xxl.job.accessToken= - +xxl: + job: + admin: + addresses: http://127.0.0.1:8080/xxl-job-admin + accessToken: ### xxl-job, access token ### xxl-job executor appname -xxl.job.executor.appname=xxl-job-executor-sample + executor: +# appname: xxl-job-executor-sample ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null -xxl.job.executor.address= + address: ### xxl-job executor server-info -xxl.job.executor.ip= -xxl.job.executor.port=9999 + ip: + port: 9999 ### xxl-job executor log-path -xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler + logpath: /data/applogs/xxl-job/jobhandler ### xxl-job executor log-retention-days -xxl.job.executor.logretentiondays=30 + logretentiondays: 30