disconf使用小结
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服务端改配置,客户端也立即能获取到最新的配置。
disconf使用小结的更多相关文章
- disconf原理 “入坑”指南
之前有了解过disconf,也知道它是基于zookeeper来做的,但是对于其运行原理不太了解,趁着周末,debug下源码,也算是不枉费周末大好时光哈 :) .关于这篇文章,笔者主要是参考discon ...
- disconf原理解析
前有了解过disconf,也知道它是基于zookeeper来做的,特意写了文章记录下自己的见解.如有错误,欢迎指正. 1.disconf-web会在启动时,将自身的host和配置文件注册到zookee ...
- 从零开始编写自己的C#框架(26)——小结
一直想写个总结,不过实在太忙了,所以一直拖啊拖啊,拖到现在,不过也好,有了这段时间的沉淀,发现自己又有了小小的进步.哈哈...... 原想框架开发的相关开发步骤.文档.代码.功能.部署等都简单的讲过了 ...
- Python自然语言处理工具小结
Python自然语言处理工具小结 作者:白宁超 2016年11月21日21:45:26 目录 [Python NLP]干货!详述Python NLTK下如何使用stanford NLP工具包(1) [ ...
- java单向加密算法小结(2)--MD5哈希算法
上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...
- iOS--->微信支付小结
iOS--->微信支付小结 说起支付,除了支付宝支付之外,微信支付也是我们三方支付中最重要的方式之一,承接上面总结的支付宝,接下来把微信支付也总结了一下 ***那么首先还是由公司去创建并申请使用 ...
- iOS 之UITextFiled/UITextView小结
一:编辑被键盘遮挡的问题 参考自:http://blog.csdn.net/windkisshao/article/details/21398521 1.自定方法 ,用于移动视图 -(void)mov ...
- K近邻法(KNN)原理小结
K近邻法(k-nearst neighbors,KNN)是一种很基本的机器学习方法了,在我们平常的生活中也会不自主的应用.比如,我们判断一个人的人品,只需要观察他来往最密切的几个人的人品好坏就可以得出 ...
- scikit-learn随机森林调参小结
在Bagging与随机森林算法原理小结中,我们对随机森林(Random Forest, 以下简称RF)的原理做了总结.本文就从实践的角度对RF做一个总结.重点讲述scikit-learn中RF的调参注 ...
随机推荐
- Java基础--面向对象编程4(多态)
1.多态的概念 多态是指程序中的同一引用类型,使用不同的实例而执行结果不同的. 同一个类型:一般指父类 不同的实例:不同的子类实例 执行结果不同:针对同一方法执行的结果不同 package cn.sx ...
- Apicloud学习第三天——获取云数据库的数据方法
apicloud学习30天中的对用进行注册和登录以及数据的获取的代码,在apicloud中有单独的api对用户的增删查改进行操作,这里写下增加和查询. 增加用户数据 var model=api.req ...
- TCP和UDP的优缺点及区别
1.TCP是什么? TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议. TCP的优点: 可靠,稳定 TCP的可靠体 ...
- P5283 [十二省联考2019]异或粽子
考场上想到了没打完,细节思路还是不是很优,我原先的想法是每一次找完后标记那个点,下次再继续找(并不是这个意思,说不清楚)但实际上和平衡树一样加个大小就很好写了 #include<bits/std ...
- Linux-Shell编程之数组操作
源码 #!/bin/bash str="Array - Demo Shell"; echo ${#str} #求字符串長度 #定義 arr=('a' 'b' 'c' 'd' 'e' ...
- go之路
目录 go初识[第一篇]初识 go初识[第二篇]包.变量.函数
- win10 64位Python 3.6.2 + Django 环境安装
一.安装Python3.6.2 1.下载安装包 https://www.python.org/downloads/release/python-362/ 2.一直下一步,记得到了这个界面全部勾选再下一 ...
- 一个疑问,int对象5为何没有__dict__属性,而类却有,这是怎么做到的?对象不是都可以调用类属性吗?
a=1 print hasattr(a.__dict__) print hasattr(a.__class__.__dict__)
- 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165237
2018-2019-2 <网络对抗技术>Exp0 Kali安装 Week1 20165237 安装虚拟机 首先创建虚拟机 创建好虚拟机后,打开虚拟机进行安装.第一步选择Graphcal i ...
- 分布式系列十三: nginx
nginx偏运维, 不过作为开发应该了解它能做什么事情, 其作为技术架构的一部分必不可少 正向代理和反向代理 正向代理是代理的客户端, 反向代理是代理的服务端. nginx就是一款可以作反向代理的we ...