disconf使用小结

时间:2023-03-09 06:08:41
disconf使用小结

disconf使用小结

目前我们公司用的分布式配置中心是disconf,对于普通的spring项目集成还是比较方便,主要功能点分布式配置还有配置的动态更新通知

安装disconf服务端

参考地址https://disconf.readthedocs.io/zh_CN/latest/install/index.html

本人自我实践,踩了一些坑

  1. 新建文件夹

    本人使用的是mac,新建目录/data/disconf,并执行以下命令mkdir -p /data/disconf/online-resources /data/disconf/tomcat /data/disconf/war /data/disconf/source

  2. 克隆disconf源码

    github地址https://github.com/knightliao/disconf,比如到切换到你自己的目录/data/disconf/source,然后执行命令git clone https://github.com/knightliao/disconf

  3. 复制配置文件

    复制源码的disconf-web/profile/rd下面的配置文件到/data/disconf/online-resources下面,并修改application-demo.properties为application.properties,其余配置文件按照自己的服务进行配置,包括mysql,redis(里面有2个配置如果只有1个redis服务写成一样即可)和zk

  4. 打包部署

    切换到源码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目录下生成文件

  5. 配置tomcat

    解压一个tomcat到/data/disconf/tomcat目录下,修改配置文件server.xml端口改为8015,然后在Host标签下新增配置

    <Context path="" docBase="/data/disconf/war"></Context>
  6. 初始化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
  7. 配置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;
    }
    }
  8. 配置本机host

    127.0.0.1 disconf.com

  9. 各个服务启动

    • mysql
    • redis
    • zookeeper
    • tomcat
    • nginx
  10. 访问disconf服务端

    http://disconf.com/,可以使用默认用户admin/admin登录,首先新建一个app叫smc,然后在新建一个配置文件叫info.properties,版本为1.0.0,环境选择dev,应用选择smc,输入配置名info.properties,文本为phone=18888888888,再新建一个配置项,名称为name,值为name。

客户端使用

新建一个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服务端改配置,客户端也立即能获取到最新的配置。