本文將詳細介紹如何使用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/zh-hant/n/374993.html