在一个企业级系统中,我们可能会遇到这样一个问题:开发时使用开发环境,测试时使用测试环境,上线时使用生产环境。每个环境的配置都可能不一样,比如开发环境的数据库是本地地址,而测试环境的数据库是测试地址。
实际开发和打包部署如何更高效呢?
手动管理
每次编译之前手动把所有配置信息修改成当前运行的环境信息。这种方式导致每次都需要修改,相当麻烦,也容易出错。
maven管理
利用 Maven在 pom.xml 里配置多个环境,每次编译之前将 settings.xml 里面修改成当前要编译的环境 ID。这种方式会事先设置好所有环境,缺点就是每次也需要手动指定环境,如果环境指定错误,发布时是不知道的。
一般来说我们都会在src/main/resources 目录下面建一个文件夹,里面分别放三个环境对应的文件dev、pre、pro,如下:
jdbc.properties
jdbc.driver=${jdbc_driver}
jdbc.url=${jdbc_url}
jdbc.username=${jdbc_username}
jdbc.password=${jdbc_password}
filter-dev-env.properties
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/dev
jdbc_username=root
jdbc_password=123456
其他文件类似,然后在pom里面进行设置了三个环境对应的变量:
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<!-- 默认是本地开发环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>pro</id>
<properties>
<profiles.active>pro</profiles.active>
</properties>
</profile>
</profiles>
<build>
<!-- maven模块化的话最好从父类继成取,打成包的命名 -->
<finalName>${artifactId}-${version}</finalName>
<!-- 使用指定的filter进行过滤,在执行mvn命令的时候带上-Ppro就代表生产环境,就会加载生产环境的properties,-Pdev就代表开发环境(默认) -->
<filters>
<filter>src/main/resources/multiEnv/filter-${profiles.active}-env.properties</filter>
</filters>
<!-- 配置需要被替换的资源文件路径, jdbc.properties -->
<resources>
<resource>
<!--
资源文件位置src/main/resources/,这下面的资源文件的${}会全部被替换成filter中的标签内容。
directory指定的value会作为classes的资源跟目录,
比如指定:src/main/resources/,则classes下会出现jdbc等包,
若指定:src/main/resources/jdbc/,则classes下直接出现jdbc包下的文件,不会额外出现jdbc等其他包结构。因为他把jdbc作为了根目录
-->
<directory>src/main/resources/</directory>
<!-- 在某个resource中如果设置filtering为true,将会根据输入参数动态修改相关内容。 -->
<filtering>true</filtering>
<!-- 排除标签 -->
<excludes>
<!--
exclude可以排除指定文件,支持通配符 ,匹配项不会生成到classes目录下,路径是以directory开始的
在这里就是directory(src/main/resources/)/multiEnv/filter-*-env.properties
-->
<exclude>multiEnv/filter-*-env.properties</exclude>
<!-- **/*.xml 代表 directory(src/main/resources/)目录以及所有子目录的xml文件-->
<!--
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
-->
</excludes>
<!-- 包含标签 -->
<!--
<includes>
<include></include>
</includes>
-->
</resource>
</resources>
</build>
打包运行
打本地开发环境包:
mvn clean package -Pdev
打部署上线环境包:
mvn clean package -Ppro
打测试环境包:
mvn clean package -Ptest
上面配置文件随着打包不同内容不同,会将filter-dev-env.properties、filter-pro-env.properties、filter-test-env.properties三个文件的内容根据不同环境将内容注入jdbc.properties。
spring管理
首先,创建 application.yml 文件,在里面添加如下内容:
spring:
profiles:
active: dev
含义是指定当前项目的默认环境为 dev,即项目启动时如果不指定任何环境,Spring Boot 会自动从 dev 环境文件中读取配置信息。我们可以将不同环境都共同的配置信息写到这个文件中。
然后创建多环境配置文件,文件名的格式为:application-{profile}.yml,其中,{profile} 替换为环境名字,如 application-dev.yml,我们可以在其中添加当前环境的配置信息,如添加数据源:
spring:
datasource:
url: jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useSSL=true
username: root
password: root
driverClassName: com.mysql.jdbc.Drive
这样,我们就实现了多环境的配置,每次编译打包我们无需修改任何东西,编译为 jar 文件后,运行命令:
java -jar api.jar --spring.profiles.active=dev
其中 –spring.profiles.active 就是我们要指定的环境。
参考或转载
1
-
个人笔记中移过来详情不清晰且无地址无法发布转载,麻烦知情者留言修改发布
↩︎