springcloud集成 xxl-job
这两天做了公司项目集成xxl-job,记录一下
xxl-job-admin调度中心直接移植,在pom里引入一下core的依赖,我用了最新的版本
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.1.2</version>
</dependency>
执行器参考了 springboot的示例,引入一下core依赖.
项目使用eureka作为注册中心,从eureka获取调度中心的地址,所以修改了一下yml 和 XxlJobConfig
xxl:
job:
executor:
logpath: /home/jar/public/job-service/log/jobhandler
appname: yurun-job
port: -1
logretentiondays: 10
ip:
# 调度中心 eureka服务
admin:
addresses: job-admin-service
accessToken:
XxlJobConfig
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
//服务发现 从eureka拉取服务 dll
List<String> urls = Lists.newArrayList();
List<ServiceInstance> instances = discoveryClient.getInstances(adminAddresses);
if(StringUtils.isNotEmpty(instances)) {
instances.forEach(serviceInstance->{
urls.add("http://"+ serviceInstance.getHost() + ":" + serviceInstance.getPort());
});
}
xxlJobSpringExecutor.setAdminAddresses(String.join(",",urls));
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}