知识杂货铺 知识杂货铺
首页
后端(1本书)
  • 主题初衷与诞生
  • 介绍
  • 快速上手
  • 目录结构
  • 核心配置和约定
  • 自动生成front matter
  • Markdown 容器
  • Markdown 中使用组件
  • 相关文章

    • 使目录栏支持h2~h6标题
    • 如何让你的笔记更有表现力
    • 批量操作front matter工具
    • 部署
    • 关于写文章和H1标题
    • 关于博客搭建与管理
    • 在线编辑和新增文章的方法
  • 主题配置
  • 首页配置
  • front matter配置
  • 目录页配置
  • 添加摘要
  • 修改主题颜色和样式
  • 评论栏
  • 快速开始
  • 代码集成_TODO
  • 框架初探
  • 在GitHub上贡献代码
  • 使用K8s部署系统
  • Seata分布式事务
GitHub (opens new window)

Kevin Zhang

爱凑热闹的高龄程序猿
首页
后端(1本书)
  • 主题初衷与诞生
  • 介绍
  • 快速上手
  • 目录结构
  • 核心配置和约定
  • 自动生成front matter
  • Markdown 容器
  • Markdown 中使用组件
  • 相关文章

    • 使目录栏支持h2~h6标题
    • 如何让你的笔记更有表现力
    • 批量操作front matter工具
    • 部署
    • 关于写文章和H1标题
    • 关于博客搭建与管理
    • 在线编辑和新增文章的方法
  • 主题配置
  • 首页配置
  • front matter配置
  • 目录页配置
  • 添加摘要
  • 修改主题颜色和样式
  • 评论栏
  • 快速开始
  • 代码集成_TODO
  • 框架初探
  • 在GitHub上贡献代码
  • 使用K8s部署系统
  • Seata分布式事务
GitHub (opens new window)
  • Spring Boot 培训教程
  • Spring Boot介绍

  • 开发环境配置

  • 原理剖析

  • Web开发

  • 数据访问

  • 事务

  • 集成Redis

  • 集成MongoDB

  • 异步消息

  • 异常处理

  • 单元测试与热部署

  • 安全控制

  • 应用监控

  • 企业级开发

  • 多环境配置与部署

    • 多环境配置与部署
    • 多环境配置
      • 15.1 使用多环境配置
    • 打包部署应用
    • 课后作业
  • 综合示例

  • 前后端分离的vue急速入门

  • Spring Boot配置大全

  • 在Docker中部署Spring Boot应用

  • 开发前后端分离应用

  • 前进到Spring Cloud

  • 规则引擎

  • 流程引擎

  • 后记
  • 后端
  • 多环境配置与部署
Kevin Zhang
2024-10-30
目录

多环境配置

# 15.1 使用多环境配置

Spring Boot 是通过概要文件(profile)机制对多环境配置提供支持的。

典型的用法是,通过在 resources 目录下提供多个 application-xxx.properties 文件,然后在 application.properties 中设置 spring.profiles.active=xxx 之类的配置或在运行时提供 --spring.profiles.active=xxx 参数来激活某个概要文件。

还有一种不常用的方法是通过在启动类中设置激活的概要文件。

@SpringBootApplication
public class SpringBootConfigApplication {

	public static void main(String[] args) {
		SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootConfigApplication.class);
		builder.application().setAdditionalProfiles("dev");
		builder.run(args);
	}

}
1
2
3
4
5
6
7
8
9
10

创建一个 Spring Boot 应用。

image-20200131100731009

选择支持数据库的 Web 应用启动器依赖。

image-20200131100804116

提供 4 个配置文件 application.properties、application-dev.properties、application-prod.properties 和 application-test.properties。

开发环境概要文件 application-dev.properties 内容如下:

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/ssdev?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1
2
3
4
5

测试环境概要文件 application-test.properties 内容如下:

server.port=7070
spring.datasource.url=jdbc:mysql://localhost:3306/sstest?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1
2
3
4
5

