当前位置: 代码迷 >> 综合 >> ElasticJob‐Lite:整合Spring Boot
  详细解决方案

ElasticJob‐Lite:整合Spring Boot

热度:78   发布时间:2023-12-01 17:48:37.0

ElasticJob‐LiteSpring Boot Starter集成了TracingConfiguration自动配置,开发者只需注册一个DataSourceSpring容器中,并在配置文件指定事件追踪数据源类型,Starter就会自动创建一个TracingConfiguration实例并注册到Spring容器中。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.kaven</groupId><artifactId>job</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.1.RELEASE</version></parent><dependencies><dependency><groupId>org.apache.shardingsphere.elasticjob</groupId><artifactId>elasticjob-lite-spring-boot-starter</artifactId><version>3.0.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>

引入spring‐boot‐starter‐jdbc依赖,会自动注册数据源。

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency>

application.yml

spring:# 数据源datasource:driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: ITkaven@666.comurl: "jdbc:mysql://localhost:3306/trace?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"elasticjob:# 事件追踪tracing:type: RDB# 注册中心reg-center:server-lists: "192.168.31.172:9000"namespace: "my-job"connection-timeout-milliseconds: 40000max-retries: 5# 任务配置,类型为Map<String, ElasticJobConfigurationProperties>jobs:MySimpleJob:elasticJobClass: com.kaven.job.MySimpleJobshardingTotalCount: 3cron: "30 * * * * ?"description: "该作业有三个分片,每隔一分钟执行一次"

作业定义(要加上@Component注解,不然Spring Boot不能感知这个任务定义):

package com.kaven.job;import lombok.SneakyThrows;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;
import java.util.Date;/*** @Author: ITKaven* @Date: 2021/11/20 17:02* @Blog: https://kaven.blog.csdn.net* @Leetcode: https://leetcode-cn.com/u/kavenit* @Notes:*/@Component
public class MySimpleJob implements SimpleJob {
    private static final SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@SneakyThrows@Overridepublic void execute(ShardingContext shardingContext) {
    switch (shardingContext.getShardingItem()) {
    case 0:System.out.println(formatter.format(new Date()) + " : ShardingItem[0]");Thread.sleep(2000);break;case 1:System.out.println(formatter.format(new Date()) + " : ShardingItem[1]");Thread.sleep(2000);break;case 2:System.out.println(formatter.format(new Date()) + " : ShardingItem[2]");Thread.sleep(2000);break;default:System.out.println(formatter.format(new Date()) + " : Unknown ShardingItem");}}
}

启动类Server

package com.kaven.job;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @Author: ITKaven* @Date: 2021/12/16 20:21* @Blog: https://kaven.blog.csdn.net* @Leetcode: https://leetcode-cn.com/u/kavenit* @Notes:*/@SpringBootApplication
public class Server {
    public static void main(String[] args) {
    SpringApplication.run(Server.class);}
}

输出如下图所示:
在这里插入图片描述
数据库中也有作业执行记录。
在这里插入图片描述
ElasticJob‐Lite整合Spring Boot就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。

  相关解决方案