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.編寫配置類接收遠(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)文章
java編譯時(shí)與運(yùn)行時(shí)概念與實(shí)例詳解
本篇文章通過(guò)實(shí)例對(duì) java程序編譯時(shí)與運(yùn)行時(shí)進(jìn)行了詳解,需要的朋友可以參考下2017-04-04
Java Socket上的Read操作阻塞問(wèn)題詳解
這篇文章主要介紹了Java Socket上的Read操作阻塞問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Zuul 實(shí)現(xiàn)網(wǎng)關(guān)轉(zhuǎn)發(fā)的五種方式小結(jié)
這篇文章主要介紹了Zuul 實(shí)現(xiàn)網(wǎng)關(guān)轉(zhuǎn)發(fā)的五種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java實(shí)現(xiàn)微信公眾號(hào)發(fā)送模版消息
大家好,本篇文章主要講的是Java實(shí)現(xiàn)微信公眾號(hào)發(fā)送模版消息,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Java 中的Printstream介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
PrintStream 是打印輸出流,它繼承于FilterOutputStream。接下來(lái)通過(guò)本文給大家介紹Java 中的Printstream,需要的朋友參考下吧2017-05-05

