- 1、官方提供的 starter
spring-boot-starter-{name}
- 2、自己封装的 starter
{name}-spring-boot-starter
2、封装 xxl-job-spring-boot-starter
- 1、创建项目
本次我们封装的是 xxl-job的starter,项目命令为 xxl-job-spring-boot-starter,目录结构如下:
- 2、引入 pom.xml
4.0.0 com.asurplus xxl-job-spring-boot-starter 2.3.0 xxl-job-spring-boot-starter SpringBoot整合xxl-job快速开发 https://gitee.com/asurplus/xxl-job-spring-boot-starter 2.5.3 2.3.0 org.springframework.boot spring-boot-autoconfigure ${spring.boot.version} provided org.springframework.boot spring-boot-configuration-processor ${spring.boot.version} provided com.xuxueli xxl-job-core ${xxl-job.version} org.apache.maven.plugins maven-compiler-plugin 3.8.0 1.8 1.8
其中,configuration-processor 和 autoconfigure 是必备的,这样才能使用 SpringBoot 的自动装配原理,xxl-job 我们选用的版本为:2.3.0
- 3、xxl-job 配置参数
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "xxl-job")
public class XxlJobProperties {
private final AdminProperties admin = new AdminProperties();
private final ExecutorProperties executor = new ExecutorProperties();
private String accessToken = "";
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public static class AdminProperties {
private String addresses = "http://127.0.0.1:8080/xxl-job-admin";
public String getAddresses() {
return addresses;
}
public void setAddresses(String adminAddresses) {
this.addresses = adminAddresses;
}
}
public static class ExecutorProperties {
private String appname = "xxl-job-executor-sample";
private String address;
private String ip;
private int port = 9999;
private String logPath = "/data/applogs/xxl-job/jobhandler";
private int logRetentionDays = 30;
public String getAppname() {
return appname;
}
public void setAppname(String appname) {
this.appname = appname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getLogPath() {
return logPath;
}
public void setLogPath(String logPath) {
this.logPath = logPath;
}
public int getLogRetentionDays() {
return logRetentionDays;
}
public void setLogRetentionDays(int logRetentionDays) {
this.logRetentionDays = logRetentionDays;
}
}
public AdminProperties getAdmin() {
return admin;
}
public ExecutorProperties getExecutor() {
return executor;
}
}
- 4、xxl-job 配置类
import com.asurplus.spring.starter.xxljob.properties.XxlJobProperties;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({XxlJobProperties.class})
public class XxlJobConfig {
private static Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Bean
@ConditionalOnMissingBean
public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties xxlJobProperties) {
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdmin().getAddresses());
xxlJobSpringExecutor.setAppname(xxlJobProperties.getExecutor().getAppname());
xxlJobSpringExecutor.setAddress(xxlJobProperties.getExecutor().getAddress());
xxlJobSpringExecutor.setIp(xxlJobProperties.getExecutor().getIp());
xxlJobSpringExecutor.setPort(xxlJobProperties.getExecutor().getPort());
xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());
xxlJobSpringExecutor.setLogPath(xxlJobProperties.getExecutor().getLogPath());
xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getExecutor().getLogRetentionDays());
logger.info(">>>>>>>>>>> xxl-job starting...");
return xxlJobSpringExecutor;
}
}
@ConditionalOnMissingBean,它是修饰bean的一个注解,主要实现的是,当你的bean被注册之后,如果而注册相同类型的bean,就不会成功,它会保证你的bean只有一个,即你的实例只有一个,当你注册多个相同的bean时,会出现异常,以此来告诉开发人员。
- 5、spring.factories
在 resources 目录下,创建 META-INF 目录,在 META-INF 目录下创建 spring.factories 文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.asurplus.spring.starter.xxljob.config.XxlJobConfig
因为 SpringBoot 默认只扫描启动内所在包下面的配置类,我们自己封装的 starter 是没有启动类的,所以 SpringBoot 就提出了使用 spring.factories 来告诉自动加载需要扫描的文件路径
- 6、打包发布
使用 maven 打包:
mvn clean install
在 target 目录得到 xxl-job-spring-boot-starter-2.3.0.jar 文件
3、引入 xxl-job-spring-boot-starter- 1、将文件放在项目的 /resources/lib/ 目录下
- 2、在 pom.xml 文件中引入
com.asurplus xxl-job-spring-boot-starter 2.3.0 ${project.basedir}/src/main/resources/lib/xxl-job-spring-boot-starter-2.3.0.jar
- 3、配置信息 application.yml
# xxl-job配置信息
xxl-job:
admin:
# 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册
addresses: http://127.0.0.1:8080/xxl-job-admin
# 执行器通讯TOKEN [选填]:非空时启用
accessToken:
executor:
# 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
appname: xxl-job-executor-sample
# 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题
address:
# 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务"
ip: 127.0.0.1
# 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口
port: 9999
# 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径
logpath:
# 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能
logretentiondays: 30
至此,我们自己封装自己的starter(xxl-job-spring-boot-starter)就完成了,快去试试吧,也可以直接下载我的项目:
https://gitee.com/asurplus/xxl-job-spring-boot-starter
关于在 SpringBoot 中如何接入 xxl-job 参考我的另一篇博文:【SpringBoot】62、SpringBoot中接入xxl-job实现分布式任务调度
https://blog.csdn.net/qq_40065776/article/details/124475340?spm=1001.2014.3001.5501
如您在阅读中发现不足,欢迎留言!!!



