springboot+maven实现多环境动态切换yml配置及配置文件拆分

配置多环境yml

1.在springboot工程的src/main/resource目录下,创建application.yml文件。(默认应该有个applicaton.properties文件,也可以配置多环境。但这里我们使用yml格式的配置文件)。

# 默认使用配置
spring:
  profiles:
    active: dev

# 公共配置
  jpa:
    database: MYSQL
  datasource:
    name: mysql
    type: com.alibaba.druid.pool.DruidDataSource
    #druid相关配置
    druid:
      #监控统计拦截的filters
      filters: stat
      driver-class-name: com.mysql.jdbc.Driver
      #配置初始化大小/最小/最大
      initial-size: 1
      min-idle: 1
      max-active: 20
      #获取连接等待超时时间
      max-wait: 60000
      #间隔多久进行一次检测,检测需要关闭的空闲连接
      time-between-eviction-runs-millis: 60000
      #一个连接在池中最小生存的时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20

# 配置端口
server:
  port: 8080

---

# 开发配置
spring:
  profiles: dev

  datasource:
    druid:
      url: jdbc:mysql://127.0.0.1:3306/dc_async_service?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      username: root
      password: root

---
# 生产配置
spring:
  profiles: release

  datasource:
    druid:
      url: jdbc:mysql://xxxxxxx:3306/dc_async_service?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      username: xxxxx
      password: xxxxx

这里,我们对开发配置和生产环境做了配置,上面的配置是公共配置,下面我们分别配置了开发和生产的配置。如果spring.profiles.active没有指定值,那么只会加载通用的配置。

2. 启动配置选择

工程打成jar包后,我们可以在运行的时候对配置进行选择,而不需要每次打包前都手动修改spring.profiles.active的值。

例如在生产环境,我们可以使用release配置执行jar包,命令如下:

java -jar xxx.jar --spring.profiles.active=release

将yml文件拆分

为了更方便和维护各环境的配置,我们可以将yml文件拆分。在src/main/resource目录下,再创建yml配置文件,命名规则为application-{profiles}.yml,命名如

application-dev.yml

application-release.yml

然后,将原来application.yml中的dev、release配置分别移到两个配置文件中,同时可以去掉spring.profiles属性,因为spring会自动按照命名规则寻找对应的配置文件。

例如:

application-dev.yml

# 开发配置
spring:
  datasource:
    druid:
      url: jdbc:mysql://127.0.0.1:3306/dc_async_service?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      username: root
      password: root

application-release.yml

# 生产配置
spring:
  datasource:
    druid:
      url: jdbc:mysql://xxxxxxx:3306/dc_async_service?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      username: xxxxx
      password: xxxxx

这样,我们的yml文件就拆分无成了,效果和之前的是一样的。

用maven控制默认配置

之前,我们将项目打包后,在运行的时候需要指定配置,能不能在打包的时候,自动改变spring.profiles.active的激活配置,这样直接通过java -jar xxx.jar即可运行项目。需要使用maven-resources-plugin插件。

1、在pom.xml添加如下配置

<profiles>
    <!-- 开发环境 -->
    <profile>
        <id>dev</id>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!-- 生产环境 -->
    <profile>
        <id>release</id>
        <properties>
            <spring.profiles.active>release</spring.profiles.active>
        </properties>
    </profile>
</profiles>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <executions>
        <execution>
            <id>default-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>target/classes</outputDirectory>
                <useDefaultDelimiters>false</useDefaultDelimiters>
                <delimiters>
                    <delimiter>#</delimiter>
                </delimiters>
                <resources>
                    <resource>
                        <directory>src/main/resources/</directory>
                        <filtering>true</filtering>
                    </resource>
                    <resource>
                        <directory>src/main/resources.${spring.profiles.active}</directory>
                        <filtering>false</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

这里<delimiter>#</delimiter>用来增加一个点位符,maven本身有点位符${xxx},但这个点位符被springboot占用了,所以我们就再定义一个。<filtering>true</filtering>表示打开过滤器,这样application.yml文件中的#spring.profiles.active#部分就会替换为pom.xml里的profiles中定义的spring.profiles.active变量值。

最后,将application.yml的spring.profiles.active的值改为#spring.profiles.active#

# 默认使用配置
spring:
  profiles:
    active: #spring.profiles.active#

这样,在用maven打包的时候,使用mvn package -P release 打包,最后打包后的文件中,application.yml中的spring.profiles.active的值就是release。这样直接运行java -jar xxx.jar就是生产环境的配置了。

猜你喜欢

转载自blog.csdn.net/CHS007chs/article/details/86591140