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

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

Spring Boot 与 Disconf 的整合配置说明

前置环境配置

在使用 Disconf 前,需确保以下环境条件已满足:

  • Java 版本:建议使用 Java 8 或更高版本。
  • 操作系统:支持 Unix/Linux 或 Windows 系统。
  • 依赖管理:需确保 Maven 或 Gradle 已正确配置。

Disconf 环境搭建

pom.xml 配置

在项目根目录下,修改 pom.xml 文件,添加必要的依赖:

com.baidu.disconf
disconf-client
2.6.36

配置文件

创建 resource/disconf.properties 文件,配置 Disconf 客户端参数:

# 是否启用远程配置 (默认为 true)enable.remote.conf=true# Disconf 服务器地址conf_server_host=192.168.75.128:8085# 应用名称app=springboot-demo# 配置版本version=V1_0_0# 环境标识envenv=local# 配置下载目录user_define_download_dir=./disconf/download# 忽略的配置项(以逗号分隔)ignore=

Spring Boot 应用配置

application.properties

src/main/resources/application.properties 中添加配置:

test.username=useradmintest.password=passward

test.properties

src/test/resources/test.properties 中添加配置:

aaa=bbbbb

模型类配置

创建 com/example/demo/config/DisconfConfig.java,定义配置模型:

@Data@Component@DisconfFile(filename = "application.properties")public class DisconfConfig {    private String username;    private String password;    @DisconfFileItem(name = "test.username")    public String getUsername() {        return username;    }    @DisconfFileItem(name = "test.password")    public String getPassword() {        return password;    }}

回调处理(可选)

创建 com/example/demo/callback/DisconfUpdateCallback.java,实现配置变更的回调:

package com.example.demo.callback;import com.baidu.disconf.client.common.annotations.DisconfUpdateService;import com.example.demo.config.disconf.DisconfConfig;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Component@DisconfUpdateService(classes = {DisconfConfig.class})public class DisconfUpdateCallback implements IDisconfUpdate {    @Autowired    private DisconfConfig disconfConfig;    @Override    public void reload() throws Exception {        System.out.println("配置更新成功,当前配置为:" + disconfConfig);    }}

控制器开发

创建 com/example/demo/controller/TestController.java,用于展示配置信息:

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

目录结构示例

.├── src/│   ├── main/│   │   ├── java/│   │   │   └── com/│   │   │       └── example/│   │   │           ├── demo/│   │   │           │   ├── config/│   │   │           │   │   └── disconf/│   │   │           │   │       └── TestConfig.java│   │   │           │   └── callback/│   │   │           │       └── DisconfUpdateCallback.java│   │   │           └── controller/│   │   │               └── TestController.java│   │   └── resources/│   │       ├── disconf/│   │       │   └── disconf.properties│   │       └── application.properties│   └── test/│       └── resources/│           └── test.properties

配置验证

启动应用后,访问 /test 端口,应能看到配置信息:

TestConfig{username='useradmin', password='passward'}

Disconf Web 控台

在 Disconf Web 界面中,添加实例并修改配置,修改后可在控制台看到更新日志:

配置更新成功,当前配置为:TestConfig{username='useradmin', password='passward'}

通过以上配置,实现了 Spring Boot 应用的配置管理与 Disconf 的有效结合。

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

你可能感兴趣的文章
NIS服务器的配置过程
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP三大特征抽取器:CNN、RNN与Transformer全面解析
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>
NLP度量指标BELU真的完美么?
查看>>
NLP的不同研究领域和最新发展的概述
查看>>
NLP的神经网络训练的新模式
查看>>
NLP采用Bert进行简单文本情感分类
查看>>
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>
NLP项目:维基百科文章爬虫和分类【02】 - 语料库转换管道
查看>>
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>