SpringFramework应用接入Apollo配置中心过程解析
环境:
SpringFramework:4.3.5.RELEASE
apollo-client:1.5.1
1.在项目的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类,负责处理合并本地properties配置:
importcom.alibaba.dubbo.common.utils.ConfigUtils;
importcom.ctrip.framework.apollo.Config;
importcom.ctrip.framework.apollo.ConfigService;
importorg.springframework.beans.BeansException;
importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;
importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
importjava.util.Properties;
importjava.util.Set;
/**
*处理apollo配置
*/
publicclassApolloConfigurerextendsPropertyPlaceholderConfigurer{
staticfinalString[]NAMESPACES={"PUBLIC","REDIS","ZOOKEEPER","application"};
@Override
protectedvoidprocessProperties(ConfigurableListableBeanFactorybeanFactoryToProcess,Propertiesprops)throwsBeansException{
try{
this.reloadProperties(props);
}catch(Exceptione){
e.printStackTrace();
logger.info("获取apollo配置失败");
}
//设置到dubbo的上下里
ConfigUtils.addProperties(props);
super.processProperties(beanFactoryToProcess,props);
}
privatevoidreloadProperties(Propertiesprops){
for(Stringnamespace:NAMESPACES){
Configconfig=ConfigService.getConfig(namespace);
SetfieldNames=config.getPropertyNames();
for(StringattributeName:fieldNames){
props.put(attributeName,config.getProperty(attributeName,""));
//设置到系统变量里
System.setProperty(attributeName,config.getProperty(attributeName,""));
}
}
}
@Override
protectedStringresolvePlaceholder(Stringplaceholder,Propertiesprops){
this.reloadProperties(props);
returnprops.getProperty(placeholder);
}
}
3.在Spring的配置xml文件中声明配置:
classpath*:*.properties classpath*:properties/*.properties
4.编写配置类接收远程配置变量:
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;
importorg.springframework.data.redis.core.StringRedisTemplate;
importorg.springframework.data.redis.serializer.StringRedisSerializer;
importredis.clients.jedis.JedisPoolConfig;
importlombok.Data;
/**
*
*/
@Data
@Configuration
//@EnableApolloConfig不能使用该注解,否则会导致无效,该注解由ApolloConfigurer接管类似功能
publicclassRedisConfig{
@Value("${sys.redis.host}")
privateStringhostName;
@Value("${sys.redis.port}")
privateintport;
@Value("${redis.password}")
privateStringpassword;
@Value("${redis.timeout}")
privateinttimeout;
@Value("${redis.pool.maxTotal}")
privateintmaxTotal;
@Value("${redis.pool.minIdle}")
privateintminIdle;
@Value("${redis.pool.maxIdle}")
privateintmaxIdle;
@Value("${redis.pool.maxWaitMillis}")
privatelongmaxWaitMillis;
@Value("${redis.pool.testOnBorrow}")
privatebooleantestOnBorrow;
@Value("${redis.pool.testOnReturn}")
privatebooleantestOnReturn;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。