前言
集群應用的配置文件如果寫在項目的 resources 目錄下面,當遇到需要修改某一個配置值時,需要將集群的所有應用的配置信息進行修改,並且將機密的配置信息比如數據庫賬號密碼如果不進行加密配置在項目中很危險,一旦發生代碼泄露問題,后果很嚴重。
為了避免上述情況發生,將配置信息存儲到數據庫中,比如數據庫連接、用戶名、以及密碼,通過 Config 項目的一個接口提供獲取配置信息。Config 項目只用於讀取配置信息。
遠程配置
一)新建類 RemoteProperties
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class RemoteProperties implements InitializingBean, FactoryBean<Properties> {
private String url = null;
private Properties properties = new Properties();
@Override
public Properties getObject() throws Exception {
return properties;
}
@Override
public Class<?> getObjectType() {
return properties.getClass();
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void afterPropertiesSet() throws Exception {
loadProperty();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private void loadProperty() {
if (StringUtil.strIsNull(url)) return;
String content = HttpClientUtil.urlGet(url);
JSONObject object = JSONObject.parseObject(content);
JSONArray data = object.getJSONArray("datasource");
for (Object obj : data) {
JSONObject jsonObject = (JSONObject) obj;
String key = obj.getString("key");
String value = obj.getString("value");
properties.put(key, value);
}
}
}
此類用於發送請求獲取配置信息,請求返回格式為 JSON 的配置信息, 如:
{
"datasource":[
{
"value":"com.mysql.jdbc.Driver",
"key":"jdbc.driver"
},
{
"value":"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8",
"key":"jdbc.url"
},
{
"value":"root",
"key":"jdbc.username"
},
{
"value":"root",
"key":"jdbc.password"
}
]
}
二)編寫 Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="propertyConfigurerUserServer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<bean id="remoteProperties" class="com.craft.partner.server.util.RemoteProperties"
p:url="http://api.xxx.com/config"/>
<!--遠程配置提供接口-->
</property>
<!--還可以加載本地的properties文件-->
<property name="locations">
<list>
<value>classpath:configure.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="${jdbc.driver}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}">
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="50" />
<property name="maxStatements" value="0" />
<property name="maxIdleTime" value="600" />
<property name="idleConnectionTestPeriod" value="300" />
<property name="acquireIncrement" value="5" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="2000" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true"/><!--其他不符合規范的方法只允許讀操作-->
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="false">
<aop:pointcut id="serviceMethod" expression="execution(* com.craft.partner.server.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
</beans>
注意: 通過 p:url="地址" 的方式,調用 RemoteProperties 的方法,發送請求獲取配置信息,通過 Spring 進行注入。
注入后,可以通過 p:屬性 的方式獲取配置。