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

版权声明:本文为博主原创文章,转载时请注明出处,谢谢!喝酒不骑马 邮箱[email protected] https://blog.csdn.net/Colton_Null/article/details/82145467

一.引言

在实际的的开发中,对于一个工程,经常会有多种环境配置,例如开发环境、测试环境、生产环境等。在不同的环境下,配置有可能是不一样的,比如接口地址、数据库连接配置等。为了避免频繁的修改配置文件,我们想要简便地切换各种环境配置。好在SpringBoot提供了这样的功能,可以很方便地切换不同场景下的配置。

本文就来讲解如果在SpringBoot项目中动态切换配置,以及用Maven控制配置的选择。

二.配置多环境yml

1.配置多环境yml

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

接下来,做一个对于数据的简单配置。yml配置如下。

application.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表示配置的名称,spring.profiles.active表示要激活的环境,值和要切换的spring.profiles名称一致。上面这个文件,默认激活的就是dev开发配置。

如果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即可运行项目。而且我司项目上线采用自研运维系统,默认是直接执行jar、war包的,不能在启动时选择配置,所以在打包时如果能自动将spring.profiles.active配置动态切换就显得尤为诱人了。

那如何实现呢?这里我们需要使用maven-resources-plugin插件。

在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>

在< plugins/>里添加

<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,就是生产环境的配置了。

以上就是SpringBoot + Maven实现多环境动态切换yml配置及配置文件拆分。


站在前人的肩膀上前行,感谢以下博客及文献的支持。
Spring-Boot application.yml 文件拆分,实现 maven 多环境动态启用 Profiles

猜你喜欢

转载自blog.csdn.net/Colton_Null/article/details/82145467