首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >disconf客户端使用篇

disconf客户端使用篇

作者头像
Monica2333
发布2020-06-19 17:47:02
9900
发布2020-06-19 17:47:02
举报

1. 在Maven POM 文件里加入

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

2.classpath下创建disconf.properties:

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

#
# 配置服务器的 HOST,用逗号分隔  127.0.0.1:8004,127.0.0.1:8004
#nginx对外暴露的ip:port

disconf.conf_server_host=127.0.0.1:8004

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

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

# 环境
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=./disconf/download2

# 下载的文件会被迁移到classpath根路径下,强烈建议将此选项置为 true(默认是true)
disconf.enable_local_download_dir_in_class_path=true

3.创建disconf.xml,并使用spring加载:

<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.globalegrow.esearch.search.disconf"/>

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!-- 使用disconf必须添加以下配置 -->
    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
          destroy-method="destroy">
       <!--disconf配置对象扫描包-->
        <property name="scanPackage" value="com.globalegrow.esearch.search.disconf"/>
    </bean>
    <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
          init-method="init" destroy-method="destroy">
    </bean>

    <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload)-->
  <!--这部分在我看源码的时候并没有发现自动reload的入口。。通过zk watch通知就可以达到自动reload配置文件,注解也保证了properties文件配置对象的更新,然后回调函数可保证xml配置对象的及时更新。。-->
<!--欢迎朋友和我交流指正-->
    <bean id="configproperties_disconf"
          class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/disconf/config/remote.properties</value>
                <value>classpath:/testXml.xml</value>
            </list>
        </property>
    </bean>

    <bean id="propertyConfigurer1"
          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>

</beans>

4.创建配置对象注入类和配置更新时的回调函数:

@Service
@DisconfFile(filename = "remote.properties",targetDirPath = "disconf/config")
public class RemoteServerConfig {

    // 代表连接地址
    private String remoteHost;

    // 代表连接port
    private int remotePort = 8080;

    /**
     * 地址, 分布式文件配置
     *
     */
    @DisconfFileItem(name = "remoteHost")
    public String getRemoteHost() {
        return remoteHost;
    }

    public void setRemoteHost(String remoteHost) {
        this.remoteHost = remoteHost;
    }

    /**
     * 端口, 分布式文件配置
     *对应remote.properties文件中key为remotePort
     */
    @DisconfFileItem(name = "remotePort")
    public int getRemotePort() {
        return remotePort;
    }

    public void setRemotePort(int remotePort) {
        this.remotePort = remotePort;
    }

}

@Service
@DisconfUpdateService(classes = {RemoteServerConfig.class})
public class RemoteServiceUpdateCallback implements IDisconfUpdate {

    protected static final Logger LOGGER = LoggerFactory.getLogger(RemoteServiceUpdateCallback.class);

    @Autowired
    private RemoteService remoteService;

    /**
     *
     */
    public void reload() throws Exception {

       LOGGER.info("reload RemoteServerConfig ok.");
    }

}

//xml文件的托管
/**
 * 空的分布式配置文件,用途有两种:<br/>
 * 1. 对配置文件里的内容不感兴趣,只是单纯的下载<br/>
 * 2. 当配置文件更新时,可以自动下载到本地
 */
@Service
@DisconfFile(filename = "testXml.xml")
public class TestXmlConfig {

}
//xml文件更新时的回调函数
@Service
@DisconfUpdateService(classes = {TestXmlConfig.class})
public class TestXmlConfigCallback implements IDisconfUpdate {
    protected static final Logger LOGGER = LoggerFactory.getLogger(TestXmlConfigCallback.class);

    public void reload() throws Exception {
        //重新读xml更新
        LOGGER.info("===============now i'm at xml update callback================");
    }

}

此时,当更新disconf-web管理台对应的remote.properties文件时,会重新在disconf/config目录下下载最新文件,RemoteServerConfig 中的属性值会对应更新,RemoteServiceUpdateCallback 中的reload函数会被回调。但是对于testXml.xml的更新,disconf不支持配置对象注入,可添加回调函数TestXmlConfigCallback 。

配置类涉及的注解和回调接口有: DisconfFile

/**
 * 分布式的配置文件
 *
 * @author liaoqiqi
 * @version 2014-5-16
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DisconfFile {

    /**
     * 配置文件名,必须指定
     */
    String filename();

    /**
     * 环境,默认为用户指定的环境
     */
    String env() default "";

    /**
     * 版本,默认为用户指定的版本
     */
    String version() default "";

    /**
     * 版本,默认为用户指定的app
     */
    String app() default "";

    /**
     * 配置文件目标地址dir, 以"/"开头则是系统的全路径,否则则是相对于classpath的路径,默认是classpath根路径
     * 注意:根路径要注意是否有权限,否则会出现找不到路径,推荐采用相对路径
     *
     * @return
     */
    String targetDirPath() default "";
}

DisconfFileItem

/**
 * 分布式的配置文件中的ITEM
 *
 * @author liaoqiqi
 * @version 2014-5-16
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DisconfFileItem {

    /**
     * 配置文件里的KEY的名字
     */
    String name();

    /**
     * 所关联的域
     */
    String associateField() default "";
}

DisconfUpdateService

/**
 * 标识配置更新时需要进行更新的服务,需要指定它影响的配置数据
 * 可以是配置文件或者是配置项
 *
 * @author liaoqiqi
 * @version 2014-5-16
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DisconfUpdateService {

    /**
     * 配置文件
     */
    Class<?>[] classes() default {};

    /**
     * 配置文件key名
     */
    String[] confFileKeys() default {};

    /**
     * 配置项
     */
    String[] itemKeys() default {};
}

IDisconfUpdate

/**
 * 当配置更新 时,用户可以实现此接口,用以来实现回调函数
 *
 * @author liaoqiqi
 * @version 2014-5-20
 */
public interface IDisconfUpdate {

    void reload() throws Exception;
}

参考资料: 官方demo

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档