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,上传配置


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


1.添加disconf.properties配置文件

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


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

1. disconf.enable.remote.cnotallow=true
2. disconf.conf_server_host=172.171.51.151:8082
3. disconf.versinotallow=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_secnotallow=1
9. disconf.user_define_download_dir=./  
10. disconf.enable_local_download_dir_in_class_path=true

 

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

配置如下


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


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

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

其中


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

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

 

 

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


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

1. <!--     <bean id="config"
2. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
3. "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


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

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


 

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

正常的日志如下


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

1. 2016-04-27 17:57:27
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
3. 2016-04-27 17:57:28 [com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean]-[INFO] Loading properties file from class
4. 2016-04-27 17:57:28 [com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean]-[INFO] Loading properties file from class
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
7. 2016-04-27 17:57:30
8. 2016-04-27 17:57:30
9. disconf-file:   redis.properties      
10.     DisconfCenterFile [  
11.     keyMaps={}  
12. 3000, redis.hostName=172.171.51.154, redis.maxIdle=5, redis.port=6380, redis.maxActive=500}  
13. null
14. 0&key=redis.properties&version=1.0.0.0]  
15. disconf-file:   jdbc.properties   
16.     DisconfCenterFile [  
17.     keyMaps={}  
18. //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. null
20. 0&key=jdbc.properties&version=1.0.0.0]  
21. disconf-file:   configure.properties      
22.     DisconfCenterFile [  
23. //172.171.51.151:8081/Resources2/, field=private java.lang.String com.fnic.wifi.server.controller.ConfDIs.resourceUrl, setMethod=null}}
24.     additionalKeyMaps={}  
25. class
26. 0&key=configure.properties&version=1.0.0.0]  
27.   
28. 2016-04-27 17:57:30
29.   
30. 2016-04-27 17:57:30