资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringFramework应用接入Apollo配置中心的示例分析

这篇文章将为大家详细讲解有关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配置:

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 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文件中声明配置:


  
  
  
  
    
      classpath*:*.properties
      classpath*:properties/*.properties
    
  
  

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;

}

关于“SpringFramework应用接入Apollo配置中心的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


文章标题:SpringFramework应用接入Apollo配置中心的示例分析
分享路径:http://cdkjz.cn/article/jjehsg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220