好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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文件中配置配置中心,如下所示

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

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

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

spring profile多环境配置管理

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

现象

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

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

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

解决

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<!-- 开发环境配置文件 -->

    < 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方式:

?

1

ConfigurableEnvironment.setActiveProfiles( "test" )

2、JVM参数方式:

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

?

1

2

3

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方式:

?

1

2

3

4

< init-param >

   < param-name >spring.profiles.active</ param-name >

   < param-value >production</ param-value >

</ init-param >

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

?

1

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/runner668/article/details/102637031

查看更多关于springcloud使用profile实现多环境配置方式的详细内容...

  阅读:24次