Disconf入門指南(1)
Disconf簡介
參考:
https://github.com/knightliao/disconf/wiki/TutorialSummary
在一個分布式環境中,同類型的服務往往會部署很多實例。這些實例使用了一些配置,為了更好地維護這些配置就產生了配置管理服務。通過這個服務可以輕松地管理成千上百個服務實例的配置問題。
王阿晶提出了基於zooKeeper的配置信息存儲方案的設計與實現[1], 它將所有配置存儲在zookeeper上,這會導致配置的管理不那麽方便,而且他們沒有相關的源碼實現。淘寶的diamond[2]是淘寶內部使用的一個管理持久配置的系統,它具有完整的開源源碼實現,它的特點是簡單、可靠、易用,淘寶內部絕大多數系統的配置都采用diamond來進行統一管理。他將所有配置文件裏的配置打散化進行存儲,只支持KV結構,並且配置更新的推送是非實時的。百度內部的BJF配置中心服務[3]采用了類似淘寶diamond的實現,也是配置打散化、只支持KV和非實時推送。
同構系統是市場的主流,特別地,在業界大量使用部署虛擬化(如JPAAS系統,SAE,BAE)的情況下,同一個系統使用同一個部署包的情景會越來越多。但是,異構系統也有一定的存在意義,譬如,對於“拉模式”的多個下遊實例,同一時間點只能只有一個下遊實例在運行。在這種情景下,就存在多臺實例機器有“主備機”模式的問題。目前國內並沒有很明顯的解決方案來統一解決此問題。
Disconf安裝
參考
- Git:https://github.com/knightliao/disconf/tree/master/disconf-web
- OS China: http://www.oschina.net/p/disconf/similar_projects?fromerr=vfYXkqGn
- 個人網站 http://www.liaoqiqi.com/post/105
安裝依賴軟件
- 安裝 Mysql
- 安裝 Tomcat(apache-tomcat-7.0.50)
- 安裝 Nginx(nginx/1.5.3)
- 安裝 zookeeeper (zookeeper-3.3.0)
- 安裝 Redis (2.4.5)
準備配置文件
git clone https://github.com/knightliao/disconf.git
cd disconf-master
mkdir build
mkdir -p build/online-resources
mkdir -p build/war
- 1
- 2
- 3
- 4
- 5
下載好對應的代碼,準備對應的配置文件:將disconf-web/profile/rd目錄下的文件copy到build/online-resources目錄下
- jdbc-mysql.properties (數據庫配置)
- redis-config.properties (Redis配置)
- zoo.properties (Zookeeper配置)
- application.properties (應用配置)
- 1
- 2
- 3
- 4
註意,記得執行將application-demo.properties復制成application.properties:
cp application-demo.properties application.properties
- 1
構建
ONLINE_CONFIG_PATH=/home/dev1/Downloads/disconf/build/online-resources
WAR_ROOT_PATH=/home/dev1/Downloads/disconf/build/war
export ONLINE_CONFIG_PATH
export WAR_ROOT_PATH
cd disconf-web
sh deploy/deploy.sh
- 1
- 2
- 3
- 4
- 5
- 6
編譯完成後,會在 /home/dev1/Downloads/disconf/build/war 生成以下結果:
-disconf-web.war
-html
-META-INF
-WEB-INF
- 1
- 2
- 3
- 4
部署
Disconf代碼設計采用了靜動分離設計,整個的代碼的前後端完全分離。
初始化數據庫:
對應的數據庫腳本都在disconf-web/sql目錄下,依次執行對應的sql語句就可以了
mysql -u username -p password < 0-init_table.sql
mysql -u username -p password -Ddisconf < 1-init_data.sql
mysql -u username -p password -Ddisconf < 201512/20151225.sql
- 1
- 2
- 3
用戶數據:可以參考 sql/readme.md 來進行數據庫的初始化。裏面默認有6個用戶
如果想自己設置初始化的用戶名信息,可以參考代碼來自己生成用戶:
src/main/java/com/baidu/disconf/web/tools/UserCreateTools.java
動靜部署
Disconf代碼采用了動靜分離設計,
部署War(tomcat)
vim /etc/tomcat7/server.xml
修改server.xml文件,在Host結點下添加Context:
<Context path="" docBase="/usr/local/disconf/build/war"></Context>
- 1
並在對應的war目錄下創建tmp目錄,啟動tomcat服務器。
部署前端(nginx)
修改 nginx.conf,在HTTP標簽中添加以下內容:
upstream disconf {
server 127.0.0.1:8080;
}
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
註意:訪問的html路徑應該具有訪問權限,可以在在nginx.conf文件第一行中更改用戶,或者更改對應的HTML文件的權限。靜態配置成功後,可以通過查看tomcat的日誌和nginx日誌文件來查看訪問的記錄,靜態文件由nginx直接處理,而動態文件則是tomcat來處理的。
業務功能說明
支持用戶登錄/登出
瀏覽配置
按 APP/版本/環境 選擇
修改配置
修改配置
修改配置項
修改配置文件
新建配置
新建配置項
新建配置文件
新建APP
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Client使用
在spring boot中使用
1. 創建maven工程,添加對應的Maven庫
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.egoo</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.31</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
2.添加對應的屬性文件/resources
disconf.properties
disconf.enable.remote.conf=true
disconf.conf_server_host=192.168.1.169:8080
disconf.version=V2
disconf.app=FreeLink
disconf.env=local
disconf.ignore=
disconf.conf_server_url_retry_sleep_seconds=1
disconf.user_define_download_dir=./disconf/download
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
disconf.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 使用disconf必須添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="com.egoo.config"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
Disconf啟動需要加載的類,以及需要掃描的包文件目錄
3.添加對應的代碼 redis示例
RedisConfig.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfFileItem;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
/**
* Created by fiboliu on 16-3-16.
*/
@Configuration
@DisconfFile(filename = "redis.properties")
public class RedisConfig implements IDisconfUpdate {
protected static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class);
// 代表連接地址
private String host;
// 代表連接port
private int port;
/**
* 地址, 分布式文件配置
*
* @return
*/
@DisconfFileItem(name = "redis.host", associateField = "host")
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
/**
* 端口, 分布式文件配置
*
* @return
*/
@DisconfFileItem(name = "redis.port", associateField = "port")
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public void reload() throws Exception {
LOGGER.info("host: " + host);
}
}
Disconf入門指南(1)