springcloud整合xxl
前言
时下火爆的定时任务开源框架——xxl-job,有完善的中文社区,详细的整合文档及示例,此文仅作为自己整合的记录。
xxl-job-admin
从xxl社区下载源码后,将xxl-job-admin导入项目,并修改配置
- 数据库配置:源码docs/db文件夹下有sql脚本,导入并修改配置即可
- 如果要接入注册中心,新增相关依赖,并添加配置即可
spring:
datasource:
url: jdbc:mysql://10.18.32.161:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
username: hcp
password: Hcp@123_2020
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 10
maximum-pool-size: 30
auto-commit: true
idle-timeout: 3000
pool-name: dd
max-lifetime: 90000
connection-timeout: 1000
connection-test-query: SELECT 1
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:10100/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}
启动后访问http://ip:port/
但是如果通过微服务网关访问会出现静态资源丢失现象,个人是对网关做了配置去单独代理相关路径,如果有更好的方法,欢迎提出
zuul:
routes:
static:
path: /static/**
serviceId: http://localhost:10310/static/
chartInfo:
path: /chartInfo/**
serviceId: http://localhost:10310/chartInfo/
jobinfo:
path: /jobinfo/**
serviceId: http://localhost:10310/jobinfo/
joblog:
path: /joblog/**
serviceId: http://localhost:10310/joblog/
jobgroup:
path: /jobgroup/**
serviceId: http://localhost:10310/jobgroup/
user:
path: /user/**
serviceId: http://localhost:10310/user/
help:
path: /help/**
serviceId: http://localhost:10310/help/
任务整合
我们假设工程下的activemq-provider子工程需要执行定时任务:
依赖
引入xxl-core依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.2.0</version>
</dependency>
配置类
@Configuration
@RefreshScope
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() {
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;
}
}
@RefreshScope是整合了springcloud配置中心
配置
xxl:
job:
admin:
addresses: http://localhost:10302/xxl
accessToken:
executor:
appname: test
address:
ip:
port: 9999
logpath:
logretentiondays: 3
address即xxl-job-admin的url
excutor.appname:是执行器名称,后文介绍
编写测试类
@Component
public class Test {
@XxlJob("hello")
public ReturnT<String> execute(String param) {
XxlJobLogger.log("hello" + param);
System.out.println("hello" + param);
return ReturnT.SUCCESS;
}
}
记得在容器扫描路径下
定时任务
新增执行器
appname即之前配置的excutor.appname
名称随意,用于辨识
注册方式建议自动
机器地址,手动注册时生效
新增任务
执行器选择我们刚创建的
运行模式选择bean
jobHandler:即我们测试代码中@XxlJob
注解的value值
其他参数看情况填写
执行测试
点击执行一次
查看日志
整合完成~
资料
源码地址
https://github.com/dangzhicairang/my-cloud.git
配置中心地址
https://github.com/dangzhicairang/my-config.git