第3章 Spring Boot多环境配置实战:轻松应对开发、测试与生产

1,251 阅读3分钟

在上一篇文章【Spring Boot 脚手架搭建:快速配置,轻松上手】中,我们已经掌握了如何使用 Spring Initializr 快速生成一个基本的 Spring Boot 项目,并且初步了解了项目的基本结构和简单的 HTTP 接口实现。接下来,我们将进一步探讨 Spring Boot 中配置文件的使用,这是理解和掌握 Spring Boot 应用的关键之一。

任务要求

使用IDEA开发工具构建一个项目多模块工程。study-springboot-chapter01学习关于Springboot配置相关的技术知识点

  1. 基于study-springboot工程,新建一个Maven项目,坐标groupId(com.cbitedu)、artifactId(study-springboot-chapter01),其他默认
  2. 继承study-springboot工程依赖

任务收获

  1. 生产环境、开发环境、测试环境应用场景介绍

  2. 学会多环境配置:SpringBoot默认配置文件application.properties,通过键值对配置对应属性

  3. 启动工程,切换不同的环境,通过日志观察不同的环境参数值变化 :

    • 使用的域名、端口、实例数目是不同的;
    • 连接的数据库地址、端口、名称是不同的;
    • 使用的日志输出格式、级别、保存时间是不同的;
  4. 掌握配置文件的优先级:一般Springboot默认能识别的位置有四个(优先级也是从1-4)

环境要求

  1. JDK1.8+
  2. MySQL8.0.27+
  3. Maven 3.6.1+
  4. IDEA/VSCode

工程目录要求

多环境分别为application-dev.properties为开发环境

application-prod.properties为生产环境

application-test.properties为测试环境

仓库地址:admin@code.creatorblue.com/r/java/stud…

模块:study-springboot-chapter01

任务实施步骤如下:

1、 复制study-springboot工程,更改名为study-springboot-chapter01

2、修改study-springboot工程项目中的pom.xml文件构造多模块,关于修改如下图所示:

3、修改study-springboot-chapter01工程中的pom.xml文件

4、编写Spring Boot使用全局配置文件,初始化容器环境,配置文件名是固定的;

根据项目需要,实现多环境配置

这里的 环境 实际上是一个统称,不同的环境可能代表着

  • 使用的域名、端口、实例数目是不同的;
  • 连接的数据库地址、端口、名称是不同的;
  • 使用的日志输出格式、级别、保存时间是不同的;

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如

配置文件作用:修改Spring Boot在底层封装好的默认值;

image.png

一般Springboot默认能识别的位置有四个(优先级也是从1-4)

  1. config/application.properties(项目根目录中 config 目录下)

  2. application.properties(项目根目录下)

  3. resources/config/application.properties(项目 resources 目录中 config 目录下)

  4. resources/application.properties(项目的 resources 目录下)

启用具体环境: application.properties:默认配置中指定 spring.profiles.active=dev

  1. application-dev.properties:开发环境
  2. application-test.properties:测试环境
  3. application-prod.properties:生产环境
  • application.yml

在pom.xml文件中导入配置文件处理器,以后编写配置就有提示了

        <!--导入配置文件处理器,以后编写配置就有提示了-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

5、分别在application.properties中加入

#指定默认的环境配置
spring.profiles.active=dev

在application-dev.properties开发环境中配置

#SpringBoot内置属性查询
#https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties
#官方文档中参考文档第一项:Application Properties
#修改服务器端口
server.port=81
#关闭运行日志图标(banner)
spring.main.banner-mode=off
#设置日志相关
logging.level.root=debug
logging.level.com.cbitedu=info

在application-prod.properties生产环境中配置

#SpringBoot内置属性查询
#https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties
#官方文档中参考文档第一项:Application Properties
#修改服务器端口
server.port=80
#关闭运行日志图标(banner)
spring.main.banner-mode=off
#设置日志相关
logging.level.root=error

也可以用application.yaml(或用---来分割)一个文件代替

## 家乡属性
home:
  province: 浙江省
  city: 温岭松门
  desc: 我家住在${home.province}的${home.city}

## 随机属性
user:
  id: ${random.long}
  age: ${random.int[1,200]}
  desc: 程序员叫做${random.value}
  uuid: ${random.uuid}

或application.yml代替(application-dev.yml,application-prod.yml)

实验实训

熟悉Springboot常见参数配置

docs.spring.io/spring-boot…

动手实践

1、数据库MySQL配置参数

spring.datasource.url=jdbc:mysql://localhost:3306/platform?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

2、server设置

server.servlet.context-path=/console
server.port=80

3、文件上传设置

spring.servlet.multipart.max-request-size=100MB
spring.servlet.multipart.max-file-size=100MB
spring.main.allow-bean-definition-overriding=true