目錄:
- 技術背景
- 實現方式
1.技術背景
項目的某個微服務實現了A、B、C三種相近的功能,所以代碼在一個服務中,但是部署打包的時候想各自單獨為service-a.jar,service-b.jar、service-c.jar。
服務注冊到注冊中心有各自的名稱,每個功能的實現需要不同的配置,application-a.yml,application-b.yml,application-c.yml,需要激活不同的配置文件。日志寫入
到不同的文件中,service-a.log、service-b.log、service-c.log。鑒於以上需要實現的技術為:
1.動態的應用名稱:根據環境變量實現不同的應用名稱
2.動態的配置文件:根據maven @profile.active@實現
3.動態的jar包名稱:結合上一步實現修改名稱
2.實現方式
2.1動態的應用名稱
spring: application: #根據環境變量修改應用名稱 all為默認的變量 name: aicloud-record-${PROFILE_ACTIVE:all}
本地啟動可以在啟動類配置中修改
在服務器環境中導出環境變量
export PROFILE_ACTIVE:aaa
2.動態的配置文件
配置文件
spring: profiles: active: @profile.active@
pom文件
<profiles> <profile> <id>all</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profile.active>all</profile.active> </properties> </profile> <profile> <id>asr</id> <properties> <profile.active>asr</profile.active> </properties> </profile> </profiles>
本地啟動時,maven選中選項
服務器上
mvn clean install -Pall 。。。
3.動態的jar包名稱
這一步是在上一步的基礎上稍作修改就好
<build> <!--# 根據profile修改jar包名稱 ${profile.active}--> <finalName>aicloud-record-biz-${profile.active}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> </plugin> </plugins> </build>