Disconf开发参考

来源:互联网 发布:课件制作软件director 编辑:程序博客网 时间:2024/06/03 08:52

可行性分析

一、可靠性保证                                                                 基于zookeeper的主备切换方案。                                                             二、支持的数据类型                                                                   属性文件和键值对                                                                三、集成方式                                                                  代码侵入小,改动比较少,配置文件自动更新。符合咱们项目的方式。                                                             四、数据存储                                                                  数据存储在mysql,localFile,zk                                                             

不足

一、不能管理多目录文件。比如同时维护spring和properties目录。需修改源码实现。                                                              

开发指南

1.在pom中引入包                                                              <dependency>                                                                    <groupId>com.baidu.disconf</groupId>                                                                    <artifactId>disconf-client</artifactId>                                                                 <version>2.6.31</version>                                                               </dependency>                                                               <dependency>                                                                    <groupId>com.baidu.disconf</groupId>                                                                    <artifactId>disconf-core</artifactId>                                                                   <version>2.6.31</version>                                                               </dependency>                                                               <dependency>                                                                    <groupId>org.reflections</groupId>                                                                  <artifactId>reflections</artifactId>                                                                    <version>0.9.9-RC1</version>                                                                </dependency>                                                               <dependency>                                                                    <groupId>org.apache.httpcomponents</groupId>                                                                    <artifactId>httpclient</artifactId>                                                                 <version>4.5</version>                                                              </dependency>                                                               <dependency>                                                                    <groupId>com.google.code.gson</groupId>                                                                 <artifactId>gson</artifactId>                                                                   <version>2.5</version>                                                              </dependency>                                                               

2.添加spring配置。

    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"                                                                 destroy-method="destroy">                                                             <property name="scanPackage" value="amy.gs.disconf"/>                                                           </bean>                                                         <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"                                                                  init-method="init" destroy-method="destroy">                                                          </bean>                                                         <bean id="configproperties_disconf" class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">                                                             <property name="locations">                                                                 <list>                                                                      <value>classpath:properties/common.properties</value>                                   //项目用到哪几个就配置哪几个。                                    <value>classpath:properties/datasource.properties</value>                                                                       <value>classpath:properties/email.properties</value>                                                                        <value>classpath:properties/file.properties</value>                                                                     <value>classpath:properties/redis.properties</value>                                                                    </list>                                                             </property>                                                         </bean>                                                         <bean id="propertyConfigurer" class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">                                                                <property name="ignoreResourceNotFound" value="true"/>                                                              <property name="ignoreUnresolvablePlaceholders" value="true"/>                                                              <property name="propertiesArray">                                                                   <list>                                                                      <ref bean="configproperties_disconf"/>                                                                  </list>                                                             </property>                                                         </bean>                                                         <bean id="autoService" class="amy.gs.disconf.ser.AutoService">                                                              <property name="auto" value="${auto=100}"/>                                                            </bean>                                                     

3.添加java bean。(添加回调函数,主要控制配置自动刷新。)
package amy.gs.disconf.ser;
public class AutoService {
private String auto;
public String getAuto() {
return auto;
}
public void setAuto(String auto) {
this.auto = auto;
}
}

package amy.gs.disconf.ser.callbacks;                                                               @Service                                                                @DisconfUpdateService(confFileKeys = {"common.properties", "datasource.properties","email.properties","file.properties","redis.properties"})                                                                //与spring配置中的一致。public class AutoServiceCallback implements IDisconfUpdate {                                                                     protected static final Logger LOGGER = LoggerFactory.getLogger(AutoServiceCallback.class);                                                                  @Autowired                                                                 private AutoService autoService;                                                                    @Override                                                                   public void reload() throws Exception {                                                                      LOGGER.info("reload callback " + "common.properties or datasource.properties or email.properties or file.properties or redis.properties " + autoService.getAuto());                                                                    }                                                               }                                                               

4.在项目的classpath root目录下添加配置文件:disconf.properties
disconf.enable.remote.conf=true
disconf.conf_server_host=172.20.15.113:8081 //disconf服务地址
disconf.version=1.0.0 //配置文件版本
disconf.app=amy_mid //所属APP
disconf.env=dev_178 //所属环境
disconf.ignore= //指定不需要管理的配置文件
disconf.conf_server_url_retry_times=1
disconf.conf_server_url_retry_sleep_seconds=1
disconf.user_define_download_dir=src/main/resources/properties //配置文件下载目录
disconf.enable_local_download_dir_in_class_path=true

5.dubbo.properties配置文件拷贝到properties目录中。

6.更新dubbo.2.5.3.jar包。

原创粉丝点击