disconf-注解式分布式配置

x33g5p2x  于2021-12-20 转载在 其他  
字(2.1k)|赞(0)|评价(0)|浏览(276)
  1. disconf-web配置

  2. 在disconf-web上点击新建app创建一个新的app,这里假设app名字为:app_test

  3. 在disconf-web上点击新建配置文件新建一个配置文件,APP选择刚刚的app_test,版本自定义成:0.0.1,环境可选择local环境,直接选择上传文件,比如我们这里上传dev.properties,内容如下:

disconf.test=disconf_test
  1. 项目使用

  2. pom.xml中加入disconf,如下:

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>

由于自己项目需要下载disconf托管的配置文件,所以项目还需要引入httpclient

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
  1. 准备disconf.properties
# 是否使用远程配置文件
# true(默认)会从远程获取配置false则直接获取本地配置
enable.remote.conf=true

#
# 配置服务器的HOST,用逗号分隔127.0.0.1:8000,127.0.0.1:8000
#
conf_server_host=127.0.0.1:8991

# 版本, 请采用X_X_X_X 格式
version=0.0.1

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

# 环境
env=local

# debug
debug=true

# 忽略哪些分布式配置,用逗号分隔
ignore=

# 获取远程配置 重试次数,默认是3次
conf_server_url_retry_times=1
# 获取远程配置 重试时休眠时间,默认是5秒
conf_server_url_retry_sleep_seconds=1

该配置文件需要放在resources根目录下(这点感觉是很奇怪的规定,其实应该可以指定放入到resource目录下的任何位置)

  1. 修改applicationContext.xml
    在applicationContext.xml中加入如下配置:

  2. 包扫描

<context:component-scan base-package="xxx.xxx.xxx" />
<aop:aspectj-autoproxy proxy-target-class="true" />
  1. bean配置
<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
destroy-method="destroy">
<property name="scanPackage" value="xxx.trans.test"/>
</bean>
<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
init-method="init" destroy-method="destroy">
</bean>
  1. 使用示例
    java代码如下:
@Service
@DisconfFile(filename = "dev.properties")
public class QueryService {

private String disconfTest;

@DisconfFileItem(name = "disconf.test", associateField = "disconfTest") 
public String getDisconfTest() { 
    return disconfTest; 
}

public void setDisconfTest(String disconfTest) { 
    this.disconfTest = disconfTest; 
}

这样disconfTest会随着disconf-web中对dev.properties值的修改而修改

  1. 总结

该方法对代码的侵入性太强,不太适合对老项目的变更

参考:https://github.com/knightliao/disconf/wiki

相关文章