XXL-JOB--SpringBoot--使⽤教程实例原⽂⽹址:
简介
说明
官⽹上的教程已经超级详细了。本处写⼀个⽰例来验证它的⽤法。
需求:微服务启动2个实例,这两个实例以负载均衡的⽅式运⾏同⼀个定时任务。
官⽹
建库建表
下载项⽬源码并解压,获取 “调度数据库初始化SQL脚本” 并执⾏即可。
下载地址:
部署“调度中⼼”
打开项⽬
springboot结构
把上边下载的项⽬源码并解压,按照maven格式将源码导⼊IDE, 使⽤maven进⾏编译即可。源码结构如下:
xxl-job-admin:调度中⼼
xxl-job-core:公共依赖
xxl-job-executor-samples:执⾏器Sample⽰例(选择合适的版本执⾏器,可直接使⽤,也可以参考其并将现有项⽬改造成执⾏器):xxl-job-executor-sample-springboot:Springboot版本,通过Springboot管理执⾏器,推荐这种⽅式;
:xxl-job-executor-sample-spring:Spring版本,通过Spring容器管理执⾏器,⽐较通⽤;
:xxl-job-executor-sample-frameless:⽆框架版本;
修改配置⽂件
/xxl-job/xxl-job-admin/src/main/resources/application.properties //改动部分已经加粗
### web
server.port=8080
t-path=/xxl-job-admin
### actuator
management.t-path=/actuator
management.abled=false
### resources
spring.mvc.servlet.load-on-startup=0
spring.mvc.static-path-pattern=/static/**
### freemarker
plateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
quest-context-attribute=request
spring.freemarker.settings.number_format=0.>>
### mybatis
mybatis.mapper-locations=classpath:/mybatis-mapper/*l
#l.odel
### xxl-job, datasource
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxl_job?
useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=222333
spring.datasource.sql.cj.jdbc.Driver
### datasource-pool
pe=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=10
spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=HikariCP
spring.datasource.hikari.max-lifetime=900000
spring.tion-timeout=10000
spring.tion-test-query=SELECT 1
### xxl-job, email
spring.mail.host=smtp.qq
spring.mail.port=25
spring.mail.username=xxx@qq
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.able=true
spring.mail.properties.mail.quired=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.ssl.SSLSocketFactory
### xxl-job, access token
xxl.job.accessToken=
### xxl-job, i18n (default is zh_CN, and you can choose "zh_CN", "zh_TC" and "en")
xxl.job.i18n=zh_CN
## xxl-job, triggerpool max size
iggerpool.fast.max=200
iggerpool.slow.max=100
### xxl-job, log retention days
xxl.job.logretentiondays=30
部署“执⾏器(业务微服务)”
参照上边源码⾥的
配置及依赖
server:
# port: 8082
port: 8083
xxl:
job:
### xxl-job, access token
accessToken:
admin:
### xxl-job admin address list, such as "address" or "address01,address02" addresses: 127.0.0.1:8080/xxl-job-admin
executor:
# port: 9998
port: 9997
address:
### xxl-job executor appname
appname: xxl-job-executor-sample2
### xxl-job executor server-info
ip:
### xxl-job executor log-path
logpath: /data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
logretentiondays: 30
## log config
#logging:
# config: l
#server:
# port: 8081
#
#xxl:
# job:
# ### xxl-job, access token
# accessToken:
# admin:
# ### xxl-job admin address list, such as "address" or "address01,address02" # addresses: 127.0.0.1:8080/xxl-job-admin
# executor:
# port: 9999
# address:
# ### xxl-job executor appname
# appname: xxl-job-executor-sample
# ### xxl-job executor server-info
# ip:
# ### xxl-job executor log-path
# logpath: /data/applogs/xxl-job/jobhandler
# ### xxl-job executor log-retention-days
# logretentiondays: 30
## log config
#logging:
# config: l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId&le</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置类
fig;
xecutor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import t.annotation.Bean;
import t.annotation.Configuration;
/**
* xxl-job config
*
* @author xuxueli 2017-04-28
*/
@Configuration
public class XxlJobConfig {
private Logger logger = Logger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${utor.appname}")
private String appname;
@Value("${utor.address}")
private String address;
@Value("${utor.ip}")
private String ip;
@Value("${utor.port}")
private int port;
@Value("${utor.logpath}")
private String logPath;
@Value("${utor.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>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论