主应用
采用docker进行部署安装
• xuxueli/xxl-job-admin:2.3.0:官方镜像,支持x86-64,适用一般服务器
• pi4k8s/xxl-job-admin:2.3.0:支持arm64v8,树莓派可用(mac arm64架构)
$ docker pull pi4k8s/xxl-job-admin:2.3.0
tips
- mysql 启动
$ docker network create mysqlnet
$ docker run -itd --name mysql-local -p 3306:3306 --network mysqlnet --network-alias mysqlserver -e MYSQL_ROOT_PASSWORD=123456 mysql
--name 镜像名称
-p 端口映射
--network mysqlnet 设置网络用于docker 网络互访
--network-alias mysqlserver 设置别名,用于设置访问(spring.datasource.url=jdbc:mysql://(宿主ip):3306/xxl_job)
-e 设置密码
- 设置配置文件
$ mkdir -p /docker-config/server/config
$ vim application-prod.properties
### web
server.port=8100
server.servlet.context-path=/xxl-job-admin
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://mysqlserver:3306/xxl_job
spring.datasource.username=root
spring.datasource.password=123456
- 获取源代码-解压缩
$ wget https://github.com/xuxueli/xxl-job/archive/refs/tags/2.3.0.tar.gz
tar -zxf 2.3.0.tar.gz
导入数据库表文件
xxl-job-2.3.0/doc/db/tables_xxl_job.sqlarm64启动
$ docker run -e PARAMS="--spring.datasource.url=jdbc:mysql://10.122.139.48:3306/xxl_job --spring.datasource.password=123456 --xxl.job.accessToken=default_token" -p 8080:8080 --name xxl-job-admin -d pi4k8s/xxl-job-admin:2.3.0
设置params --key=value --key2=value
使用容器
• 访问地址:http://$ip:8080/xxl-job-admin
• 用户名/密码:admin/123456删除
$ docker stop xxl-job-admin && docker rm xxl-job-admin
# 强制删除
$ docker rm -f xxl-job-admin
- 日常运维
$ docker start xxl-job-admin
$ docker restart xxl-job-admin
$ docker stop xxl-job-admin
- 备份相关
• 配置文件:/opt/server/xxl-job-admin/config/application-prod.properties
• 日志目录:/opt/server/xxl-job-admin/logs/ - error
• xxxx:xxx/run 500 docker 设置ip 不要为127.0.0.1 改为局域网ip
• token 500 设置 xxl.job.accessToken=default_token 服务器、客户端均要设置一致的token
子应用
- maven
<!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
- 配置xxl-job config
docker 不要使用127.0.0.1进行绑定
xxl:
job:
admin:
addresses: http://localhost:8080/xxl-job-admin/
accessToken: default_token
executor:
# 执行器的应用名称
appname: xxl-job-executor-sample
# 执行器注册 [选填]:优先使用该配置作为注册地址
address: http://10.122.139.48:9999
# 执行器IP [选填]:默认为空表示自动获取IP
ip: 10.122.139.48
# 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999
port: 9999
# 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
# logpath: D:\Codes\logs
# logpath: /data/logs/mls/job
logpath: logs/
# 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
logretentiondays: 7
- XxlJobConfig.java
package com.example.flexible_operation.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
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() {
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;
}
/**
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP;
*
* 1、引入依赖:
* <dependency>
* <groupId>org.springframework.cloud</groupId>
* <artifactId>spring-cloud-commons</artifactId>
* <version>${version}</version>
* </dependency>
*
* 2、配置文件,或者容器启动变量
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
*
* 3、获取IP
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
*/
}
- 配置任务
UserProjectXxlJob.java
package com.example.flexible_operation.jobservice;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* XxlJob开发示例(Bean模式)
*
* 开发步骤:
* 1、任务开发:在Spring Bean实例中,开发Job方法;
* 2、注解配置:为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志;
* 4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果;
*
* @author
*/
@Component
public class UserProjectXxlJob {
private static Logger logger = LoggerFactory.getLogger(UserProjectXxlJob.class);
/**
* 1、简单任务示例(Bean模式)
*/
@XxlJob("demoJobHandler")
public void demoJobHandler() throws Exception {
XxlJobHelper.log("XXL-JOB, Hello World.");
for (int i = 0; i < 5; i++) {
XxlJobHelper.log("beat at:" + i);
TimeUnit.SECONDS.sleep(2);
}
// default success
}
}