Spring Boot 集成 disconf-client

Disconf web的安装配置

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

客户端配置

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

<dependency>
  <groupId>com.baidu.disconf</groupId>
  <artifactId>disconf-client</artifactId>
  <version>2.6.36</version>
</dependency>

(2)本地创建conf/spring-disconf.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: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

# 是否使用远程配置文件
# 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

server:
  port: 8081

spring:
  application:
    name: service-provider

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

@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

host=192.168.1.1
port=8818

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

--------------- 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 配置

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

============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等实例的刷新。

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/23/spring-boot-integrate-disconf-client/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Spring Boot 集成 disconf-client
Disconf web的安装配置 Disconf web是Disconf的管理界面及服务器,通过它来配置管理我们的配置文件。 客户端配置 (1)创建普通的SpringBoot项目config-disco……
<<上一篇
下一篇>>
文章目录
关闭
目 录