1.Java客户端:
客户端首先引入super-diamond-client的jar包
<dependency>
<groupId>com.github.diamond</groupId>
<artifactId>super-diamond-client</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
spring配置中添加
<bean id="propertiesConfiguration" class="com.github.diamond.client.PropertiesConfigurationFactoryBean"/>
PropertiesConfigurationFactoryBean类往里面找 有
System.getProperty获取虚拟机参数,故单元测试或者Tomcat容器跑的时候都要配置vm参数。
-Dsuperdiamond.projcode=***
-Dsuperdiamond.host=***
-Dsuperdiamond.port=8283
-Dsuperdiamond.profile=development
然后可以用PropertiesConfigurationFactoryBean的静态方法来获取superdiamond的配置了
PropertiesConfigurationFactoryBean.getPropertiesConfiguration().getString("这里填superDiamond配置中的key")
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="properties" ref="propertiesConfiguration" />
</bean>
<bean id="propertiesConfiguration" class="com.github.diamond.client.PropertiesConfigurationFactoryBean">
<constructor-arg index="0" value="127.0.0.1" /><!--Dsuperdiamond.host=***-->
<constructor-arg index="1" value="" /><!--Dsuperdiamond.port=***-->
<constructor-arg index="2" value="" /><!--Dsuperdiamond.projcode=***-->
<constructor-arg index="3" value="development" /><!--Dsuperdiamond.profile=***-->
</bean>
protected static PropertiesConfiguration propertieConfig = PropertiesConfigurationFactoryBean.getPropertiesConfiguration();
String value=propertieConfig.getString("jdbc.url");//key
当在spring配置文件中需要使用superDiamond的参数用于注入其他的bean时,就需要用${"你的key"}能够获取到配置参数了,添加如下配置:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="propertiesConfiguration" />
</bean>
或者配置这个也行
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="properties" ref="propertiesConfiguration" />
</bean>
参考文章:http://www.iteye.com/news/28294
添加配置栗子
1、-- 通过已知的key【项目代码中config.getString("key")】,获得PROJECT_ID
select PROJECT_ID from CONF_PROJECT_CONFIG where CONFIG_KEY='configkey';
2、-- 通过ID,获得PROJ_NAME
select PROJ_NAME from CONF_PROJECT where id='XX';--**系统
3、-- 通过系统名称,在对应的项目下添加配置
4、-- 验证页面添加的配置
select * from CONF_PROJECT_CONFIG where CONFIG_KEY='addconfigkey';
-- 查询出项目中所有的配置:
-- 项目编码--项目名称;
SELECT PROJ_CODE,PROJ_NAME,PROJECT_ID FROM CONF_PROJECT t WHERE PROJ_CODE='proj_xxxx';
SELECT CONFIG_KEY,CONFIG_VALUE FROM CONF_PROJECT_CONFIG WHERE PROJECT_ID='xx';
构造器
public PropertiesConfigurationFactoryBean(String host, int port, final String projCode, final String profile, final String modules) {
this(host, port, projCode, profile, modules, null);
}
spring.xml配置
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="properties" ref="propertiesConfiguration" />
</bean>
<bean id="propertiesConfiguration" class="com.github.diamond.client.PropertiesConfigurationFactoryBean">
<constructor-arg index="0" value="${app.server_host}" />
<constructor-arg index="1" value="${app.server_port}" />
<constructor-arg index="2" value="${artifactId}" />
<constructor-arg index="3" value="${profiles.active}" />
<constructor-arg index="4" value="${module}" />
</bean>
<context:property-placeholder location="classpath:properties/config-${env}.properties"/>
properties
#config-development.properties配置
#config-test.properties配置
#config-build.properties配置
#config-production.properties配置
### 配置中心
app.server_host=127.0.0.1
app.server_port=8283
artifactId=projectname
module=projectname
pom.xml
<profiles>
<profile>
<id>development</id>
<properties>
<profiles.active>development</profiles.active>
<env>development</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
<env>test</env>
</properties>
</profile>
<profile>
<id>build</id>
<properties>
<profiles.active>build</profiles.active>
<env>build</env>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<profiles.active>production</profiles.active>
<env>production</env>
</properties>
</profile>
</profiles>