springcloud中怎么使用profile实现多环境配置

发布时间:2022-03-01 16:25:11 作者:iii
来源:亿速云 阅读:310

这篇文章主要介绍“springcloud中怎么使用profile实现多环境配置”,在日常操作中,相信很多人在springcloud中怎么使用profile实现多环境配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”springcloud中怎么使用profile实现多环境配置”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

使用profile实现多环境配置

基本介绍

在开发过程中,我们的项目会存在不同的运行环境,比如开发环境、测试环境、生产环境,而我们的项目在不同的环境中,有的配置可能会不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置,那每次我们将软件部署到不同的环境时,都需要修改相应的配置文件,这样来回修改,很容易出错,而且浪费劳动力。

springcloud默认会访问的配置文件名是application.properties,

我们如果要创建多环境的配置文件的话,文件名格式应为:application-{profile}.properties

其中的{profile}用来标识不同的环境,如application-native.properties文件可以用来配置本地环境、application-prod.properties文件可以用来配置生产环境。

springcloud中通过“spring.profiles.active”属性来指定{profile},如spring.profiles.active=native,则使用的是application-native.properties配置文件。

由于springcloud配置中心和springboot的多环境配置并没有打通,

所以使用java -jar xxxx.jar --spring.profiles.active=prod命令只能对springboot项目中的配置有效,

并不能从配置中心获取不同的环境配置,想要实现目标还需要多做一些工作。 

项目配置

在bootstrap.yml文件中配置配置中心,如下所示

使用三个短横线将不同环境分隔开,这样可以在一个文件中完成多个环境配置

spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
  cloud:
    bootstrap:
      enabled: false
    config:
      uri: http://localhost:8888
      name: webclient
      profile: dev
---
spring:
  profiles: test
  cloud:
    bootstrap:
      enabled: false
    config:
      uri: http://localhost:8888
      name: webclient
      profile: test
---
spring:
  profiles: prod
  cloud:
    bootstrap:
      enabled: false
    config:
      uri: http://localhost:8888
      name: webclient
      profile: prod

springcloud中怎么使用profile实现多环境配置

而顶层pom中的配置情况是如下的:

springcloud中怎么使用profile实现多环境配置

spring profile多环境配置管理

本地、测试、开发、产品等不同环境文件配置

现象

如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响。

开发时的某些配置比如log4j日志的级别,和生产环境又有所区别。

各种此类的需求,让我希望有一个简单的切换开发环境的好办法。

解决

现在spring3.1也给我们带来了profile,可以方便快速的切换环境。

使用也是非常方便。只要在applicationContext.xml中添加下边的内容,就可以了

<!-- 开发环境配置文件 -->
    <beans profile="test">
        <context:property-placeholder location="/WEB-INF/test-orm.properties" />
    </beans>
    <!-- 本地环境配置文件 -->
    <beans profile="local">
        <context:property-placeholder location="/WEB-INF/local-orm.properties" />
    </beans>
  profile的定义一定要在文档的最下边,否则会有异常。整个xml的结构大概是这样
<beans xmlns="..." ...>  
  <bean id="dataSource" ... />  
  <bean ... />  
  <beans profile="...">  
    <bean ...>  
  </beans>  
</beans>

激活 profile

spring 为我们提供了大量的激活 profile 的方法,可以通过代码来激活,也可以通过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile,这里我们通过定义 JVM 参数实现。

1、ENV方式:

ConfigurableEnvironment.setActiveProfiles("test")

2、JVM参数方式:

tomcat中catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通过设置active选择不同配置文件

set JAVA_OPTS="-Dspring.profiles.active=test"
  eclipse 中启动tomcat。项目右键 run as –> run configuration–>Arguments–> VM arguments中添加。local配置文件不必上传git追踪管理
-Dspring.profiles.active="local"

3、web.xml方式:

<init-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>production</param-value>
</init-param>

4、标注方式(junit单元测试非常实用):

@ActiveProfiles({"unittest","productprofile"})

到此,关于“springcloud中怎么使用profile实现多环境配置”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. Spring @Profile注解如何实现多环境配置
  2. 如何在springboot中实现多环境配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springcloud profile

上一篇:在kali上怎么安装AWVS

下一篇:Angular变更检测的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》