SpringBoot整合任务调度中心xxl-job(docker部署xxl-job)
·
SpringBoot整合任务调度中心xxl-job(docker部署xxl-job)
前言:XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用
是目前主流的分布式任务调度平台,许多公司的产品业务都有XXL-JOB的接入
准备: 在pull镜像创建容器之前先准备好mysql,后面配置需要,表的sql:[xxl_job],进入db目录,其中 tables_xxl_job.sql就是我们所需要的数据库表
一.docker部署xxl-job
- docker拉取最新镜像,目前最新的版本 2.2.0
docker pull xuxueli/xxl-job-admin:2.2.0
- 创建并运行容器 (注意数据库的url、用户名、密码、库名对应上。端口不被占用就行)
docker run -di -e PARAMS="--spring.datasource.url=jdbc:mysql://localhost:3306/xxl_job?Unicode=true&characterEncoding=UTF-8 --spring.datasource.username=root --spring.datasource.password=123456" -p 8086:8080 -v /tmp:/data/applogs --name xxl-job-admin xuxueli/xxl-job-admin:2.2.0
- 浏览器登录访问(http://ip:8086/xxl-job-admin/ , 默认用户名admin 密码 123456)
二.SpringBoot整合xxl-job
- 导入依赖(版本对应上)
<!--任务调度中心-->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.2.0</version>
</dependency>
- 配置文件
xxl.job.admin.addresses = http://127.0.0.1:8086/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken =
### 配置当前执行器的名字
xxl.job.executor.appname = ${spring.application.name}
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address =
xxl.job.executor.ip =
xxl.job.executor.port = 9999
# 配置日志文件的路径
xxl.job.executor.logpath = /usr/local/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays = 30
- 自定义xxl-job注解
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({XxlJobConfig.class})
public @interface EnableXxlJob {
}
- 读取配置文件并创建自动注册到admin管理平台的执行器 OnLine 机器地址
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import groovy.util.logging.Log;
import groovy.util.logging.Slf4j;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Data
@Slf4j
@Configuration
public class XxlJobConfig {
private static final Logger log = 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() {
log.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();
*/
}
- 在服务启动类上加自定义注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
@EnableCSFeignClients
@EnableXxlJob
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class);
System.out.println("(♥◠‿◠)ノ゙ 系统模块启动成功");
}
}
- 方法注解使用
/**
* 测试定时器环境
*
* @param s
* @return
* @throws Exception
*/
@XxlJob("mywindTestJobHandler")
public ReturnT<String> mywindTestJobHandler(String s) throws Exception {
logger.info(">>>>>>>>>>> xxl-job test."+s);
return new ReturnT(ReturnT.SUCCESS_CODE, "测试成功");
}
测试结果
- 新增任务管理
- 新增执行器
- 开始运行
- 结果及日志
完结:任务调度中心执行成功,到此整合完毕,顺便记录一下,有问题可进行探讨交流,谢谢!
更多推荐
已为社区贡献2条内容
所有评论(0)