java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot部署xxl-job

SpringBoot部署xxl-job方法详细讲解

作者:我有一只肥螳螂

XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展,这篇文章主要介绍了springboot整合xxl-job流程,需要的朋友可以参考下

部署xxl-job

K8S 部署 xxl-job 参考文档:https://www.jb51.net/article/252770.htm

SpringBoot配置

maven 配置

pom 文件添加依赖

<!--xxl-job依赖-->
<dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.3.0</version>
</dependency>

application.yaml

添加参数

xxl:
  job:
    admin:
      addresses: http://192.168.140.1:8080/xxl-job-admin
    executor:
      appname: demo-feng-executor
      ip:
      port: 9997
      logpath: /home/applogs/xxl-job/jobhandler
      logretentiondays: -1
    accessToken:

配置 XxlJobConfig

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.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
    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);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }
}

XxlJobSpringExecutor

这里聊一下 XxlJobSpringExecutor,不想多了解的可以跳过

afterSingletonsInstantiated 函数

public void afterSingletonsInstantiated() {
    this.initJobHandlerMethodRepository(applicationContext);
    GlueFactory.refreshInstance(1);
    try {
        super.start();
    } catch (Exception var2) {
        throw new RuntimeException(var2);
    }
}

start函数

public void start() throws Exception {
     XxlJobFileAppender.initLogPath(this.logPath);
     this.initAdminBizList(this.adminAddresses, this.accessToken);
     JobLogFileCleanThread.getInstance().start((long)this.logRetentionDays);
     TriggerCallbackThread.getInstance().start();
     this.initEmbedServer(this.address, this.ip, this.port, this.appname, this.accessToken);
 }

新建执行任务

@Slf4j
@Component
public class MyTask {
    @XxlJob("fengDemoTask")
    private void task() {
        log.info("成功调用demo任务");
    }
}

配置xxl-job-admin

执行器管理

点击 “新增”

任务管理

选择刚刚执行器,点击 “新增”

编辑任务状态

到此这篇关于SpringBoot部署xxl-job方法详细讲解的文章就介绍到这了,更多相关SpringBoot部署xxl-job内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文