Properties配置分离

背景

目前公司从开发到发布一个版本的流程是本地开发环境(自测)->测试环境(测试人员)->灰度发布环境(测试人员+开发人员)->上线, 不同的环境配置的属性不同(例如: 数据库连接url, 用户名, 密码), 以下为我经历的阶段.

阶段一: 将所有环境的配置信息写在一个文件中, 如db.properties

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
##-------------开发-------------------------
##=============主库========================
jdbc.writer.driverClassName=com.mysql.jdbc.Driver
jdbc.writer.url=jdbc:mysql://192.168.1.10:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=utf8
jdbc.writer.username=dev
jdbc.writer.password=dev

##-------------测试-------------------------
##=============主库========================
#jdbc.writer.driverClassName=com.mysql.jdbc.Driver
#jdbc.writer.url=jdbc:mysql://192.168.0.10:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=utf8
#jdbc.writer.username=test
#jdbc.writer.password=test

##-------------灰度发布-------------------------
##=============主库========================
#jdbc.writer.driverClassName=com.mysql.jdbc.Driver
#jdbc.writer.url=jdbc:mysql://192.168.10.10:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=utf8
#jdbc.writer.username=prePub
#jdbc.writer.password=prePub

##-------------生产-------------------------
##=============主库========================
#jdbc.writer.driverClassName=com.mysql.jdbc.Driver
#jdbc.writer.url=jdbc:mysql://192.168.100.10:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=utf8
#jdbc.writer.username=pub
#jdbc.writer.password=pub

以上配置在打包时根据不同的环境手动注释和反注释相关代码.

阶段二: 将以上配置拆分成四个配置文件

配置文件为db-dev.properties, db-test.properties, db-prepub.properties, db-pub.properties, 利用maven进行配置, 在打包的时候指定要打的包或把所有包打出来

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<profiles>

<profile>

<id>dev</id>

<properties>

<package.environment>dev</package.environment>

</properties>

</profile>

<profile>

<id>test</id>

<properties>

<package.environment>test</package.environment>

</properties>

</profile>

<profile>

<id>prePub</id>

<properties>

<package.environment>prePub</package.environment>

</properties>

</profile>

<profile>

<id>pub</id>

<properties>

<package.environment>pub</package.environment>

</properties>

</profile>
</profiles>

打包与配置的繁琐事情解决了, 在需要打包的时候指定参数就可以打出自己所需环境的包, 但经过一段时间的开发, 新的问题出现了, 当需要在配置文件中添加或修改属性时, 得同时修改三个文件, 其次线上环境那些敏感的数据被暴露给开发人员了. 经过思考, 打算采用配置服务器对配置进行管理.

方案三: 配置分离

在各个环境中准备好配套的配置(例如: /data/configs/proj_name/db.properties)

在spring中配置读取文件properties的位置, 如下:

1
2
3
4
5
6
7
8
9
10
<bean id="commonProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"></property>
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
<property name="locations">
<list>
<value>file:/data/configs/myProj/spring-redis.properties</value>
<value>file:/data/configs/myProj/db.properties</value>
</list>
</property>
</bean>

该方案解决了敏感数据暴露给开发人员的问题, 但维护配置还是繁琐;接下来计划开发一个配置服务器,对各个项目提供配置信息.

方案四: 配置分离之配置服务器

待续.

优缺点

方案一

  • 优点: 只需要维护一个文件
  • 缺点: 打包时需要注释及反注释配置, 繁琐; 配置中敏感数据暴露给开发人员;

方案二

  • 优点: 一条指令可以打出不同环境的包;
  • 缺点: 需要维护三个配置文件; 配置中敏感数据暴露给开发人员;

方案三

  • 优点: 配置中敏感数据不再暴露给开发人员;
  • 缺点: 需要在不同的机器上维护配置文件;

转载

本文出自<<arccode>>, 欢迎转载, 转载请注明出处.