[2023] XXL-Job specifically configures and installs containers through docker, and then performs registration through springboot
I. Overview
In daily business scenarios, there are often some scenarios that require the use of scheduled tasks, such as:
- Time-driven scenarios: sending coupons, sending text messages, etc. at a certain point in time.
- Batch data processing: batch statistics of last month's bills, statistics of last month's sales data, etc.
- Fixed frequency scenario: needs to be executed every 5 minutes.
In Java, traditional scheduled task implementation solutions, such as Timer, Quartz, etc., have more or less problems:
- No support for clusters, no statistics, no management platform, no failure alarms, no monitoring, etc.
And in the current distributed architecture, there are some scenarios that require distributed task scheduling:
- When tasks of multiple instances of the same service are mutually exclusive, unified scheduling is required.
- Task scheduling needs to support high availability, monitoring, and fault alarms.
- It is necessary to uniformly manage and track the results of task scheduling of each service node, and record and save task attribute information, etc.
Obviously, the traditional scheduled tasks are no longer suitable for the current distributed architecture, so a distributed task scheduling platform is needed. Currently, the mainstream ones are elasticjob and xxl-job.
2. Installation
Installed using docker
1. Pull the image
ocker pull xuxueli/xxl-job-admin:2.3.0
2. Create database
Pull tables_xxl_job.sql
the database table from the official website and throw it into your own mysql to get the database address
3. Create the container and run it
The configured -e PARAMS
parameters can refer to the official website , which is similar to the configuration in spring's .properties file.
- If you need to customize mysql and other configurations, you can specify it through "-e PARAMS", and the parameter format PARAMS="–key=value --key2=value2";
- Configuration item reference file: /xxl-job/xxl-job-admin/src/main/resources/application.properties
- If you need to customize JVM memory parameters and other configurations, you can specify it through "-e JAVA_OPTS". The parameter format JAVA_OPTS= "-Xmx512m";
//配置mysql参数
docker run -e PARAMS="--server.port=9051 --spring.datasource.url=jdbc:mysql://192.168.0.146:3306/xxl_job?Unicode=true&characterEncoding=UTF-8 --spring.datasource.username=root --spring.datasource.password=123456" -p 9051:9051 -v /tmp:/data/applogs --name xxl-job-admin -d xuxueli/xxl-job-admin:2.3.0
!Notice
- If mysql is also deployed on docker, do not write localhost or 127.0.0.1, but write ip
- username and password need to be the address of your database
- xxl_job is a database pulled from the official website. If the name is changed, it needs to be corresponding.
- And there should be no spaces such as line breaks in the middle of the configuration.
3. View containers and logs
C:\Users>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
2227fc303594 xuxueli/xxl-job-admin:2.3.0 "sh -c 'java -jar $J…" 17 minutes ago Up 17 minutes 0.0.0.0:9051->9051/tcp
xxl-job-admin
7f6c94285299 consul "docker-entrypoint.s…" 2 months ago Exited (255) 2 months ago 8300-8302/tcp, 8301-8302/udp, 8600/tcp, 8600/udp, 0.0.0.0:8500->8500/tcp
mysql8.0
740eb45a513d redis "docker-entrypoint.s…" 9 months ago Exited (0) 2 days ago
myredis
Check the startup log to see if there is any problem, and finally success
C:\Users>docker logs -t -f --tail 10f xxl-job-admin
2023-08-04T08:49:28.302253800Z
2023-08-04T08:49:28.302441500Z . ____ _ __ _ _
2023-08-04T08:49:28.302461700Z /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2023-08-04T08:49:28.302484200Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2023-08-04T08:49:28.302499100Z \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2023-08-04T08:49:28.302512600Z ' |____| .__|_| |_|_| |_\__, | / / / /
2023-08-04T08:49:28.302526000Z =========|_|==============|___/=/_/_/_/
2023-08-04T08:49:28.306031900Z :: Spring Boot :: (v2.4.2)
2023-08-04T08:49:28.306065400Z
2023-08-04T08:49:28.438761800Z 16:49:28.434 logback [main] INFO c.x.job.admin.XxlJobAdminApplication - Starting XxlJobAdminApplication using Java 1.8.0_265 on 2227fc303594 with PID 7 (/app.jar started by root in /)
2023-08-04T08:49:28.440836200Z 16:49:28.440 logback [main] INFO c.x.job.admin.XxlJobAdminApplication - No active profile set, falling back to default profiles: default
2023-08-04T08:49:31.210036000Z 16:49:31.209 logback [main] INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 9051 (http)
2023-08-04T08:49:31.226888300Z 16:49:31.226 logback [main] INFO o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-9051"]
2023-08-04T08:49:31.228067500Z 16:49:31.227 logback [main] INFO o.a.catalina.core.StandardService - Starting service [Tomcat]
2023-08-04T08:49:31.228668800Z 16:49:31.228 logback [main] INFO o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.41]
2023-08-04T08:49:31.302073600Z 16:49:31.301 logback [main] INFO o.a.c.c.C.[.[.[/xxl-job-admin] - Initializing Spring embedded WebApplicationContext
2023-08-04T08:49:31.302455100Z 16:49:31.302 logback [main] INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 2408 ms
2023-08-04T08:49:32.422937800Z 16:49:32.422 logback [main] INFO c.x.j.a.c.scheduler.XxlJobScheduler - >>>>>>>>> init xxl-job admin success.
2023-08-04T08:49:32.464778700Z 16:49:32.462 logback [xxl-job, admin JobRegistryMonitorHelper-registryMonitorThread] INFO com.zaxxer.hikari.HikariDataSource - HikariCP - Starting...
2023-08-04T08:49:32.712904100Z 16:49:32.712 logback [main] INFO o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
2023-08-04T08:49:32.931666500Z 16:49:32.931 logback [main] INFO o.s.b.a.w.s.WelcomePageHandlerMapping - Adding welcome page template: index
2023-08-04T08:49:33.198892600Z 16:49:33.198 logback [xxl-job, admin JobRegistryMonitorHelper-registryMonitorThread] INFO com.zaxxer.hikari.HikariDataSource - HikariCP - Start completed.
2023-08-04T08:49:33.854065100Z 16:49:33.853 logback [main] INFO o.s.b.a.e.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'
2023-08-04T08:49:33.916699800Z 16:49:33.916 logback [main] INFO o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-9051"]
2023-08-04T08:49:33.949473300Z 16:49:33.948 logback [main] INFO o.a.c.c.C.[.[.[/xxl-job-admin] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-08-04T08:49:33.949971100Z 16:49:33.949 logback [main] INFO o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2023-08-04T08:49:33.953091200Z 16:49:33.952 logback [main] INFO o.s.web.servlet.DispatcherServlet - Completed initialization in 2 ms
2023-08-04T08:49:33.956208900Z 16:49:33.955 logback [main] INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 9051 (http) with context path '/xxl-job-admin'
2023-08-04T08:49:33.987836700Z 16:49:33.986 logback [main] INFO c.x.job.admin.XxlJobAdminApplication - Started XxlJobAdminApplication in 6.262 seconds (JVM running for 7.286)
2023-08-04T08:49:37.002114700Z 16:49:37.001 logback [xxl-job, admin JobScheduleHelper#scheduleThread] INFO c.x.j.a.c.thread.JobScheduleHelper - >>>>>>>>> init xxl-job admin scheduler success.
4. Open the web page 127.0.0.1:9051/xxl-job-admin/
Default account/password: admin/123456
3. Implement registration testing
1. Create a SpringBoot project and add dependencies.
- Name it as you like
and add dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency><!-- 官网的demo是2.2.1,中央maven仓库还没有,所以就用2.2.0 -->
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2. Add configuration
2.1. Add yml
server:
port: 8080
spring:
application:
name: xxl-test01
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mycinema?serverTimezone=Asia/Shanghai
username: root
password: 123456
xxl:
job:
admin:
### 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
addresses: http://localhost:9051/xxl-job-admin
executor:
### 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
appname: xxl-test01
### 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
ip: 192.168.0.146
### 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
port: 3333
### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
logPath: logs/jeecg/job/jobhandler/
logRetentionDays: 30
2.2. Add configuration class for configuring beans
/**
* @author zhengfuping
* @version 1.0
* @description: TODO 执行配置,注入到bean容器
*/
@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;
}
}
2.3. Add execution timing Handler
/**
* @author zhengfuping
* @version 1.0
* @description: TODO 具体执行测试demo
*/
@Component
@Slf4j
public class XxlJobDemoHandler {
/**
* Bean模式,一个方法为一个任务
*/
@XxlJob("demoJobHandler")
public ReturnT<String> demoJobHandler(String param) throws Exception {
log.info("java, Hello World~~~");
log.info("获取参数:",param);
log.info("----xxl执行成功-----");
return ReturnT.SUCCESS;
}
}
3. Configure the client
- Configure the executor and
appname
need to be consistent with yml
- configuration tasks
- After the configuration is completed, you need to start it manually. You can view the execution log or execute it manually.