博客
关于我
Disconf配SpringBoot实战(自动刷新无需重启)
阅读量:554 次
发布时间:2019-03-09

本文共 7566 字,大约阅读时间需要 25 分钟。

前置环境需要的配置:

disconf环境搭建:

SpringBoot与Disconf整合

pom文件

这里我用的是SpringBoot2.0

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.projectlombok
lombok
1.18.0
provided
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.baidu.disconf
disconf-client
2.6.36
org.springframework.boot
spring-boot-maven-plugin

配置

这里需要在resource目录下新建一个名为 disconf.properties 配置文件

# 是否使用远程配置文件#true(默认)会从远程获取配置 false则直接获取本地配置enable.remote.conf=true# disconf HOST地址conf_server_host=192.168.75.128:8085# App nameapp=springboot-demo# versionversion=V1_0_0# envenv=local#获取远程配置 重试次数,默认是3次conf_server_url_retry_times=1#获取远程配置 试时休眠时间,默认是5秒conf_server_url_retry_sleep_seconds=1# 下載檔案的目錄user_define_download_dir=./disconf/download#忽略哪些分布式配置,用逗号分隔#ignore=

这块的host地址请根据你的实际情况进行修改。

application.properties内容如下

test.username=useradmintest.password=passward

test.properties内容如下

aaa=bbbbb

配置类

package com.example.demo.config;import com.baidu.disconf.client.DisconfMgrBean;import com.baidu.disconf.client.DisconfMgrBeanSecond;import com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;import org.springframework.boot.autoconfigure.AutoConfigureOrder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.Ordered;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Properties;@Configurationpublic class DisconfConfiguration {    @Bean(destroyMethod = "destroy")    public DisconfMgrBean getDisconfMgrBean() {        DisconfMgrBean disconfMgrBean = new DisconfMgrBean();        //你的需要被扫描的包        disconfMgrBean.setScanPackage("com.example.demo");        return disconfMgrBean;    }    @Bean(destroyMethod = "destroy", initMethod = "init")    public DisconfMgrBeanSecond getDisconfMgrBean2() {        return new DisconfMgrBeanSecond();    }    @Bean(name = "reloadablePropertiesFactoryBean")    @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)    public ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean() {        ReloadablePropertiesFactoryBean propertiesFactoryBean = new ReloadablePropertiesFactoryBean();        propertiesFactoryBean.setSingleton(true);        // disconf配置的文件        List
fileNames = new ArrayList<>(); fileNames.add("classpath:application.properties"); fileNames.add("classpath:test.properties"); propertiesFactoryBean.setLocations(fileNames); return propertiesFactoryBean; } @Bean(name = "propertyPlaceholderConfigurer") public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(ReloadablePropertiesFactoryBean reloadablePropertiesFactoryBean) { PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer(); placeholderConfigurer.setIgnoreResourceNotFound(true); placeholderConfigurer.setIgnoreUnresolvablePlaceholders(true); try { Properties properties = reloadablePropertiesFactoryBean.getObject(); placeholderConfigurer.setProperties(properties); } catch (IOException e) { throw new RuntimeException("unable to find properties", e); } return placeholderConfigurer; }}

 下面创建一个需要被disconf所管理的配置类

package com.example.demo.config.disconf;import com.baidu.disconf.client.common.annotations.DisconfFile;import com.baidu.disconf.client.common.annotations.DisconfFileItem;import lombok.Data;import org.springframework.stereotype.Component;@Data@Component@DisconfFile(filename = "application.properties")public class TestConfig {    public  String username;    public  String password;    @Override    public String toString() {        return "TestConfig{" +                "username='" + username + '\'' +                ", password='" + password + '\'' +                '}';    }    @DisconfFileItem(name = "test.username")    public  String getUsername() {        return username;    }    @DisconfFileItem(name = "test.password")    public  String getPassword() {        return this.password;    }}

如果想要在配置修改后能做某些特特殊事件(如连接信息修改)的话,需要新加一个回调类

package com.example.demo.callback;import com.baidu.disconf.client.common.annotations.DisconfUpdateService;import com.baidu.disconf.client.common.update.IDisconfUpdate;import com.example.demo.config.disconf.TestConfig;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Component@DisconfUpdateService(classes = {TestConfig.class})public class SimpleTestServiceUpdateCallback implements IDisconfUpdate {    @Autowired    TestConfig testConfig;    @Override    public void reload() throws Exception {        System.out.println(testConfig);    }}

OK~下面添加一个控制器用来观察是否能在配置修改后实时刷新

package com.example.demo.controller;import com.example.demo.config.disconf.TestConfig;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class TestController {    @Autowired    TestConfig testConfig;    @RequestMapping(name="test")    public String test() {        return testConfig.toString();    }}

我的目录结构如下

启动后可以看到获取到的配置信息

并且在disconf-web控制台也能看到实例信息 

调用

到disconf-web上修改实例

可以看到控制台打印了最新修改的信息说明配置修改成功。

再次调用

Ok完美!!!

除了通过继承单独实现IDisconfUpdate接口外它本身还可以在自身上实现,如:

package com.example.demo.config.disconf;import com.baidu.disconf.client.common.annotations.DisconfFile;import com.baidu.disconf.client.common.annotations.DisconfFileItem;import com.baidu.disconf.client.common.annotations.DisconfUpdateService;import com.baidu.disconf.client.common.update.IDisconfUpdate;import lombok.Data;import org.springframework.stereotype.Component;@Data@Component@DisconfFile(filename = "application.properties")@DisconfUpdateService(classes = {TestConfig.class})//更新后回调public class TestConfig implements IDisconfUpdate {    public  String username;    public  String password;    @Override    public String toString() {        return "TestConfig{" +                "username='" + username + '\'' +                ", password='" + password + '\'' +                '}';    }    @DisconfFileItem(name = "test.username")    public  String getUsername() {        return username;    }    @DisconfFileItem(name = "test.password")    public  String getPassword() {        return this.password;    }    @Override    public void reload() throws Exception {        System.out.println("更新后:"+this.toString());    }}

 

参考:

 

转载地址:http://jgmsz.baihongyu.com/

你可能感兴趣的文章
Network 灰鸽宝典【目录】
查看>>
NetworkX系列教程(11)-graph和其他数据格式转换
查看>>
Networkx读取军械调查-ITN综合传输网络?/读取GML文件
查看>>
network小学习
查看>>
Netwox网络工具使用详解
查看>>
Net与Flex入门
查看>>
net包之IPConn
查看>>
Net操作配置文件(Web.config|App.config)通用类
查看>>
Neutron系列 : Neutron OVS OpenFlow 流表 和 L2 Population(7)
查看>>
New Relic——手机应用app开发达人的福利立即就到啦!
查看>>
NFinal学习笔记 02—NFinalBuild
查看>>
NFS
查看>>
NFS Server及Client配置与挂载详解
查看>>
NFS共享文件系统搭建
查看>>
nfs复习
查看>>
NFS安装配置
查看>>
NFS的安装以及windows/linux挂载linux网络文件系统NFS
查看>>
NFS的常用挂载参数
查看>>
NFS网络文件系统
查看>>
NFS远程目录挂载
查看>>