在日常开发中,我们可能经常会碰到要调试代码或者切换到线上环境去查看问题。
在没有配置环境切换的情况下,我们需要来回修改各种配置地址,例如 MySQL、Redis。。。等等
所以,当你看完本文,你可以学到如何对一个SpringBoot单体项目进行改造,通过Maven选择环境来实现动态切换。
阅读耗时:10-15min



目录

  • 一、原始项目配置
  • 二、利用Profile实现环境切换
  • 2.1 创建各种环境的配置文件
  • 2.2 POM文件添加配置
  • 2.3 配置占位符生效(假如你是通过Maven插件来进行动态切换的,你需要添加如下内容, 否则跳过)
  • 2.4 修改我们的`application.yml`文件
  • 三、结果测试并验证:
  • 3.1 编写验证类
  • 3.2 验证dev环境
  • 3.3 切换为prod环境
  • 3.4 问题踩坑
  • 四、总结



案例Demo地址:https://gitee.com/Linda_master/multi-environment

一、原始项目配置

你的Resource文件夹下application.yml配置文件可能是这样(这里只是简单列举了MySQL,其它配置雷同这里就不在阐述),我们要切换环境就要一个个修改地址。

server:
  port: 8080
  tomcat:
    max-connections: 20
    threads:
      max: 20
      min-spare: 10
    accept-count: 10
spring:
  config:
    name: tomandcat
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/xxxdb?serverTimezone=GMT%2B8
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

二、利用Profile实现环境切换

2.1 创建各种环境的配置文件

创建如下图:

springboot怎么切换xml文件 springboot环境切换配置_springboot

在Resource包下创建我们多种环境的配置文件,application-dev.ymlapplication-dev.yml这时候你肯定会想为什么我这样创建文件就可以SpringBoot就能读到相关的配置呢?
这是应为SpringBoot 它就是一个整合框架啊,同时它的开发理念就是我们常常谈到的约定>配置>编码,从而提高我们程序员的开发效率。
比如拿application.yml配置文件来说,因为约定,我们才可以把所有配置按照固定格式写到该文件中,然后由SpringBoot和第三方jar整合包通过自动装配来读到自己需要的配置,如MySQL、Redis、Nacos等等吧,这就是它好用的原因之一。

小tips,你还记得配置文件的优先级吗?
bootstrap.properties > bootstrap.yml > application.properties > application.yml

现在我们看看application-dev.yml会有什么内容吧

# 自定义配置 为了等下验证读取的配置文件环境
appName: tomandcat-dev

server:
  port: 8091

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/xxx01?serverTimezone=GMT%2B8
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource

其它环境的配置文件类似,这里我就不再添加了(如有需要查看案例

2.2 POM文件添加配置

<!-- 配置maven占位符-->
<profiles>
    <!-- 配置需要切换的环境 -->
    <profile>
        <id>dev</id>
        <!-- properties下的每个子标签我们可以理解为一个配置 -->
        <properties>
            <!--  标签名为自定义,和我们平时写properties一样,同样我们也可以定义一个或多个(key, value)配置-->
            <runtime-profile>dev</runtime-profile>
          <!-- 如果你需要配置一些变量,你可以在这里设置一个k-v数据,在具体的环境变量中读取这个数据或者你也可以在具体的配置文件中直接设置(如application-dev.yml) -->
            <config-addr>127.0.0.1:8848</config-addr>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <runtime-profile>prod</runtime-profile>
            <config-addr>127.0.0.1:8848</config-addr>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <runtime-profile>test</runtime-profile>
            <config-addr>127.0.0.1:8848</config-addr>
        </properties>
    </profile>
</profiles>

2.3 配置占位符生效(假如你是通过Maven插件来进行动态切换的,你需要添加如下内容, 否则跳过)

springboot怎么切换xml文件 springboot环境切换配置_配置文件_02

<build>
    <!-- 配置所有的yml文件中占位符生效, 假如我们是手动来更新application.yml文件我们可以不配置,手动更新指的是我们在application.yml直接指定profile的值如dev、prod-->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <!-- 会生效-->
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

2.4 修改我们的application.yml文件

手动的话无需在pom中配置profiles,SpringBoot 会自动读取到对应的环境

springboot怎么切换xml文件 springboot环境切换配置_mysql_03


代码如下(注意缩进):

# 根据环境读取配置文件(手动)
  #  profiles:
  #    active: dev
  # 根据环境读取配置文件(通过勾选maven插件)
  profiles:
    active: @runtime-profile@

三、结果测试并验证:

我是采用的maven插件勾选环境来动态切换的;

3.1 编写验证类

@RestController
@RequestMapping("/api")
public class HelloWord {

    @Value("${appName}")
    private String appName;
    @Value("${server.port}")
    private String port;

    @RequestMapping("/hello")
    public String test(){
        return "hello word! - " + port + " - " + appName;
    }
}

3.2 验证dev环境

springboot怎么切换xml文件 springboot环境切换配置_springboot怎么切换xml文件_04


启动项目,访问http://localhost:8091/api/hello

springboot怎么切换xml文件 springboot环境切换配置_配置文件_05

3.3 切换为prod环境

切换环境,访问http://localhost:8092/api/hello

springboot怎么切换xml文件 springboot环境切换配置_bc_06


springboot怎么切换xml文件 springboot环境切换配置_mysql_07

3.4 问题踩坑

假如启动报如下错误

springboot怎么切换xml文件 springboot环境切换配置_bc_08


就是我们切换了环境,但是由于缓存问题暂位符profile没有更新(个人认为),我们可以重新加载下maven,如下图即可解决

springboot怎么切换xml文件 springboot环境切换配置_springboot怎么切换xml文件_09


上述缓存的问题并不影响编译和打包,验证如下(查看编译和打包结果):

springboot怎么切换xml文件 springboot环境切换配置_springboot_10


可以看到读取的是prod

springboot怎么切换xml文件 springboot环境切换配置_mysql_11

我们可以解压jar包查看

springboot怎么切换xml文件 springboot环境切换配置_bc_12

四、总结

以上就是本文的所有内容,本文仅介绍了SpringBoot单体项目如何实现多环境动态切换,如有错误之处,还请各位读者多多指正,感谢!后续还会更新微服务的整合nacos实现多环境动态切换,记得关注喔