SpringFramework應(yīng)用接入Apollo配置中心過(guò)程解析
環(huán)境:
SpringFramework:4.3.5.RELEASE
apollo-client:1.5.1
1.在項(xiàng)目的 resources/META-INF/ 目錄下添加 app.properties 文件:
#Apollo配置id
app.id = phpdragon-demo
apollo.bootstrap.enabled = true
apollo.eagerLoad.enabled = true
apollo.cacheDir = /data/app_data/apollo_cache/
2. 新建 ApolloConfigurer 類,負(fù)責(zé)處理合并本地properties配置:
import com.alibaba.dubbo.common.utils.ConfigUtils; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigService; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import java.util.Properties; import java.util.Set; /** * 處理apollo配置 */ public class ApolloConfigurer extends PropertyPlaceholderConfigurer { static final String[] NAMESPACES = {"PUBLIC", "REDIS", "ZOOKEEPER", "application"}; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { try { this.reloadProperties(props); } catch (Exception e) { e.printStackTrace(); logger.info("獲取apollo配置失敗"); } //設(shè)置到dubbo的上下里 ConfigUtils.addProperties(props); super.processProperties(beanFactoryToProcess, props); } private void reloadProperties(Properties props) { for (String namespace : NAMESPACES) { Config config = ConfigService.getConfig(namespace); Set<String> fieldNames = config.getPropertyNames(); for (String attributeName : fieldNames) { props.put(attributeName, config.getProperty(attributeName, "")); //設(shè)置到系統(tǒng)變量里 System.setProperty(attributeName, config.getProperty(attributeName, "")); } } } @Override protected String resolvePlaceholder(String placeholder, Properties props) { this.reloadProperties(props); return props.getProperty(placeholder); } }
3.在 Spring 的配置xml文件中聲明配置:
<bean class="com.phpdragon.config.ApolloConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <property name="ignoreResourceNotFound" value="true"/> <property name="trimValues" value="true"/> <property name="locations"> <list> <value>classpath*:*.properties</value> <value>classpath*:properties/*.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8"/> </bean>
4.編寫(xiě)配置類接收遠(yuǎn)程配置變量:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; import lombok.Data; /** * */ @Data @Configuration //@EnableApolloConfig不能使用該注解,否則會(huì)導(dǎo)致無(wú)效,該注解由 ApolloConfigurer 接管類似功能 public class RedisConfig { @Value("${sys.redis.host}") private String hostName; @Value("${sys.redis.port}") private int port; @Value("${redis.password}") private String password; @Value("${redis.timeout}") private int timeout; @Value("${redis.pool.maxTotal}") private int maxTotal; @Value("${redis.pool.minIdle}") private int minIdle; @Value("${redis.pool.maxIdle}") private int maxIdle; @Value("${redis.pool.maxWaitMillis}") private long maxWaitMillis; @Value("${redis.pool.testOnBorrow}") private boolean testOnBorrow; @Value("${redis.pool.testOnReturn}") private boolean testOnReturn; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證
這篇文章主要介紹了如何使用IntelliJ IDEA的HTTP Client進(jìn)行接口驗(yàn)證,本文給大家分享最新完美解決方案,感興趣的朋友跟隨小編一起看看吧2024-06-06Spring?Security?基于URL的權(quán)限判斷源碼解析
這篇文章主要介紹了Spring?Security?基于URL的權(quán)限判斷問(wèn)題,我們想要實(shí)現(xiàn)自己的基于請(qǐng)求Url的授權(quán)只需自定義一個(gè)?AccessDecisionManager即可,接下來(lái)跟隨小編一起看看實(shí)現(xiàn)代碼吧2021-12-12springboot使用ThreadPoolTaskExecutor多線程批量插入百萬(wàn)級(jí)數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot利用ThreadPoolTaskExecutor多線程批量插入百萬(wàn)級(jí)數(shù)據(jù),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02java文件重命名(文件批量重命名)實(shí)例程序代碼分享
這篇文章主要介紹了java文件重命名的程序代碼,大家參考使用吧2013-12-12