生产环境概要文件 application-prod.properties 内容如下:

server.port=9090
spring.datasource.url=jdbc:mysql://localhost:3306/ssprod?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1
2
3
4
5

项目配置文件 application.properties 中设置激活的概要文件,其内容如下:

spring.profiles.active=dev
1

添加 entiy、dao、service 和 controller 类,以便测试配置文件是否生效。数据库结构和代码复用“5.1 Spring Boot 集成 JdbcTemplate”小节的对应代码。

数据库表结构 DDL 语句:

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `userName` varchar(32) NOT NULL,
  `passWord` varchar(50) NOT NULL,
  `realName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
1
2
3
4
5
6
7
8

实体 User 类:

public class User {
	
	private Integer id;
	private String userName;
	private String passWord;
	private String realName;
//...setter & getter
	@Override
	public String toString() {

        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", realName='" + realName + '\'' +
                '}';
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

数据访问 UserDAO 类:

@Repository
public class UserDAO {

	@Autowired
	JdbcTemplate jdbcTemplate;

	public User getUser(int id) {
		String sql = "select * from user where id=" + id;
		return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class));
	}

}
1
2
3
4
5
6
7
8
9
10
11
12

服务 UserService 类:

@Service
public class UserService {

	@Autowired
	UserDAO userDAO;

	public User getUser(int id) {
		return userDAO.getUser(id);
	}

}
1
2
3
4
5
6
7
8
9
10
11

控制器 UserController 类:

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("get/{id}")
	public String getUser(@PathVariable int id) {
		return userService.getUser(id).toString();
	}
	
}
1
2
3
4
5
6
7
8
9
10
11
12
13

清空配置文件 application.properties 内容,使用启动类中的代码配置方式激活“dev”概要文件,应用程序在 8080 端口提供服务,在浏览器中访问测试,情况如下:

image-20200131103455222

在配置文件 application.properties 中添加“spring.profiles.active=prod”激活生产环境概要文件,应用程序在 9090 端口提供服务,在浏览器中访问测试,情况如下:

image-20200131103557938

应用打包后,在项目的“spring-boot-config\target”下,通过启动参数 java -jar config-0.0.1-SNAPSHOT.jar --spring.profiles.active=test 激活测试环境概要文件。

java -jar config-0.0.1-SNAPSHOT.jar --spring.profiles.active=test

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.4.RELEASE)
...
2020-01-31 10:40:31.269  INFO 12344 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7070 (http) with context path ''
2020-01-31 10:40:31.275  INFO 12344 --- [           main] c.e.config.SpringBootConfigApplication   : Started SpringBootConfigApplication in 3.369 seconds (JVM running for 4.134)
1
2
3
4
5
6
7
8
9
10
11
12

应用程序在测试环境的 7070 端口提供服务,在浏览器中访问测试,情况如下:

image-20200131104149349

通过上述示例,我们可以知道如何配置概要文件,和如何激活概要文件。也验证了概要文件的激活优先顺序:

  1. 启动命令行参数 --spring.profiles.active=test 最优先;
  2. 配置配置文件 application.properties 中 spring.profiles.active=prod 的设置次之;
  3. 启动类中 SpringApplicationBuilder.application().setAdditionalProfiles("dev") 优先级最低。

这也是符合开发运维的通行做法:命令优先于配置,配置优先于代码。这样的原则或做法,在 Spring 体系内大量存在,广泛使用。

本小节示例项目代码:

https://github.com/gyzhang/SpringBootCourseCode/tree/master/spring-boot-config (opens new window)

编辑 (opens new window)
上次更新: 2024/11/17, 16:29:23
多环境配置与部署
打包部署应用

← 多环境配置与部署 打包部署应用→

最近更新
01
PNG图片处理C++
02-07
02
PNG图片处理
01-24
03
离线安装Docker
12-24
更多文章>
Theme by Vdoing | Copyright © 2008-2025 Kevin Zhang | MIT License | 蜀ICP备20013663号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式