The previous chapter introduced the installation and pre-configuration of disconf. This chapter mainly introduces the integration of disconf and spring.
1. Add dependencies
<dependency> <groupId>com.baidu.disconf</groupId> <artifactId>disconf-client</artifactId> <version>2.6.36</version> </dependency>
2. Modify the configuration file
Modify the spring configuration file spring-config.xml
Add initial configuration
<!-- The following configuration must be added to use 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> <!-- Autocomplete to create proxy weaving aspect--> <aop:aspectj-autoproxy />
Add a disconf.properties configuration file to the classpath:
# Whether to use a remote configuration file # true (default) will get the configuration from the remote, false will get the local configuration directly disconf.enable.remote.conf=true # # Configure the HOST of the server, separated by commas 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 # version, please use X_X_X_X format disconf.version=1_0_0_0 # APP please use the format of product line_service name disconf.app=pinganwj_appt # environment disco disconf.env=dev # Which distributed configurations to ignore, separated by commas disconf.ignore= # Get the number of remote configuration retries, the default is 3 disconf.conf_server_url_retry_times=1 # Get the sleep time when retrying the remote configuration, the default is 5 seconds disconf.conf_server_url_retry_sleep_seconds=1 # The download folder specified by the user, the remote file will be placed here after downloading disconf.user_define_download_dir=./disconf/download # Downloaded files will be migrated to the classpath root path, it is strongly recommended to set this option to true (default is true) disconf.enable_local_download_dir_in_class_path=true
3. Distributed configuration through xml
Add static configuration file
<!-- Use managed disconf configuration (no code intrusion, configuration changes will not be automatically reloaded) --> <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>
Add dynamic configuration ( hosted ), download the configuration file at startup; when the configuration file changes, it is responsible for dynamic push. The program will not automatically reload the configuration, you need to write the callback function by yourself ( implement the IDisconfUpdate interface and add the DisconfUpdateService annotation ) .
<!-- Use managed disconf configuration (no code intrusion, configuration changes will be automatically reloaded) --> <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 { } }
4. Annotation-based distributed configuration
@Configuration @DisconfFile(filename="redis.properties") public class JedisConfig implements IDisconfUpdate { protected static final Logger LOGGER = LoggerFactory .getLogger(JedisConfig.class); // represents the connection address private String host; // represents the connection port private int port; /** * address, distributed file configuration * * @return */ @DisconfFileItem(name = "redis.host", associateField = "host") public String getHost() { return host; } public void setHost(String host) { this.host = host; } /** * Port, distributed file configuration * * @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); } }