首页 > 其他分享 >SpringBoot——多环境配置

SpringBoot——多环境配置

时间:2023-01-08 18:33:20浏览次数:53  
标签:SpringBoot 配置 环境 dev application test properties

多环境配置

在开发应用时,常用部署的应用是多个的,比如:开发、测试、联调、生产等不同的应用环境,这些应用环境都对应不同的配置项,比如swagger一般上在生产时是关闭的;不同环境数据库地址、端口号等都是不尽相同的,要是没有多环境的自由切换,部署起来是很繁琐也容易出错的。

maven的多环境配置

在没有使用过springboot的多环境配置时,原先是利用mavenprofile功能进行多环境配置,这里我简单回顾下。

maven配置

<profiles>
    <profile>
       <id>dev</id>
       <activation>
             <activeByDefault>true</activeByDefault>
         </activation>
         <properties>
            <pom.port>8080</pom.port>
         </properties>
    </profile>
    <profile>
       <id>test</id>
         <properties>
            <pom.port>8888</pom.port>
         </properties>
    </profile>       
 </profiles>
 <build>
 <resources>
     <resource>
         <directory>src/main/resources</directory>
         <includes>
             <include>**/*</include>
         </includes>
     </resource>
     <resource>
         <directory>${project.basedir}/src/main/resources</directory>
         <includes>
             <include>**/*.properties</include>
         </includes>
         <!-- 加入此属性,才会进行过滤 -->
         <filtering>true</filtering>
     </resource>
 </resources>
 <plugins>
     <plugin>
         <artifactId>maven-resources-plugin</artifactId>
         <configuration>
             <encoding>utf-8</encoding>
             <!-- 需要加入,因为maven默认的是${},而springbooot 默认会把此替换成@{} -->
             <useDefaultDelimiters>true</useDefaultDelimiters>
         </configuration>
     </plugin>
     <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
 </plugins>
 </build>

然后编译时,加入-Ptest,则会替换test环境下的参数值。 完整参数:

mvn clean install -DskipTests -Ptest

application.properties

server.port=${pom.port}

利用maven实现多环境配置,比较麻烦的就是每次部署新环境时,都需要再次指定环境编译打包一次。一下进入主题,springboot的多环境,比较优雅了许多。

springboot多环境配置

Profile是Spring针对不同环境不同配置的支持。需要满足application-{profile}.properties{profile}对应你的环境标识。如:

  • application-dev.properties:开发环境
  • application-test.properties:测试环境

image

而指定执行哪份配置文件,只需要在application.properties配置spring.profiles.active为对应${profile}的值。

# 指定环境为dev
spring.profiles.active=dev

则会加载:application-dev.properties的配置内容。

2018-07-15 14:52:41.304  INFO 15496 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-07-15 14:52:41.310  INFO 15496 --- [           main] c.l.l.s.chapter5.Chapter5Application     : Started Chapter5Application in 8.506 seconds (JVM running for 10.81)
2018-07-15 14:52:41.316  INFO 15496 --- [           main] c.l.l.s.chapter5.Chapter5Application     : 多环境应用启动.

还可以在**命令行方式**激活不同环境配置,如

java -jar xxx.jar --spring.profiles.active=test

此时就会加载application-test.properties的配置内容。 test:

image

image

这里顺便提一句,可能在不同环境下,可能加载不同的bean时,可利用@Profile注解来动态激活

@Profile("dev")//支持数组:@Profile({"dev","test"})
@Configuration
@Slf4j
public class ProfileBean {
 
    @PostConstruct
    public void init() {
        log.info("dev环境下激活");
    }    
}

启动时。控制台输出:

2018-07-15 15:04:44.540  INFO 11876 --- [           main] c.l.l.springboot.chapter5.ProfileBean    : dev环境下激活

参考:

https://blog.lqdev.cn/2018/07/15/springboot/chapter-five/

标签:SpringBoot,配置,环境,dev,application,test,properties
From: https://blog.51cto.com/u_14014612/5996748

相关文章

  • SpringBoot——常用注解介绍及简单使用
    常用注解@SpringBootApplication前几章,在系统启动类里面,都加入了此启动注解,此注解是个组合注解,包括了@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentSc......
  • SpringBoot——过滤器、监听器、拦截器
    前言在实际开发过程中,经常会碰见一些比如系统启动初始化信息、统计在线人数、在线用户数、过滤敏高词汇、访问权限控制(URL级别)等业务需求。这些对于业务来说一般上是无关......
  • SpringBoot——统一异常、数据校验处理
    前言在web应用中,请求处理时,出现异常是非常常见的。所以当应用出现各类异常时,进行异常的捕获或者二次处理(比如sql异常正常是不能外抛)是非常必要的,比如在开发对外api服务时......
  • SpringBoot——Mybatis-plus的集成和使用
    前言对于后端开发者而言,和数据库打交道是每天都在进行的,所以一个好用的ORM框架是很有必要的。目前,绝大部分公司都选择MyBatis框架作为底层数据库持久化框架。Mybatis-Plus......
  • 【SpringBoot实战专题】「开发实战系列」从零开始教你舒服的使用RedisTemplate操作Red
    SpringBoot快速操作Redis数据在SpringBoot框架中提供了spring-boot-starter-data-redis的依赖组件进行操作Redis服务,当引入了该组件之后,只需要配置Redis的配置即可进行链......
  • go语言环境配置(windous)
    1.下载go语言安装包:https://golang.google.cn/dl/2.配置环境变量:GOBIN:项目bin目录 GOPATH:项目目录(src下)GOROOT(go的环境变量)GOROOT,Go解释器安装路径,用于之后去调用g......
  • mysql5.7配置文件
    [client]port =3306socket =/data/mysql/mysql.sock[mysql]prompt="\u@mysqldb\R:\m:\s[\d]>"no-auto-rehash[mysqld]server-id={{server_id}}gtid-mode=onenforce......
  • P7_小程序的宿主环境
    宿主环境简介什么是宿主环境宿主环境(hostenvironment)指的是程序运行所必须的依赖环境。例如:Android系统和iOS系统是两个不同的宿主环境。安卓版的微信App是不能在......
  • springboot加入拦截器后swagger无法访问
    由于需要对用户权限进行认证,判断登录与否,然后设置了全局拦截器,然后测接口的额时候swagger也被拦截了(QAQ)拦截器不配置拦截路径时可以访问,配置后出现以下情况两种......
  • OpenHarmony开发06 —— 配置EMQX与MQTTX
    OpenHarmony开发06——配置EMQX与MQTTX安装与启动在网站下载EMQX——Windows版解压后,命令行进入解压路径里的bin,启动EMQX./bin/emqxstart浏览器打开http:......