亲宝软件园·资讯

展开

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

我有一只肥螳螂 人气:0

部署xxl-job

K8S 部署 xxl-job 参考文档:https:

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

执行器管理

点击 “新增”

任务管理

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

编辑任务状态

加载全部内容

相关教程
猜你喜欢
用户评论