disconf分佈式配置管理(二) 與spring集成

上一章介紹了disconf的安裝預配置,這章主要介紹下disconf與spring集成java

一、添加依賴redis

 

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

 

 

二、修改配置文件spring

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

    添加初始化配置服務器

    

<!-- 使用disconf必須添加如下配置 -->
	<bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
		destroy-method="destroy">
		<property name="scanPackage" value="com.abcde" />
	</bean>
	<bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
		init-method="init" destroy-method="destroy">
	</bean>
<!-- 自動完成建立代理織入切面 -->
	<aop:aspectj-autoproxy />

   增長一個disconf.properties配置文件到classpath下:app

  

# 是否使用遠程配置文件
# 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

 

 

三、經過xml的分佈式配置  分佈式

   添加靜態配置文件ide

   

<!-- 使用託管方式的disconf配置(無代碼侵入, 配置更改不會自動reload) -->
	<bean id="configproperties_no_reloadable_disconf"
		class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>file:config/global.properties</value>
				<value>file:config/jdbc.properties</value>
				<value>file:config/config-db.properties</value>
				<value>file:config/clinic-api.properties</value>
			</list>
		</property>
	</bean>
<bean id="propertyConfigurerForProject1" class="com.abcde.core.base.utils.PropertyPlaceholder">
		<property name="ignoreResourceNotFound" value="true" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="propertiesArray">
			<list>
				<ref bean="configproperties_no_reloadable_disconf" />
			</list>
		</property>
	</bean>

 

 

    添加動態配置(託管式),啓動時下載配置文件;配置文件變化時,負責動態推送。程序不會自動reload配置,須要本身寫回調函數(實現IDisconfUpdate接口,並添加DisconfUpdateService註解)函數

   

<!-- 使用託管方式的disconf配置(無代碼侵入, 配置更改會自動reload) -->
	<bean id="configproperties_reloadable_disconf"
		class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>file:config/kafka.properties</value>
				<value>file:config/emailSendConfig.properties</value>
			</list>
		</property>
	</bean>

<bean id="propertyConfigurerForProject"
		class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">
		<property name="ignoreResourceNotFound" value="true" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="propertiesArray">
			<list>
				<ref bean="configproperties_reloadable_disconf" />
			</list>
		</property>
	</bean>

   

 

   

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

    @Override
    public void reload() throws Exception {
    }

}

 

 

 四、基於註解的分佈式配置this

 

 

@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);    
    }    
}
相關文章
相關標籤/搜索