1、pom.xml文件配置
<profiles><profile><id>dev</id><properties><!-- 本地开发环境 --><surroundings>dev</surroundings></properties><activation><!-- 默认的,不加参数时执行这个profile --><activeByDefault>true</activeByDefault></activation></profile><profile><id>test</id><properties><!-- 测试环境 --><surroundings>test</surroundings></properties></profile><profile><id>pro</id><properties><!-- 生产环境 --><surroundings>pro</surroundings></properties></profile>
</profiles><build><resources><!-- Resource Filter --><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><!--指定资源的位置--><resource><filtering>true</filtering><directory>src/main/resources</directory><excludes><!-- 存在的环境配置,如果不写那么就会把没写配置也打包进去 --><exclude>bootstrap-dev.yml</exclude><exclude>bootstrap-test.yml</exclude><exclude>bootstrap-prod.yml</exclude></excludes></resource><resource><filtering>true</filtering><directory>src/main/resources</directory><includes><!-- 根据打包命令选择配置后缀 --><include>bootstrap-${surroundings}.yml</include></includes></resource></resources><!-- build中的其他配置 --><!-- ... --></build>
2、bootstrap.yml配置
spring:profiles:# 注意这里一定要带有英文单引号active: '@surroundings@'
随后创建bootstrap-dev.yml、bootstrap-test.yml、bootstrap-prod.yml配置文件,文件内容按需配置
spring:cloud:nacos:discovery:server-addr: 192.168.1.8:8848enabled: trueregister-enabled: truenamespace: fawu_devgroup: DEFAULT_GROUPconfig:server-addr: 192.168.1.8:8848extension-configs[0]:data-id: fawu-public.ymlgroup: DEFAULT_GROUPrefresh: trueextension-configs[1]:data-id: fawu-gateway.ymlgroup: DEFAULT_GROUPrefresh: truefile-extension: ymlnamespace: fawu_devgroup: DEFAULT_GROUP
3、打包运行:
使用maven默认的package命令打包即可,打包后的jar包运行命令如下
java -jar -Dspring.profiles.active=prod -Dfile.encoding=utf-8 gateway-0.0.1-SNAPSHOT.jar
-Dspring.profiles.active=prod 选择运行环境
-Dfile.encoding=utf-8 设置字符编码