使用背景
随着业务需求的复杂,分布式服务的流行,配置文件的管理也成为一个问题?
今天介绍的disconf工具就能解决这个问题,如果你还为修改很多服务器上的配置文件而烦恼,那么你可以看看该工具的使用介绍.
disconf 环境搭建
从github上下载源代码,都知道这个是开源的,所以都可以下载源码,然后自己编译.
代码地址 : github上地址: https://github.com/knightliao/disconf
文档地址: https://github.com/knightliao/disconf/wiki
该项目运行需要依赖nginx,redis、zookeper、mysql,所以要安装之类的组件,在这里不一一介绍了,默认安装好,直接进行安装disconf项目
管理端disconf-web
首先需要java 和maven的编译环境.默认环境都安装好了.
cp ./disconf/disconf-web/profile/rd/* /usr/local/disconf/source #将git上检下的项目配置文件拷贝到指定的目录上,同时把application-demo.properties修改成application.properties(mv命令)
修改配置文件
将/usr/local/disconf/source下的这4个配置文件修改成自己环境相关的配置
application.properties 该文件中主要是配置了监控邮件发送和接受的服务器和邮箱地址
编译生成war包
将存放路径配置成环境变量,等会脚本需要使用
ONLINE_CONFIG_PATH= /usr/local/disconf/source
初始化数据库
tomcat配置
在tomcat的server.xml中加入 <Context path="" docBase="/usr/local/disconf/war"></Context>
nginx配置
在http这个标记对里面加上如下配置(/usr/local/nginx/conf/nginx.conf):
include /etc/nginx/mime.types; #指定浏览器对css加载的类型
(特别注意https://blog.csdn.net/m0_37904728/article/details/78745243 )
default_type application/octet-stream;#指定浏览器对css加载的类型
upstream disconf {
server 127.0.0.1:8080;
}
server {
listen 8991;
server_name localhost;
access_log /home/xxx/nginx/log/disconf/access.log;
error_log /home/xxx//nginx/log/disconf/error.log;
location / {
root /usr/local/xxx/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;
}
}
以上环境就配置好了,启动后访问:http://IP:8991/ 用户名:admin 密码:admin 出现以下的界面,这算是环境弄好了.

环境弄好了,那如何使用到项目中呢?
接下来就是将项目整合disconf,将配置文件交给disconf管理,使用maven作为例子.
导包:
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.31</version>
</dependency>
disconf支持两种开发模式,一种注解式,配置文件->配置文件可以无侵入模式
先来注解模式:
将disconf.properties文件放到resources目录下,内容如下:
# 是否使用远程配置文件
# true(默认)会从远程获取配置 false则直接获取本地配置
disconf.enable.remote.conf=true
#
# 配置服务器的 HOST,用逗号分隔 127.0.0.1:8000,127.0.0.1:8000
#
disconf.conf_server_host=172.16.7.217:8991
# 版本, 请采用 X_X_X_X 格式
disconf.version=1_0_0_0
# APP 请采用 产品线_服务名 格式
disconf.app=disconf_demo
# 环境
disconf.env=rd
# 忽略哪些分布式配置,用逗号分隔
disconf.ignore=
# 获取远程配置 重试次数,默认是3次
disconf.conf_server_url_retry_times=1
# 获取远程配置 重试时休眠时间,默认是5秒
disconf.conf_server_url_retry_sleep_seconds=1
# 自定义的下载路径
disconf.user_define_download_dir=./config
在applicationContext.xml里面添加如下内容:
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 使用disconf必须添加以下配置 -->
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="cn.disconf.test"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>
重点解释: <property name="scanPackage" value="cn.disconf.test"/> value填写的是discnf注解扫描的类路径
然后将之前的配置文件映射成java bean:
@Configuration
@DisconfFile(filename = "redis.properties")
public class JedisConfig{
protected static final Logger LOGGER = LoggerFactory
.getLogger(JedisConfig.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;
}
}
重点解释注解:
@DisconfFile 映射配置文件名称
@DisconfFileItem 映射配置文件内容 name :配置文件的key ,associateField : java bean的属性. 该注解写在bean属性的get方法上.
以上就是注解式开发,接下来就是写disconf的配置.

直接运行项目,在项目启动的时候,会到disconf上拉取配置文件到指定的本地目录下,同时会放一份到classes下一份.
再来说下配置文件模式:
配置文件用起来很简单,还是要用到disconf的核心disconf.properties文件.
# 是否使用远程配置文件
# true(默认)会从远程获取配置 false则直接获取本地配置
disconf.enable.remote.conf=true
#
# 配置服务器的 HOST,用逗号分隔 127.0.0.1:8000,127.0.0.1:8000
#
disconf.conf_server_host=172.16.7.217:8991
# 版本, 请采用 X_X_X_X 格式
disconf.version=1_0_0_0
# APP 请采用 产品线_服务名 格式
disconf.app=disconf_demo
# 环境
disconf.env=rd
# 忽略哪些分布式配置,用逗号分隔
disconf.ignore=
# 获取远程配置 重试次数,默认是3次
disconf.conf_server_url_retry_times=1
# 获取远程配置 重试时休眠时间,默认是5秒
disconf.conf_server_url_retry_sleep_seconds=1
# 自定义的下载路径
disconf.user_define_download_dir=./config
在applicationContext.xml文件中添加disconf需要的启动配置
<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.example.disconf.demo"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>
<!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload)-->
<bean id="configproperties_disconf"
class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:/redis.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer"
class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="propertiesArray">
<list>
<ref bean="configproperties_disconf"/>
</list>
</property>
</bean>
这里使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload),还有种配置更改不会自动reload,有兴趣可以看看官方的文档,这里就不在描述了.
这两种方式用起来是不是觉得都很简单啊!如果服务器很多,也不用去一台一台的去修改配置文件了.这个工具使用介绍就到这里了,如果有什么疑问或者问题可以联系,初次写博客,如果有什么不到位的地方,请大家多多包涵.同时大家有什么关于(java)新的技术要讨论的可以留言评论,我会去尝试研究然后一样写博客.如果没有时间限制,每个星期都要坚持也一篇关于技术使用的博客,希望大家多多给建议.




留下下次想了解的技术啊!谢谢......

DisConf 是一款用于分布式系统配置管理的工具,能够帮助开发者轻松管理和更新多个服务器上的配置文件。本文详细介绍 DisConf 的环境搭建过程及两种集成方式:注解式和配置文件模式。
404

被折叠的 条评论
为什么被折叠?



