disconf使用小結
目前我們公司用的分布式配置中心是disconf,對於普通的spring項目集成還是比較方便,主要功能點分布式配置還有配置的動態更新通知
安裝disconf服務端
參考地址https://disconf.readthedocs.io/zh_CN/latest/install/index.html
本人自我實踐,踩了一些坑
-
新建文件夾
本人使用的是mac,新建目錄/data/disconf,並執行以下命令mkdir -p /data/disconf/online-resources /data/disconf/tomcat /data/disconf/war /data/disconf/source
-
克隆disconf源碼
github地址https://github.com/knightliao/disconf,比如到切換到你自己的目錄/data/disconf/source,然后執行命令git clone https://github.com/knightliao/disconf
-
復制配置文件
復制源碼的disconf-web/profile/rd下面的配置文件到/data/disconf/online-resources下面,並修改application-demo.properties為application.properties,其余配置文件按照自己的服務進行配置,包括mysql,redis(里面有2個配置如果只有1個redis服務寫成一樣即可)和zk
-
打包部署
切換到源碼disconf-web目錄下面,修改deploy/deploy.sh,增加環境變量
export ONLINE_CONFIG_PATH=/data/disconf/online-resources export WAR_ROOT_PATH=/data/disconf/war
執行命令sh deploy/deploy.sh,完成后會在/data/disconf/war目錄下生成文件
-
配置tomcat
解壓一個tomcat到/data/disconf/tomcat目錄下,修改配置文件server.xml端口改為8015,然后在Host標簽下新增配置
<Context path="" docBase="/data/disconf/war"></Context>
-
初始化db數據
創建disconf數據庫,可以參考 源碼disconf-web/sql/readme.md 來進行數據庫的初始化。注意順序執行
- 0-init_table.sql
- 1-init_data.sql
- 201512/20151225.sql
- 20160701/20160701.sql
后面清楚測試數據,只要保留role,role_resource,user表數據即可,env表需要自己配置各個環境比如dev,test,pre,prod
-
配置nginx
upstream disconf { server 127.0.0.1:8015; } server { listen 80; server_name disconf.com; access_log /data/disconf/access.log; error_log /data/disconf/error.log; location / { root /data/disconf/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; } }
-
配置本機host
127.0.0.1 disconf.com
-
各個服務啟動
- mysql
- redis
- zookeeper
- tomcat
- nginx
-
訪問disconf服務端
客戶端使用
新建一個maven工程,添加依賴
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.36</version>
</dependency>
在src/main/resources下面新建applicationContext.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:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<aop:aspectj-autoproxy/>
<!-- 使用disconf必須添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="com.yaojiafeng.test.disconf"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>
<context:component-scan base-package="disconf"/>
</beans>
新建啟動類Application.java
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
while (true) {
Info info = (Info) context.getBean("info");
System.out.println(info.getPhone());
Name name = (Name) context.getBean("name");
System.out.println(unicodeToCn(name.getName()));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static String unicodeToCn(String unicode) {
/** 以 \ u 分割,因為java注釋也能識別unicode,因此中間加了一個空格*/
String[] strs = unicode.split("\\\\u");
String returnStr = "";
// 由於unicode字符串以 \ u 開頭,因此分割出的第一個字符是""。
for (int i = 1; i < strs.length; i++) {
returnStr += (char) Integer.valueOf(strs[i], 16).intValue();
}
return returnStr;
}
}
新建2個配置類Info和Name
package disconf;
import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfFileItem;
import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import org.springframework.stereotype.Service;
@Service
@DisconfFile(filename = "info.properties")
@DisconfUpdateService(classes = {Info.class}, itemKeys = {"name"})
public class Info {
private String phone;
@DisconfFileItem(name = "phone", associateField = "phone")
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
package disconf;
import com.baidu.disconf.client.common.annotations.DisconfItem;
import org.springframework.stereotype.Service;
@Service
public class Name {
private String name;
@DisconfItem(key = "name", associateField = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后在src/main/resources里新建disconf.properties配置文件內容
# 是否使用遠程配置文件
# true(默認)會從遠程獲取配置 false則直接獲取本地配置
enable.remote.conf=true
#
# 配置服務器的 HOST,用逗號分隔 127.0.0.1:8000,127.0.0.1:8000
#
conf_server_host=disconf.com
# 版本, 請采用 X_X_X_X 格式
version=1.0.0
# APP 請采用 產品線_服務名 格式
app=smc
# 環境
env=dev
# debug
debug=true
# 忽略哪些分布式配置,用逗號分隔
ignore=
# 獲取遠程配置 重試次數,默認是3次
conf_server_url_retry_times=1
# 獲取遠程配置 重試時休眠時間,默認是5秒
conf_server_url_retry_sleep_seconds=1
然后運行Application的main方法,即可打印出disconf拉取到的信息,並且嘗試在disconf服務端改配置,客戶端也立即能獲取到最新的配置。