SpringCloud系列:整合Disconf实现分布式配置中心

一、概述

在一个分布式环境中,同类型的服务往往会部署很多实例。这些实例使用一些配置,为了更好地维护这些配置就产生了配置管理服务。通过这个服务可以轻松地管理成千上百个服务实例的配置问题。王阿晶提出了基于zooKeeper的配置信息存储方案的设计与实现, 它将所有配置存储在zookeeper上,这会导致配置的管理不那么方便,而且他们没有相关的源码实现。淘宝的diamond是淘宝内部使用的一个管理持久配置的系统,它具有完整的开源源码实现,它的特点是简单、可靠、易用,淘宝内部绝大多数系统的配置都采用diamond来进行统一管理。他将所有配置文件里的配置打散化进行存储,只支持KV结构,并且配置更新的推送是非实时的。百度内部的BJF配置中心服务采用了类似淘宝diamond的实现,也是配置打散化、只支持KV和非实时推送。Disconf的出现,就是为了解决这些问题。

二、Disconf web的安装

Disconf web是Disconf的管理界面及服务器,通过它来配置管理我们的配置文件,具体的安装步骤在此不再详述,自行百度或者可参考https://my.oschina.net/gengkangkang/blog/1593017。

134359_Yqds_2376417.png

 

三、创建服务

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

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
   </dependency>
   <dependency>
      <groupId>com.baidu.disconf</groupId>
      <artifactId>disconf-client</artifactId>
      <version>2.6.36</version>
   </dependency>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>
</dependencies>

创建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="com.gkk.configdisconf"/>
    </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.properties</value>
              <value>disconf.properties</value>
         </list>
      </property> 
   </bean>

</beans>

新增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=10.17.5.45:8081

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

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

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

在ConfigDisconfApplication上导入依赖文件

@ImportResource({"classpath:conf/spring-disconf.xml"})
@SpringBootApplication
public class ConfigDisconfApplication {

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

 

新建参数类PayPara

package com.gkk.configdisconf.config;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baidu.disconf.client.common.annotations.DisconfFile;
import com.baidu.disconf.client.common.annotations.DisconfFileItem;
import com.baidu.disconf.client.common.annotations.DisconfUpdateService;

/**
 * @Author:gkk
 * @Remark
 * @Date: Created in 2017/12/21 11:29
 */
@Service
@DisconfFile(filename = "pay_para.properties")
@DisconfUpdateService(classes = { PayPara.class })
public class PayPara implements IDisconfUpdate {
    private Logger logger= LoggerFactory.getLogger(getClass());


    private String flag;
    private String url;

    @DisconfFileItem(name = "flag",associateField="flag")
    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    @DisconfFileItem(name = "url",associateField="url")
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "PayPara{" +
                "flag='" + flag + '\'' +
                ", url='" + url + '\'' +
                '}';
    }

    @Override
    public void reload() throws Exception {
        logger.info("配置文件更新了!更新为:[{}]",this.toString());
    }
}

至此,client端已经配置完毕,剩下的去服务端新建配置

新建配置APP

135308_R1H1_2376417.png

新建配置文件pay_para.properties

140042_4YHN_2376417.png

启动应用,可以看到配置生效了

140300_Xp5z_2376417.png

接下来,我们修改配置文件,看看是否实时推送配置更新

144424_wcZX_2376417.png

144510_g5SJ_2376417.png

四、小结

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

   码云地址:https://gitee.com/gengkangkang/springcloud.git

    github地址:https://github.com/gengkangkang/springcloud.git

转载于:https://my.oschina.net/gengkangkang/blog/1593163

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值