首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot项目代码如何使用Disconf

springboot项目代码如何使用Disconf

作者头像
算法之名
发布2019-08-20 10:48:55
7230
发布2019-08-20 10:48:55
举报
文章被收录于专栏:算法之名算法之名算法之名

因为是springboot的项目,不同于springmvc,不需要写那么多的xml配置文件。

第一步,还是放入pom的依赖

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

第二步,是写一个配置类

package com.xxx.xxx.disconf.config;

import com.baidu.disconf.client.DisconfMgrBean;
import com.baidu.disconf.client.DisconfMgrBeanSecond;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by 关键 on 2018-01-29.
 * Disconf配置类
 */
@Configuration
public class DisConfig {
    @Bean(destroyMethod = "destroy")
    public DisconfMgrBean getDisconfMgrBean() {
        DisconfMgrBean disconfMgrBean = new DisconfMgrBean();
        disconfMgrBean.setScanPackage("com.wmq.consumer");
        return disconfMgrBean;
    }

    @Bean(destroyMethod = "destroy", initMethod = "init")
    public DisconfMgrBeanSecond getDisconfMgrBean2() {
        return new DisconfMgrBeanSecond();
    }
}

第三步,在resources目录下放入disconf.properties

# 是否使用远程配置文件
# true(默认)会从远程获取配置 false则直接获取本地配置
enable.remote.conf=true
#
# 配置服务器的 HOST,用逗号分隔  127.0.0.1:8000,127.0.0.1:8000
#
conf_server_host=http://xxx.xxx.xxx.xxx:xx
# 版本, 请采用 X_X_X_X 格式
version=1_0_0_0
# APP 请采用 产品线_服务名 格式
app=spring-boot-xxx-xxx-xxx
# 环境
env=online
# debug
debug=true
# 忽略哪些分布式配置,用逗号分隔
ignore=
# 获取远程配置 重试次数,默认是3次
conf_server_url_retry_times=1
# 获取远程配置 重试时休眠时间,默认是5秒
conf_server_url_retry_sleep_seconds=1

大概说一下这里面啥意思,conf_server_host,就是你之前配置服务器nginx的地址。app不好解释,上图

总之就是跟你服务端建立的保持一致。

环境就是你建立配置项是在哪里建立的,如图

第四步,选好你在代码中哪些常量,全部在上图中加入配置项,录入进去,这个是可以随时在线修改的,无需到代码中修改,无需改一个常量要重新编译一次代码。

第五步,在代码中使用配置项

@Service
public class PriceService {
    private double money = 1000;

    private static final String KEY = "money";

    /**
     * 单项配置项
     */
    @DisconfItem(key = KEY)
    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}

@DisconfItem(key = KEY)这个是很重要的一个标识,就是使用在disconf中配的money这个常量

这里配成了100,运行你调用这个常量的代码

至此springboot使用disconf就结束了。

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

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

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

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

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