springboot集成xx-job;

概念理解:

xx-job是一个分布式任务调度平台。比如你有AB两个项目。

AB的定时任务就要在xx-job上个注册。同时AB要配置对应的依赖。

所以集成xx-job要分2步骤:第一步:先搭建xx-job服务

第二步,在A项目中导包并引用。

第一步:搭建xx-job服务

0.下载jar

地址:

Xxl-job项目地址:
GitHub地址:https://github.com/xuxueli/xxl-job
Gitee地址:https://gitee.com/xuxueli0323/xxl-job

1导入idea

2执行数据库脚本。创建数据库

 

3修改项目配置文件

 4启动xxjobadmin

5访问地址

http://localhost:8080/xxl-job-admin/

用户名admin 密码123456

第二步:A项目(springboot项目)基本xx-job;

2.1导包:

  <!-- SpringBoot集成Xxl-Job -->
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>2.3.0</version>
        </dependency>

2.2创建配置类

package com.example.springbootjdbc.config;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: XxlJobConfig
 * @Description: xxl-job依赖配置
 * @author:
 * @date: 2022年12月07日 08:37
 * @version: 1.0
 */
@Configuration  //是否开启xxl-job定时任务,注释掉 //@Configuration 则不开启定时任务
@Data
@Slf4j
public class XxlJobConfig {

    @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() {
        XxlJobHelper.log(">>>>>>>>>>> xxl-job config init.>>>>>>>>>>>");
        System.out.println("=============== 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修改yml文件 application.yml

# Xxl-Job分布式定时任务调度中心
xxl:
  job:
    admin:
      # 调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。
      addresses: http://localhost:8080/xxl-job-admin
      # addresses: http://192.168.110.2:9090/xxl-job-admin
    # 执行器通讯TOKEN [选填]:非空时启用 系统默认 default_token
    accessToken: default_token
    executor:
      # 执行器的应用名称
      appname: mls-xxl-job
      # 执行器注册 [选填]:优先使用该配置作为注册地址
      address: ""
      # 执行器IP [选填]:默认为空表示自动获取IP
      ip: ""
      # 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999
      port: 9999
      # 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
      logpath: D:\Codes\logs
      #logpath: /data/logs/mls/job
      # 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
      logretentiondays: 7

2.4编写测试

package com.example.springbootjdbc.controller.job;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @ClassName: XxlJobTest
 * @Description: XxlJobTest
 * @author:
 * @date: 2022年12月07日 12:58
 * @version: 1.0
 */
@Slf4j
@Component
public class XxlJobTest {

    @XxlJob("xxlJobTest")
    public ReturnT<String> xxlJobTest(String date) {
//        log.info("---------xxlJobTest定时任务执行成功--------");ss
        XxlJobHelper.log("xxlJobTest定时任务执行成功");
        return ReturnT.SUCCESS;
    }

}

2.5添加执行器

2.6添加任务

 

 2.7启动项目后执行测试并查看日志

 2.9启动项目后执行测试并查看日志后在查看执行日志

### 集成 ElasticJob 3 到 Spring Boot 3 的指南 #### 背景介绍 ElasticJob 是阿里巴巴开源的一个分布式调度解决方案,支持作业分片、弹性扩容以及高可用等功能。Spring Boot 提供了一种现代化的方式来构建 Java 应用程序,并通过自动配置简化了开发流程。为了在 Spring Boot 3 中集成 ElasticJob 3 并完成部署,以下是详细的指导。 --- #### 添加依赖项 首先,在 `pom.xml` 文件中引入必要的依赖项来支持 ElasticJob 和 Spring Boot 的集成: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- ElasticJob-Lite for standalone job scheduling --> <dependency> <groupId>com.dangdang</groupId> <artifactId>elastic-job-lite-spring-boot-starter</artifactId> <version>3.x.x</version> <!-- Ensure compatibility with Spring Boot 3 --> </dependency> <!-- Zookeeper client dependency, required by ElasticJob --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>5.x.x</version> <!-- Compatible version --> </dependency> </dependencies> ``` 注意:版本号应根据官方文档确认兼容性[^1]。 --- #### 配置文件设置 编辑 `application.yml` 或 `application.properties` 来定义 ElasticJob 所需的基础配置,例如注册中心(Zookeeper)、作业属性等。 ```yaml spring: application: name: elasticjob-demo elasticjob: lite: registry-center: server-lists: localhost:2181 # Zookeeper 地址 namespace: elasticjob-namespace # 命名空间 jobs: demo-job: class-name: com.example.ElasticJobDemoTask # 自定义任务类路径 cron: "0/5 * * * * ?" # 定时表达式 sharding-total-count: 3 # 分片总数 overwrite: true # 是否覆盖已有配置 ``` 上述配置指定了一个名为 `demo-job` 的定时任务,每 5 秒执行一次,并将其分为三个分片[^2]。 --- #### 创建自定义任务类 实现具体的业务逻辑需要创建一个继承自 `SimpleJob` 接口的任务类。以下是一个简单的例子: ```java import com.dangdang.ddframe.job.api.simple.SimpleJob; import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter; public class ElasticJobDemoTask implements SimpleJob { @Override public void execute(CoordinatorRegistryCenter coordinatorRegistryCenter) { System.out.println("Executing Elastic Job Task..."); // Add business logic here } } ``` 此代码片段展示了如何编写一个基本的作业处理逻辑[^3]。 --- #### 启动应用程序 确保所有的依赖已正确加载后,可以通过运行主类启动应用。如果一切正常,您应该能够在日志中看到类似如下消息: ``` INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) INFO c.e.demo.DemoApplication - Started DemoApplication in X.XX seconds... ``` 此时,您的 Spring Boot 应用已经成功集成了 ElasticJob,并按照设定的时间表触发任务。 --- #### 测试与验证 建议使用工具如 JConsole 或者 Grafana 监控作业的状态和性能指标。此外,也可以手动检查 Zookeeper 注册节点是否存在对应的信息以验证集成是否成功[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值