本文将详细介绍如何使用Spring Boot ElasticJob进行配置覆盖。
一、目录结构
我们需要准备两个目录,分别是“elastic-job-lite-spring-boot-starter-example”和“elastic-job-lite-spring-boot-starter-example-override”。
├── elastic-job-lite-spring-boot-starter-example
│ ├── src
│ └── pom.xml
└── elastic-job-lite-spring-boot-starter-example-override
├── src
└── pom.xml
二、使用Spring Boot ElasticJob配置覆盖
1、使用父模块
首先,在“elastic-job-lite-spring-boot-starter-example”项目的pom.xml文件中添加父模块。
<parent>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-root</artifactId>
<version>${elastic-job.version}</version>
<relativePath/>
</parent>
然后,添加Spring Boot Starter依赖。
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring-boot-starter</artifactId>
<version>${elastic-job.version}</version>
</dependency>
接着,我们需要创建一个简单的任务,例如:
@Component
public class MySimpleJob implements SimpleJob {
@Override
public void execute(ShardingContext context) {
System.out.println(String.format("------Thread ID: %s, 任务总片数: %s, " +
"当前分片项: %s.当前参数: %s," +
"当前任务名称: %s.当前任务参数: %s",
Thread.currentThread().getId(),
context.getShardingTotalCount(),
context.getShardingItem(),
context.getShardingParameter(),
context.getTaskName(),
context.getJobParameter()));
}
}
2、使用子模块
现在,在“elastic-job-lite-spring-boot-starter-example-override”项目的pom.xml文件中,我们需要重写默认的配置。
<parent>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-root</artifactId>
<version>${elastic-job.version}</version>
<relativePath/>
</parent>
<artifactId>elastic-job-lite-spring-boot-starter-example-override</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot-starter.version>2.4.5</spring-boot-starter.version>
</properties>
<dependencies>
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>elastic-job-lite-spring-boot-starter</artifactId>
<version>${elastic-job.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<!-- 自定义profile,名称为dev -->
<profile>
<id>dev</id>
<build>
<finalName>elastic-job-lite-spring-boot-starter-example-override-dev</finalName>
</build>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<!-- 自定义profile,名称为test -->
<profile>
<id>test</id>
<build>
<finalName>elastic-job-lite-spring-boot-starter-example-override-test</finalName>
</build>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
</profiles>
可以看到,我们添加了Spring Boot Test依赖,以及两个profiles。
3、配置覆盖
现在,我们需要在“elastic-job-lite-spring-boot-starter-example-override”目录下创建“application-dev.properties”。
elasticjob.zk.serverLists=localhost:2181 elasticjob.zk.namespace=my-namespace
这里,我们只覆盖了ZooKeeper的地址和命名空间。
同样地,我们也需要在“elastic-job-lite-spring-boot-starter-example-override”目录下创建“application-test.properties”。
elasticjob.zk.serverLists=localhost:2181 elasticjob.zk.namespace=my-test-namespace
这里,我们覆盖了ZooKeeper的地址,并且使用了一个不同的命名空间。
三、运行示例项目
最后,我们可以在两个没目录下运行示例项目。
1、在“elastic-job-lite-spring-boot-starter-example”目录下运行
java -jar target/elastic-job-lite-spring-boot-starter-example-${project.version}.jar当我们在浏览器中访问“http://localhost:8080/job/initSimpleJob”时,我们将会看到如下的结果。
------Thread ID: 38, 任务总片数: 3, 当前分片项: 1.当前参数: a, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob ------Thread ID: 39, 任务总片数: 3, 当前分片项: 2.当前参数: b, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob ------Thread ID: 48, 任务总片数: 3, 当前分片项: 0.当前参数: c, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob
2、在“elastic-job-lite-spring-boot-starter-example-override”目录下运行
运行dev profile
java -jar -Dspring.profiles.active=dev target/elastic-job-lite-spring-boot-starter-example-override-${project.version}-dev.jar当我们在浏览器中访问“http://localhost:18080/job/initSimpleJob”时,我们将会看到如下的结果。
------Thread ID: 30, 任务总片数: 3, 当前分片项: 0.当前参数: c, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob ------Thread ID: 32, 任务总片数: 3, 当前分片项: 1.当前参数: a, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob ------Thread ID: 36, 任务总片数: 3, 当前分片项: 2.当前参数: b, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob
运行test profile
java -jar -Dspring.profiles.active=test target/elastic-job-lite-spring-boot-starter-example-override-${project.version}-test.jar当我们在浏览器中访问“http://localhost:8090/job/initSimpleJob”时,我们将会看到如下的结果。
------Thread ID: 39, 任务总片数: 3, 当前分片项: 0.当前参数: a, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob ------Thread ID: 42, 任务总片数: 3, 当前分片项: 2.当前参数: b, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob ------Thread ID: 43, 任务总片数: 3, 当前分片项: 1.当前参数: c, 当前任务名称: initSimpleJob.当前任务参数: mySimpleJob
四、总结
本文介绍了如何使用Spring Boot ElasticJob进行配置覆盖,通过运行示例项目,我们可以看到不同的配置被覆盖。
原创文章,作者:FINBS,如若转载,请注明出处:https://www.506064.com/n/374993.html
微信扫一扫
支付宝扫一扫