1

多环境配置 - SpringBoot 2.7.2 实战基础 - 程序员优雅哥

 1 year ago
source link: https://www.cnblogs.com/youyacoder/p/16580308.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

优雅哥 SpringBoot 2.7.2 实战基础 - 06 -多环境配置

在一个项目的开发过程中,通常伴随着多套环境:本地环境 local、开发环境 dev、集成测试环境 test、用户接受测试环境 uat、预生产环境 pre、生产环境 prod。本节的内容有些脱离真实企业开发,因为在真实的企业开发中,不会只开发一个独立的服务,而是多个微服务。发展至今,云原生也越来越普遍。面对多套环境,通常是”一套代码 + 配置中心“的方式,将代码编译打包后,在启动服务时或服务运行过程中,从配置中心读取配置,保证各个环境代码完全一致,仅配置不同。关于配置中心,在后面的系列文章中会详细介绍。

1 配置文件

1.1 配置文件优先级

在第一篇文章中就谈到,Spring Boot 默认核心配置文件名为 application,支持 application.propertiesapplication.ymlapplication.yaml。后面两个本质上同一种类型。当三者同时存在时,优先级为:properties > yaml > yml。也就是说,当某个配置项在几者中都存在时,该配置项的值就使用 application.properties 中的值。

此外,配置文件的优先级与目录有关。通常情况下,我们都把核心配置文件放在 src/main/resources 下,本质上是在类路径下(编译后 src/main/resources 下的文件会编译到 target/classes/下面)。如果放到 config目录下(如: src/main/resources/config/application.yml),config目录下的核心配置文件优先级会更高。

官方文档中还提到核心配置文件放在项目根路径等情景,在项目中到目前为止都没碰到过,这里就不提了。

最后说明一点,优先级最最高的,是在启动命令行后面的参数。如 server.port,无论在配置文件中配置什么,只要在启动命令后面配置了该参数,就使用该参数的值:

java -jar hero-springboot-demo.jar --server.port=9099

1.2 bootstrap.yml

也许在很多代码里会看到 bootstrap.yml文件,有些文章说 bootstrap.yml 的优先级最高。这种说法是不完全准确的!

bootstrap.yml 文件在纯粹的 Spring Boot 应用中不会生效,它只有在 Spring Cloud 下才会生效,在 Spring Cloud 中,应用会直接或间接依赖 spring-cloud-context,此时才会读取 bootstrap.yml 文件和 application.yml 文件,这种情况下 bootstrap.yml 优先级高于其他核心配置文件。

1.3 准备配置文件

为多个环境准备不同的配置文件,这里模拟三个环境:本地环境 local、开发环境 dev、测试环境 test。在 src/main/resources 目录下复制 application.yml 到当前目录,分别重命名为 application-local.ymlapplication-dev.ymlapplication-test.yml。端口号 server.port 分别修改为 9099、9091、9092。

src/main/resources/
|- application.yml
|- application-local.yml	9099
|- application-dev.yml		9091
|- application-test.yml		9092
|- ....

上面复制的三个文件命名方式都是:application-{环境名称},按照这种方式命名,就不需要分别在每个环境的配置文件中使用 spring.profiles 来设置名字了。application.yml为主文件。

2 多环境两种配置方式

2.1 spring.profiles.active

这种方式可以通过设置 spring.profiles.active 的值来指定使用的环境配置文件。修改 application.yml ,删除里面的全部内容,添加 profile 配置,如下:

spring:
  profiles:
    active: local

上面的配置指定了环境为 local,会加载 application-local.yml 文件。

同样的,可以将 local修改为devtest,分别对应 application-dev.ymlapplication-test.yml

此外,在打包后,也可以在运行 jar 包时指定生效的环境:

java -jar hero-springboot-demo.jar --spring.profiles.active=test

由于启动参数中指定了 test,无论 application.yml 中配置什么,都会被命令行参数 test 覆盖,读取 application-test.yml 的配置。

2.2 Maven

除了上面手动修改 spring.profiles.active 的方式,还可以通过 Maven打包实现。

application.ymlspring.profiles.active 配置一个占位符,在 Maven 打包时,通过具体的 profile,替换 application.yml 中的占位符。具体实现如下:

1)application.yml 中使用占位符

spring:
  profiles:
    active: @env@

2)pom.xml 中配置多环境

首先配置 profile:

<profiles>
    <profile>
        <id>local</id>
        <properties>
            <env>local</env>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <env>test</env>
        </properties>
    </profile>
</profiles>

上面配置了三套 profile,env的值分别定义为 local、dev、test,且 local 为默认激活。 接着在 <build> 中添加资源的处理:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>application-*.yml</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application.yml</include>
                <include>application-${env}*.yml</include>
            </includes>
        </resource>
    </resources>
    <plugins>
       ...
    </plugins>
</build>

filtering设置为 true,表示利用占位符进行替换。上面的配置先排除 application- 开头的资源文件,然后在根据生效的profile 对应的 env的值,打包对应的 application-xxx.yml 文件。

现在启动服务,正常加载使用 application-local.yml,端口号为 9099。

接下来测试打包。打包有两种方式:使用命令行和IDEA。

mvn clean package -Pdev

-P后面就是对应的环境。

使用图形界面,需要先选择profile,如下图所示:

image-20220802115445886

两种方式打包后都会在 target 目录下生成 hero-springboot-demo.jar,可以在命令行中启动,查看加载的环境配置文件和运行的端口号:

java -jar target/hero-springboot-demo-1.0-SNAPSHOT.jar

image-20220802115630839

image

今日程序员优雅哥(\/ youyacoder)学习到此结束~~~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK