分布式配置文件管理工具disconf部署以及运用

disconf之前有搭建过一次但是没有成功,比较纠结它的基于nginx实现动静分离的思想,其实明明可以少一个组件的依赖和配置,或许开发者有其自己的考虑吧。

这次终于搭建完毕并且跑通,下面写几个碰到但是官方文档中没给出的坎。

 

 

 

 

搭建项目

 --就是一个disconf-web

安装依赖软件

  • 安装Mysql(Ver 14.12 Distrib 5.0.45, for unknown-linux-gnu (x86_64) using EditLine wrapper)
  • 安装Tomcat(apache-tomcat-7.0.50)
  • 安装Nginx(nginx/1.5.3)
  • 安装 zookeeeper (zookeeper-3.3.0)
  • 安装 Redis (2.4.5)

准备配置

将你的配置文件放到此地址目录下(以下地址可自行设定):

home/work/dsp/disconf-rd/online-resources

配置文件包括:

- jdbc-mysql.properties (数据库配置)
- redis-config.properties (Redis配置)
- zoo.properties (Zookeeper配置)
- application.properties (应用配置)

注意,记得执行将application-demo.properties复制成application.properties:

cp application-demo.properties application.properties 

设置War包将要被部署的地址(以下地址可自行设定):

/home/work/dsp/disconf-rd/war

构建

ONLINE_CONFIG_PATH=/home/work/dsp/disconf-rd/online-resources
WAR_ROOT_PATH=/home/work/dsp/disconf-rd/war
export ONLINE_CONFIG_PATH
export WAR_ROOT_PATH
cd disconf-web
sh deploy/deploy.sh

这样会在 /home/work/dsp/disconf-rd/war 生成以下结果:

-disconf-web.war  
-html  
-META-INF  
-WEB-INF

上线前的初始化工作

初始化数据库:

可以参考 sql/readme.md 来进行数据库的初始化。

里面默认有6个用户

如果想自己设置初始化的用户名信息,可以参考代码来自己生成用户:

src/main/java/com/baidu/disconf/web/tools/UserCreateTools.java

部署War

修改server.xml文件,在Host结点下设定Context:

<Context path="" docBase="/home/work/dsp/disconf-rd/war"></Context>

并设置端口为 8015

启动Tomcat,即可。

部署 前端

修改 nginx.conf

upstream disconf {
    server 127.0.0.1:8015;
}

server {

    listen   8081;
    server_name localhost;
    access_log /home/work/var/logs/disconf/access.log;
    error_log /home/work/var/logs/disconf/error.log;

    location / {
        root /home/work/dsp/disconf-rd/war/html;
        if ($query_string) {
            expires max;
        }
    }

    location ~ ^/(api|export) {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://disconf;
    }
}

特别指出在打包项目时候请检查python库版本3.*版本会报错,请用2.*打包

第二点,在打出来的war包修改诸如zookeeper的配置时,必须重新打包,单纯修改war目录下的文件是无效的

 

 

 

 

 

 

 

 

 

 

 

 

 

使用

用户名密码 admin/admin

创建app,上传配置


1.添加disconf.properties配置文件

  disconf.user_define_download_dir=./                                 此处意思就是将zookeeper中的配置拉到本地的classpath下,若配置成其他目录,似乎会报错

Java代码   收藏代码
  1. disconf.enable.remote.conf=true  
  2. disconf.conf_server_host=172.171.51.151:8082  
  3. disconf.version=1.0.0.0  
  4. disconf.app=17wifiServer  
  5. disconf.env=local  
  6. disconf.ignore=  
  7. disconf.conf_server_url_retry_times=1  
  8. disconf.conf_server_url_retry_sleep_seconds=1  
  9. disconf.user_define_download_dir=./  
  10. disconf.enable_local_download_dir_in_class_path=true  

 

2.添加disconf.xml,并且指定给spring加载

配置如下

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  7.         http://www.springframework.org/schema/aop  
  8.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  9.     <aop:aspectj-autoproxy proxy-target-class="true"/>  
  10.     <!-- 使用disconf必须添加以下配置 -->  
  11.     <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"  
  12.           destroy-method="destroy">  
  13.         <property name="scanPackage" value="com.fnic.wifi.server"/>  
  14.     </bean>  
  15.     <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"  
  16.           init-method="init" destroy-method="destroy">  
  17.     </bean>  
  18.     <!--################################################################ -->  
  19.     <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload)-->  
  20.     <bean id="configproperties_disconf"  
  21.           class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">  
  22.         <property name="locations">  
  23.             <list>  
  24.                                 <value>classpath:jdbc.properties</value>  
  25.                 <value>classpath:redis.properties</value>  
  26.             </list>  
  27.         </property>  
  28.     </bean>  
  29.     <bean id="propertyConfigurer"  
  30.           class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">  
  31.         <property name="ignoreResourceNotFound" value="true"/>  
  32.         <property name="ignoreUnresolvablePlaceholders" value="true"/>  
  33.         <property name="propertiesArray">  
  34.             <list>  
  35.                 <ref bean="configproperties_disconf"/>  
  36.             </list>  
  37.         </property>  
  38.     </bean>    
  39. </beans>  

 配置上半段是必须配置,下半段是将配置文件托管给spring,并且可以运用${}这样的形式配置在spring的xml中,可以参考jdbc数据源的配置

其中

Java代码   收藏代码
  1. <value>classpath:jdbc.properties</value>  
  2. <value>classpath:redis.properties</value>  
  3. 要和上面  
  4. disconf.user_define_download_dir=./ 对应起来  

 

 

 

3.注释掉spring原本的加载配置类

Java代码   收藏代码
  1. <!--     <bean id="config"  
  2.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <list>  
  5.                 <value>classpath:conf/jdbc.properties</value>  
  6.                 <value>classpath:conf/redis.properties</value>  
  7.             </list>  
  8.         </property>  
  9.     </bean> -->    

 4.编写测试bean

Java代码   收藏代码
  1. @Component  
  2. @DisconfFile(filename = "configure.properties")  
  3. public class ConfDIs {  
  4.     private String resourceUrl;  
  5.   
  6.     @DisconfFileItem(name = "resource_server_url", associateField = "resourceUrl")  
  7.     public String getResourceUrl() {  
  8.         return resourceUrl;  
  9.     }  
  10.   
  11.     public void setResourceUrl(String resourceUrl) {  
  12.         this.resourceUrl = resourceUrl;  
  13.     }  
  14.   
  15. }  

 

ok启动就可以运用disconf了,其性能还有待验证,不过其原理应该是从zk中拉配置到本地内存中,同自己手动加载本地的property文件。

正常的日志如下

Java代码   收藏代码
  1. 2016-04-27 17:57:27 [com.baidu.disconf.client.DisconfMgr]-[INFO] ******************************* DISCONF END FIRST SCAN *******************************  
  2. 2016-04-27 17:57:28 [org.springframework.beans.GenericTypeAwarePropertyDescriptor]-[WARN] Invalid JavaBean property 'locations' being accessed! Ambiguous write methods found next to actually used [public void com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean.setLocations(java.util.List)]: [public void org.springframework.core.io.support.PropertiesLoaderSupport.setLocations(org.springframework.core.io.Resource[])]  
  3. 2016-04-27 17:57:28 [com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean]-[INFO] Loading properties file from class path resource [jdbc.properties]  
  4. 2016-04-27 17:57:28 [com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean]-[INFO] Loading properties file from class path resource [redis.properties]  
  5. 2016-04-27 17:57:28 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[INFO] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@63b7e77: defining beans [wifiAspect,activityDataBuilder,apActiveManageController,adDataBuilder,apAdController,apResourceController,resourceDataBuilder,apUserController,busController,busOnlineController,clickStatistics,confDIs,couponController,apFeedBackController,apFindAroundController,indexController,splashDataBuilder,messageSendController,sendMailController,pluginController,appScoreController,versionController,wifiAuthController,startListener,busServiceImpl,activityServiceImpl,apAdServiceImpl,apFeedBackSeriveImpl,appScoreServiceImpl,apResourceServiceImpl,apUserServiceImpl,busOnlineServiceImpl,couponServiceImpl,DAServiceImpl,findAroundServiceImpl,groupServiceImpl,indexServiceImpl,messageSendServiceImpl,pluginServiceImpl,versionServiceImpl,wifiAuthServiceImpl,httpService,springHolder,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,jedisPoolConfig,jedisConnFactory,stringRedisTemplate,objRedisTemplate,redisObj,redisString,redisTemplate,redisSubscribe,messageDelegateListener,serialization,messageListener,redisContainer,dataSource,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,sqlSessionFactory,org.mybatis.spring.mapper.MapperScannerConfigurer#0,transactionManager,disconfMgrBean,disconfMgrBean2,configproperties_disconf,propertyConfigurer,disconfAspectJ,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,activityDao,apAdDao,apFeedBackDao,appScoreDao,apResourceDao,apUserDao,busDao,busOnlineDao,couponDao,DADao,findAroundDao,groupDao,indexDao,messageSendDao,pluginDao,versionDao,wifiAuthDao]; root of factory hierarchy  
  6. 2016-04-27 17:57:28 [com.fnic.wifi.server.aop.WifiAspect]-[INFO] 初始化Controller Aop  
  7. 2016-04-27 17:57:30 [com.baidu.disconf.client.DisconfMgr]-[INFO] ******************************* DISCONF START SECOND SCAN *******************************  
  8. 2016-04-27 17:57:30 [com.baidu.disconf.client.DisconfMgr]-[INFO] Conf File Map:   
  9. disconf-file:   redis.properties      
  10.     DisconfCenterFile [  
  11.     keyMaps={}  
  12.     additionalKeyMaps={redis.maxWait=3000, redis.hostName=172.171.51.154, redis.maxIdle=5, redis.port=6380, redis.maxActive=500}  
  13.     cls=null  
  14.     remoteServerUrl=/api/config/file?app=17wifiServer&env=local&type=0&key=redis.properties&version=1.0.0.0]  
  15. disconf-file:   jdbc.properties   
  16.     DisconfCenterFile [  
  17.     keyMaps={}  
  18.     additionalKeyMaps={jdbc.url=jdbc:mysql://172.171.48.110:3306/wifimanage?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull, jdbc.minIdle=5, jdbc.username=root, jdbc.maxWait=3000, jdbc.removeAbandoned=true, jdbc.maxActive=15, jdbc.removeAbandonedTimeout=300, jdbc.maxIdle=30, jdbc.logAbandoned=true, jdbc.driver=com.mysql.jdbc.Driver, jdbc.password=root}  
  19.     cls=null  
  20.     remoteServerUrl=/api/config/file?app=17wifiServer&env=local&type=0&key=jdbc.properties&version=1.0.0.0]  
  21. disconf-file:   configure.properties      
  22.     DisconfCenterFile [  
  23.     keyMaps={resource_server_url=FileItemValue{value=http://172.171.51.151:8081/Resources2/, field=private java.lang.String com.fnic.wifi.server.controller.ConfDIs.resourceUrl, setMethod=null}}  
  24.     additionalKeyMaps={}  
  25.     cls=class com.fnic.wifi.server.controller.ConfDIs  
  26.     remoteServerUrl=/api/config/file?app=17wifiServer&env=local&type=0&key=configure.properties&version=1.0.0.0]  
  27.   
  28. 2016-04-27 17:57:30 [com.baidu.disconf.client.DisconfMgr]-[INFO] Conf Item Map:   
  29.   
  30. 2016-04-27 17:57:30 [com.baidu.disconf.client.DisconfMgr]-[INFO] ******************************* DISCONF END *******************************  

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值