下载源码
https://github.com/xuxueli/xxl-job.git
配置数据库
执行源码下doc.db下的sql
修改application.properties配置文件
#修改为自己的数据库
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
maven打包
docker部署(自定义镜像)
挑选位置,创建Dockerfile文件 vi Dockerfile
FROM openjdk:8-jre-slim
ENV PARAMS=""
ENV TZ=PRC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ADD xxl-job-admin-*.jar /app.jar
ENTRYPOINT ["sh","-c","java -jar $JAVA_OPTS /app.jar $PARAMS"]
jar包放到服务器上
构建镜像
docker build -t xxl-job-admin:2.4.1 .
启动自定义镜像,生成容器
docker run --name xxl-job -v /mnt/Logs/xxl-job:/mnt/Logs/xxl-job -p 8107:8107 -d xxl-job-admin:2.4.1
整合springboot
引入依赖
<dependencies>
<!-- xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
配置文件
xxl:
job:
admin:
addresses: http://172.17.0.1:8107/xxl-job-admin
executor:
appname: xxl-job-executor-pv
address:
ip:
port: -1
logpath: /mnt/Logs/xxl-job/jobhandler
logretentiondays: 30
accessToken: default_token
配置类
@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;
}
定时器代码:
@XxlJob("demoJobHandler")
public void demoJobHandler() throws Exception {
logger.info(">>>>>>>>>>>>>>>>>>>>>>"+Thread.currentThread().getName());
}
界面操作
http://...:8107/xxl-job-admin/(自己服务器ip)
创建执行器:xxl-job-executor-pv
创建任务,挑选执行器等等
点击执行就可以了