由于是springboot的项目,不一样于springmvc,不须要写那么多的xml配置文件。nginx
第一步,仍是放入pom的依赖spring
<dependency> <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> <version>2.6.36</version> </dependency>
第二步,是写一个配置类springboot
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很差解释,上图mvc
总之就是跟你服务端创建的保持一致。app
环境就是你创建配置项是在哪里创建的,如图分布式
第四步,选好你在代码中哪些常量,所有在上图中加入配置项,录入进去,这个是能够随时在线修改的,无需到代码中修改,无需改一个常量要从新编译一次代码。spring-boot
第五步,在代码中使用配置项this
@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这个常量url
这里配成了100,运行你调用这个常量的代码
至此springboot使用disconf就结束了。