SpringFramework应用接入Apollo配置中心过程解析

所属分类: 软件编程 / java 阅读数: 31
收藏 0 赞 0 分享

环境:

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配置:

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配置失败");
    }

    //设置到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, ""));
        //设置到系统变量里
        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.编写配置类接收远程配置变量:

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不能使用该注解,否则会导致无效,该注解由 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;

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多