Cloud集群模式XXL-job开启自动注册执行器
在微服务架构下,传统的spring task、Quartz已经不再推荐使用,
在数据与业务增长的同时,定时任务处理数据是避免不了的,抛开单节点不谈,集群服务如果想要执行以往的定时任务最简单粗暴的方式就是使用分布式锁来保证唯一性,但是由于不可控原因,可能在执行任务期间所以压力集中到其中一个节点,所以这里使用了分布式任务调度平台xxl-job
在微服务集群中,如果需要作为xxl-job的执行器,需要在每个微服务中配置执行器的参数
像一些公共的配置可以提取到nacos中,执行器的name,、ip与port可以使用当前服务的,从而达到简化配置的目的
项目依赖lombok
新建配置文件
@Data
@ConfigurationProperties(prefix = "xxl.job")
public class XxlJobProperties {
@NestedConfigurationProperty
private XxlAdminProperties admin = new XxlAdminProperties();
@NestedConfigurationProperty
private XxlExecutorProperties executor = new XxlExecutorProperties();
}
@Data
public class XxlAdminProperties {
/**
* 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。 执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
*/
private String addresses;
}
@Data
public class XxlExecutorProperties {
/**
* 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
*/
private String appname;
/**
* 服务注册地址,优先使用该配置作为注册地址 为空时使用内嵌服务 ”IP:PORT“ 作为注册地址 从而更灵活的支持容器类型执行器动态IP和动态映射端口问题
*/
private String address;
/**
* 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP ,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和
* "调度中心请求并触发任务"
*/
private String ip;
/**
* 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
*/
private Integer port = 0;
/**
* 执行器通讯TOKEN [选填]:非空时启用;
*/
private String accessToken;
/**
* 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
*/
private String logPath = "logs/applogs/xxl-job/jobhandler";
/**
* 执行器日志保存天数 [选填] :值大于3时生效,启用执行器Log文件定期清理功能,否则不生效;
*/
private Integer logRetentionDays = 30;
}
xxl-job自动装配能力
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
@EnableConfigurationProperties(XxlJobProperties.class)
public class XxlJobAutoConfiguration {
/**
* 服务名称 包含 XXL_JOB_ADMIN 则说明是 Admin
*/
private static final String XXL_JOB_ADMIN = "xxl-job-admin";
/**
* 配置xxl-job 执行器,提供自动发现 xxl-job-admin 能力
* @param xxlJobProperties xxl 配置
* @param discoveryClient 注册发现客户端
* @return
*/
@Bean
public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties xxlJobProperties, Environment environment,
DiscoveryClient discoveryClient) {
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
XxlExecutorProperties executor = xxlJobProperties.getExecutor();
// 应用名默认为服务名
String appName = executor.getAppname();
if (!StringUtils.hasText(appName)) {
appName = environment.getProperty("spring.application.name");
}
xxlJobSpringExecutor.setAppname(appName);
xxlJobSpringExecutor.setAddress(executor.getAddress());
xxlJobSpringExecutor.setIp(executor.getIp());
xxlJobSpringExecutor.setPort(executor.getPort());
xxlJobSpringExecutor.setAccessToken(executor.getAccessToken());
xxlJobSpringExecutor.setLogPath(executor.getLogPath());
xxlJobSpringExecutor.setLogRetentionDays(executor.getLogRetentionDays());
// 如果配置为空则获取注册中心的服务列表 "http://bukeneng-xxl:9080/xxl-job-admin"
if (!StringUtils.hasText(xxlJobProperties.getAdmin().getAddresses())) {
String serverList = discoveryClient.getServices().stream().filter(s -> s.contains(XXL_JOB_ADMIN))
.flatMap(s -> discoveryClient.getInstances(s).stream()).map(instance -> String
.format("http://%s:%s/%s", instance.getHost(), instance.getPort(), XXL_JOB_ADMIN))
.collect(Collectors.joining(","));
xxlJobSpringExecutor.setAdminAddresses(serverList);
}
else {
xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdmin().getAddresses());
}
return xxlJobSpringExecutor;
}
申明注解开启支持xxl-job
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({ XxlJobAutoConfiguration.class })
public @interface EnableXxlJob {
}
一般公司会有很多的微服务,在微服务中如果需要xxl-job的能力,可以直接使用@EnableXxlJob开启,其中后台调度中心地址addresses可以直接放在公用配置文件中,XxlExecutorProperties执行器配置项可以按需添加,并且添加当前项目依赖和xxl-job-core依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
这里注意如果服务为集群模式,xxl.job.executor.port需要在启动参数重进行区分,避免重复的端口号,这样就可以使用分布式的不同路由策略(轮循、随机、一致性hash、故障转移)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!