disconf分布式配置管理(二) 与spring集成

阅读更多

上一章介绍了disconf的安装预配置,这章主要介绍下disconf与spring集成

1、添加依赖

 


            com.baidu.disconf
            disconf-client
            2.6.36
        

 

 

2、修改配置文件

   修改spring的配置文件spring-config.xml

    添加初始化配置

    

	
	
		
	
	
	

	

   增加一个disconf.properties配置文件到classpath下:

  

# 是否使用远程配置文件
# true(默认)会从远程获取配置 false则直接获取本地配置
disconf.enable.remote.conf=true

#
# 配置服务器的 HOST,用逗号分隔  127.0.0.1:8004,127.0.0.1:8004
#
disconf.conf_server_host=172.20.50.26:8990
#disconf.conf_server_host=127.0.0.1:80

# 版本, 请采用 X_X_X_X 格式 
disconf.version=1_0_0_0

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

# 环境disco
disconf.env=dev

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

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

# 用户指定的下载文件夹, 远程文件下载后会放在这里
disconf.user_define_download_dir=./disconf/download

# 下载的文件会被迁移到classpath根路径下,强烈建议将此选项置为 true(默认是true)
disconf.enable_local_download_dir_in_class_path=true

 

 

3、通过xml的分布式配置  

   添加静态配置文件

   

	
		
			
				file:config/global.properties
				file:config/jdbc.properties
				file:config/config-db.properties
				file:config/clinic-api.properties
			
		
	

		
		
		
			
				
			
		
	

 

 

    添加动态配置(托管式),启动时下载配置文件;配置文件变化时,负责动态推送。程序不会自动reload配置,需要自己写回调函数(实现IDisconfUpdate接口,并添加DisconfUpdateService注解)

   

	
		
			
				file:config/kafka.properties
				file:config/emailSendConfig.properties
			
		
	


		
		
		
			
				
			
		
	

   

 

   

@Component
@DisconfUpdateService(confFileKeys = { "kafaka.properties" })
public class KafakaConfigCallback implements IDisconfUpdate {

    @Override
    public void reload() throws Exception {
    }

}

 

 

 4、基于注解的分布式配置

 

 

@Configuration    
@DisconfFile(filename="redis.properties")  
public class JedisConfig implements IDisconfUpdate {    
    
    protected static final Logger LOGGER = LoggerFactory    
            .getLogger(JedisConfig.class);    
    
    // 代表连接地址    
    private String host;    
    
    // 代表连接port    
    private int port;    
    
    /**  
     * 地址, 分布式文件配置  
     *   
     * @return  
     */    
    @DisconfFileItem(name = "redis.host", associateField = "host")    
    public String getHost() {    
    
        return host;    
    }    
    
    public void setHost(String host) {    
    
        this.host = host;    
    }    
    
    /**  
     * 端口, 分布式文件配置  
     *   
     * @return  
     */    
    @DisconfFileItem(name = "redis.port", associateField = "port")    
    public int getPort() {    
    
        return port;    
    }    
    
    public void setPort(int port) {    
    
        this.port = port;    
    }    
    
    public void reload() throws Exception {    
    
        LOGGER.info("host: " + host);    
    }    
}    

 

 

   

你可能感兴趣的:(disconf,spring,注解)