xxl-job接入springboot项目
为了在Spring Boot项目中接入XXL-JOB,你需要按照以下步骤操作:
- 在
pom.xml
中添加XXL-JOB客户端依赖:
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>你的版本号</version>
</dependency>
- 在
application.properties
或application.yml
中配置XXL-JOB:
# xxl-job admin address
xxl.job.admin.addresses=http://xxl-job-admin-address
# xxl-job executor appname
xxl.job.executor.appname=your-springboot-project
# xxl-job executor ip
xxl.job.executor.ip=
# xxl-job executor port
xxl.job.executor.port=-1
# xxl-job access token
xxl.job.accessToken=
# xxl-job executor logpath
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
# xxl-job executor logretentiondays
xxl.job.executor.logretentiondays=
- 创建一个配置类,配置XXL-JOB的执行器:
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 static final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appName;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppName(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
评论已关闭