一、需求
實現分布式配置中心:
(1)集中管理外部依賴的服務配置和服務內部配置
(2)提供web管理平台進行配置和查詢
(3)支持服務注冊與發現
(4)支持客戶端拉取配置
(5)支持訂閱與發布,配置變更主動通知到client,實時變更配置
二、開源解決方案
http://disconf.readthedocs.io/zh_CN/latest/index.html disconf官方wiki文檔及其他開源解決方案
注: disonf目前客戶端只支持Java版本 https://github.com/knightliao/disconf-demos-java
三、disconf-web部署
3.1 How to deploy
-
安裝依賴軟件
-
- 安裝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)
- 從github獲取disconf項目
[root@minion data]# git clone https://github.com/knightliao/disconf.git
-
目錄結構說明
[root@minion data]# tree disconf -L 2 disconf ├── disconf-client # 分布式配置客戶端模塊, 依賴disconf-core包。 用戶程序使用它作為Jar包進行分布式配置編程。 │ ├── deploy │ ├── pom.xml │ ├── README.md │ ├── src │ └── tools ├── disconf-core #分布式配置基礎包模塊 │ ├── pom.xml │ ├── README.md │ └── src ├── disconf-web #分布式配置平台服務模塊, 依賴disconf-core包。采用SpringMvc+純HTML方式實現。 用戶使用它來進行日常的分布式配置管理。 │ ├── bin │ ├── deploy │ ├── html │ ├── pom.xml │ ├── profile │ ├── README.md │ ├── sql #初始化數據庫sql文件 │ └── src ├── docs │ ├── make.bat │ ├── Makefile │ ├── myMake.sh │ ├── README.md │ └── source ├── LICENSE ├── pom.xml ├── README.md └── sql 16 directories, 13 files
-
配置准備
假定程序部署目錄為:/data/disconf 配置模板文件路徑為:/data/disconf/disconf-web/profile/rd
[root@minion disconf]# cd /data/disconf/ 進入程序目錄 [root@minion disconf]# mkdir -p /data/disconf/disconf-rd/{online-resources,war} #創建配置文件存放目錄及war包存放路徑,可自行設定目錄 [root@minion disconf]# cd /data/disconf/disconf-web/profile/rd/ 進入模板文件路徑 [root@minion rd]# ls application-demo.properties jdbc-mysql.properties log4j.properties logback.xml redis-config.properties zoo.properties [root@minion rd]# cp application-demo.properties jdbc-mysql.properties redis-config.properties zoo.properties /data/disconf/disconf-rd/online-resources/ #復制模板配置文件 [root@minion rd]# ll /data/disconf/disconf-rd/online-resources/ total 16 -rw-r--r-- 1 root root 419 Apr 8 15:54 application-demo.properties #應用配置,郵件及應用相關 -rw-r--r-- 1 root root 370 Apr 8 15:54 jdbc-mysql.properties #數據庫配置 -rw-r--r-- 1 root root 498 Apr 8 15:54 redis-config.properties #Redis配置,主要用於web登錄使用
-rw-r--r-- 1 root root 130 Apr 8 15:54 zoo.properties #Zookeeper配置
[root@minion rd]# cd /data/disconf/disconf-rd/online-resources/
[root@minion online-resources]# cp application-demo.properties application.properties #切記,將模板文件application-demo.properties復制成application.properties
-
注意,即使只有一個redis,也應該配置兩個redis client,否則將造成內部錯誤。
[root@minion online-resources]# cat redis-config.properties redis.group1.retry.times=2 redis.group1.client1.name=BeidouRedis1 redis.group1.client1.host=127.0.0.1 redis.group1.client1.port=6379 redis.group1.client1.timeout=5000 redis.group1.client1.password=foobared redis.group1.client2.name=BeidouRedis2 redis.group1.client2.host=127.0.0.1 redis.group1.client2.port=6380 redis.group1.client2.timeout=5000 redis.group1.client2.password=foobared redis.evictor.delayCheckSeconds=300 redis.evictor.checkPeriodSeconds=30 redis.evictor.failedTimesToBeTickOut=6
-
項目構建
注: 安裝前需確保mysql/tomcat/redis/zookeeper軟件環境部署完成
[root@minion disconf]# ONLINE_CONFIG_PATH=/data/disconf/disconf-rd/online-resources [root@minion disconf]# WAR_ROOT_PATH=/data/disconf/disconf-rd/war [root@minion disconf]# export ONLINE_CONFIG_PATH [root@minion disconf]# export WAR_ROOT_PATH [root@minion disconf]# cd disconf-web [root@minion disconf-web]# sh deploy/deploy.sh
腳本運行結束后會在/data/disconf/disconf-rd/war/路徑下生成web部署所用到的程序文件
-disconf-web.war -html -META-INF -WEB-INF
3.2 數據庫初始化
mysql> select * from disconf.user; +---------+------------+------------------------------------------+------------------------------------------+---------+---------+ 用戶名對應的密碼 | user_id | name | password | token | ownapps | role_id | +---------+------------+------------------------------------------+------------------------------------------+---------+---------+ | 1 | testUser1 | 5eec8499597a115c88e0a9580ae1562ab85d0b1a | b9070d385a13357efa09e50e080607c2b299241b | 2 | 1 | -->MhxzKhl9209 | 2 | testUser2 | 71e9dc667eefa5a3a4840cb4f1ce22bc246f22f0 | b169dec42f61ec6cbad88d70e7c4c6b89630ccfb | 2 | 1 | -->MhxzKhl167 | 3 | testUser3 | e2cdc4a9195030543e38e19a923f075d54471cc4 | a1a20b0e03a5191c530cbfc064eda3c16254df64 | 2 | 1 | -->MhxzKhl783 | 4 | testUser4 | 5cef2d7e4ada5a615f03e12b569d80aedfb056fc | 007b07fccbc1c82c987f7b8e4651e85cca01cf2b | 2 | 1 | -->MhxzKhl8758 | 5 | testUser5 | f996eeaa224abe0037d99adbce73c315e13238f9 | c9dfdcb50a3d84f2b6a4771dcb7c2ceb19e7d281 | 2 | 1 | -->MhxzKhl112 | 6 | admin | d033e22ae348aeb5660fc2140aec35850c4da997 | f28d164d23291c732f64134e6b7d92be3ff8b1b3 | | 2 | -->admin | 7 | admin_read | b76f3e20d1c8d0bc17d40158e44097d5eeee8640 | 2022ab9c2754d62f9ddba5fded91e4238247ebaf | 2 | 3 | | 8 | mobiledsp | 0855b44a368e44dc6e6825532073b29a368584af | 132069654193f802203d1c6c86e753ecede698f6 | 4 | 1 | +---------+------------+------------------------------------------+------------------------------------------+---------+---------+ 8 rows in set (0.00 sec) 如果想自己設置初始化的用戶名信息,可以參考代碼來自己生成用戶: [root@minion disconf-web]# cat /data/disconf/disconf-web/src/main/java/com/baidu/disconf/web/tools/UserCreateTools.java 源代碼文件
3.3 部署war包
[root@shuke conf]# vim /opt/apache-tomcat-9.0.0.M18/conf/server.xml ......
69 <Connector port="8014" protocol="HTTP/1.1" #設定端口為8014,可自行設定端口
70 connectionTimeout="20000"
71 redirectPort="8443" />
....... 148 <Host name="localhost" appBase="webapps" 149 unpackWARs="true" autoDeploy="true"> 150 <Context path="" docBase="/data/disconf/disconf-rd/war"></Context> #增加內容
[root@shuke bin]# sh /opt/apache-tomcat-9.0.0.M18/bin/startup.sh 啟動tomcat程序
3.4 部署前端nginx代理
[root@shuke conf.d]# cat /etc/nginx/conf.d/disconf.conf upstream disconf { server 127.0.0.1:8014; #代理tomcat,動態請求轉發至后端tomcat程序 } server { listen 8081; server_name 192.168.1.100; #如果使用域名,可以設置為disconf.com(可以自定義),但它必須與 application.properties配置文件中的domain保持一致。 access_log /var/logs/disconf/access.log; error_log /var/logs/disconf/error.log; location / { root /data/disconf/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; } location = /favicon.ico { log_not_found off; access_log off; } }
瀏覽器訪問:http://192.168.1.100:8081, 訪問異常可查看nginx或tomcat日志
3.5 disconf-web使用說明
首頁,點擊登陸后,輸入用戶名:admin,密碼:admin登陸即可

左上角可以選擇APP和環境,選擇之后,就會在中間出現若干個版本,
選擇版本后,就會顯示 APP、環境、版本 三個條件下的配置列表:


表格中 各個列的意義是:
- APP:使用哪個APP,及它的ID
- KEY:配置文件或配置項
- 配置內容:配置文件或配置項在配置中心中的值
- 實例列表:使用此配置文件或配置項的所有實例列表,及每個實例的配置值。如果實例的配置值與配置中心的值不一致,這里會標識出來。
- 修改時間:修改此配置的最后一次時間
- 操作:個性、刪除、下載
右上角可以
新建配置項、新建配置文件、新建APP
表格右上方
可以批量下載所有配置文件至本地,還可以查看ZK上的部署情況。
至此,disconf-web端已經部署完成!