
基于若依框架实践(二):通过profile隔离不同环境配置
基于springboot的应用通过profile隔离不同环境配置参数:1、提取可变点到配置文件中2、maven的pom中配置3、编译打包时候切换不同profile4、源码可参考开源地址:
相关文章:
基于若依框架实践(一):总体的介绍_疯癫的老码农的博客-CSDN博客
基于若依框架实践(二):通过profile隔离不同环境配置_疯癫的老码农的博客-CSDN博客
基于若依框架实践(三):通过swagger定义接口_疯癫的老码农的博客-CSDN博客
写作背景
做了很多年的软件研发,写的文章不多、总结的也不多,曾经做过的积累或者后续的遇到的技术难点还是考虑稍微写一写,算是给自己的一个记录吧~。
近期基于若依框架开发了好几个小系统, 系统从功能上既包含管理台功能也有自己的前台部分,借此机会做一个总结,并且分享给有需要的同学,不喜勿喷,有劳各位看官费神了。
本文简介
在 SpringBoot 中,我们可以通过 profile 来隔离不同的环境配置。profile 可以看做是一个标识,用来区分不同的环境,比如开发环境、测试环境、生产环境等。通过 profile,我们可以为不同的环境定制不同的配置文件,实现环境的隔离。
实践总结
1、提取可变点到配置文件中
具体做法如下:
在application.yml文件中,通过spring.profiles.active来指定当前激活的 profile。比如,我们可以在application.yml中设置spring.profiles.active=test,表示当前激活的是测试环境。
对于不同的环境,我们可以创建不同的配置文件,比如application-dev.yml、application-sit.yml、application-prd.yml等。在这些文件中,可以分别配置不同的属性值,以满足不同环境的需求。SpringBoot 会自动扫描这些配置文件,并根据spring.profiles.active指定的 profile 来选择对应的配置文件进行加载。
- 公共配置继续保留在application.yml中
- 每个环境的不同值信息提取到各自环境中
- 开发环境:application-dev.yml
- 测试环境:application-sit.yml
- 生产环境:application-prd.yml
通过这种方式,我们可以灵活地管理和切换不同环境的配置,提高了开发的灵活性和可维护性。
3、application.yml配置的信息
主要是通过profileActive这个变量进行切换的,如下:
# Spring配置
spring:
# 资源信息
messages:
# 国际化资源文件路径
basename: i18n/messages
profiles:
active: @profileActive@
3、具体某个环境的配置
一般情况下,sit环境、prd环境的数据库信息、redis等配置肯定不一样,我们拿sit为例:
# 数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql://127.0.0.1:3306/ry-vue-v3.8.6?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
username: root
password: root
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: false
url:
username:
password:
# 初始连接数
initialSize: 5
# 最小连接池数量
minIdle: 10
# 最大连接池数量
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置连接超时时间
connectTimeout: 30000
# 配置网络超时时间
socketTimeout: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username: ruoyi
login-password: 123456
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
# redis 配置
redis:
# 地址
host: 127.0.0.1
# 端口,默认为6379
port: 6379
# 数据库索引
database: 0
# 密码
password:
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
4、maven的pom中配置
-
profile的配置内容:
<profiles>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<!-- 默认激活的环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>sit</id>
<properties>
<profileActive>sit</profileActive>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profileActive>prd</profileActive>
</properties>
</profile>
</profiles>
- build的配置信息
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application-${profileActive}.yml</include>
<include>application.yml</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.1.RELEASE</version>
<configuration>
<fork>true</fork>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>${project.artifactId}</warName>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
5、编译打包时候切换不同profile
项目源码
更多推荐
所有评论(0)