Spring Cloud 集成 disconf-client

Disconf web的安装配置

Disconf web是Disconf的管理界面及服务器,通过它来配置管理我们的配置文件。

客户端配置

(1)创建普通的SpringBoot项目config-disconf,在pom.xml文件中增加如下依赖

1
2
3
4
5
<dependency>
<groupId>com.baidu.disconf</groupId>
<artifactId>disconf-client</artifactId>
<version>2.6.36</version>
</dependency>

(2)本地创建conf/spring-disconf.xml配置文件

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
<?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="cn.appblog.service.provider.config"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>

<!-- 需要托管的配置文件 -->
<bean id="configproperties_no_reloadable_disconf"
class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
<property name="locations">
<list>
<value>application.yml</value>
<value>disconf.properties</value>
</list>
</property>
</bean>

</beans>

配置scanPackage(扫描配置文件)
配置locations(本地配置文件)

(3)本地新增disconf配置文件disconf.properties

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
# 是否使用远程配置文件
# true(默认)会从远程获取配置 false则直接获取本地配置
disconf.enable.remote.conf=true

#
# 配置服务器的 HOST,用逗号分隔 127.0.0.1:8000,127.0.0.1:8000
#
disconf.conf_server_host=192.168.1.20:8015

# 版本, 请采用 X_X_X_X 格式
disconf.version=1_0_0_0

# APP 请采用 产品线_服务名 格式
disconf.app=service-provider

# 环境
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

#disconf.enable_local_download_dir_in_class_path=false

(4)本地应用配置文件application.yml

1
2
3
4
5
6
server:
port: 8081

spring:
application:
name: service-provider

(5)在ConfigDisconfApplication上导入依赖文件

1
2
3
4
5
6
7
8
@ImportResource({"classpath:conf/spring-disconf.xml"})
@SpringBootApplication
public class ConfigDisconfApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigDisconfApplication.class, args);
}
}

至此,客户端已经配置完毕

服务端配置

新建配置APP

APP名称需与disconf.properties中的disconf.app配置一致

新建配置文件

(1)application.yml

选择输入文本方式,内容与本地application.yml一致,上传配置文件容易导致中文乱码

(2)disconf.properties

选择输入文本方式,内容与本地disconf.properties一致,上传配置文件容易导致中文乱码

(3)server.properties

1
2
host=192.168.1.1
port=8818

(4)启动应用,可以看到配置已经生效

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
--------------- LOAD CONFIG START ---------------
SERVER disconf.conf_server_host: [192.168.1.20:8015]
SERVER disconf.version: 1_0_0_0
SERVER disconf.app: service-provider
SERVER disconf.env: rd
SERVER disconf.enable.remote.conf: true
SERVER disconf.debug: false
SERVER disconf.user_define_download_dir: config
SERVER disconf.ignore: []
--------------- LOAD CONFIG END ---------------
******************************* DISCONF START FIRST SCAN *******************************
given scan urls are empty. set urls in the configuration
execute http request [null], status code [200]
execute http request [null], status code [200]
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
zookeeper: 192.168.1.23:2181,192.168.1.24:2181,192.168.1.25:2181 , connected.
zoo prefix: /disconf
exists connect lost... will retry 0 org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /disconf
sleep 2
exists connect lost... will retry 1 org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /disconf
sleep 4
exists connect lost... will retry 2 org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /disconf
sleep 6
zk SyncConnected
******************************* DISCONF END FIRST SCAN *******************************
Tomcat initialized with port(s): 8081 (http)
Starting service [Tomcat]
Starting Servlet engine: [Apache Tomcat/9.0.21]
Initializing Spring embedded WebApplicationContext
Root WebApplicationContext: initialization completed in 38823 ms
******************************* DISCONF START SECOND SCAN *******************************
Conf File Map:
disconf-file: server.properties
DisconfCenterFile [
keyMaps={port=FileItemValue{value=8812, field=private int cn.appblog.service.provider.config.ServerConfig.port, setMethod=public void cn.appblog.service.provider.config.ServerConfig.setPort(int)}, host=FileItemValue{value=192.168.1.1, field=private java.lang.String cn.appblog.service.provider.config.ServerConfig.host, setMethod=public void cn.appblog.service.provider.config.ServerConfig.setHost(java.lang.String)}}
additionalKeyMaps={}
cls=class cn.appblog.service.provider.config.ServerConfig
remoteServerUrl=/api/config/file?version=1_0_0_0&app=service-provider&env=rd&key=server.properties&type=0]

Conf Item Map:

******************************* DISCONF END *******************************
Invalid JavaBean property 'locations' being accessed! Ambiguous write methods found next to actually used [public void com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean.setLocations(java.util.List)]: [public void org.springframework.core.io.support.PropertiesLoaderSupport.setLocations(org.springframework.core.io.Resource[])]
create: /disconf/service-provider_1_0_0_0_rd
create: /disconf/service-provider_1_0_0_0_rd/file
create: /disconf/service-provider_1_0_0_0_rd/file/application.yml
create: /disconf/service-provider_1_0_0_0_rd/file/disconf.properties
create: /disconf/service-provider_1_0_0_0_rd/file/server.properties

(5)disconf-web状态更新

![Disconf 配置](http://www.yezhou.me/AppBlog/images/Java/Disconf 配置.png)

(6)修改配置文件,看看是否实时推送配置更新

1
2
3
============GOT UPDATE EVENT WatchedEvent state:SyncConnected type:NodeDataChanged path:/disconf/service-provider_1_0_0_0_rd/file/server.properties: (/disconf/service-provider_1_0_0_0_rd/file/server.properties,server.properties,配置文件)======================
start to call class cn.appblog.service.provider.config.ServerConfig$$EnhancerBySpringCGLIB$$1937a869
配置文件更新!更新为:[ServerConfig{host='192.168.1.1', port=8818}]

小结

Disconf对于分布式系统非常有用,更新配置文件只需要去控制台更新,它就会自动推送到每台服务器,利用好监控配置功能做一些db、redis等实例的刷新。

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :