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

作者:phpdragon 时间:2021-08-30 09:03:50 

环境:

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;

}

来源:https://www.cnblogs.com/phpdragon/p/12562429.html

标签:Spring,Frame,work,Apollo,配置
0
投稿

猜你喜欢

  • Android UI设计与开发之实现应用程序只启动一次引导界面

    2022-03-01 19:47:29
  • SpringBoot项目中如何访问HTML页面

    2021-08-09 11:19:07
  • Android简单实现弹幕效果

    2022-08-12 01:24:08
  • Java基于虹软实现人脸识别、人脸比对、活性检测等

    2023-02-18 15:29:09
  • Struts 2中实现Ajax的三种方式

    2022-04-30 05:46:28
  • C# SynchronizationContext以及Send和Post使用解读

    2023-10-16 04:27:28
  • C#递归算法之归并排序

    2023-01-01 14:49:36
  • 在Linux上运行C#的方法

    2023-03-06 00:28:39
  • Netty序列化深入理解与使用

    2023-05-24 20:13:07
  • Android编程实现读取本地SD卡图片的方法

    2023-01-10 01:32:30
  • java WebSocket客户端断线重连的实现方法

    2023-05-26 22:45:18
  • Android编程实现图片背景渐变切换与图层叠加效果

    2021-10-10 06:04:03
  • Mybatis拦截器的实现介绍

    2023-07-04 04:23:31
  • 深入解析C语言中常数的数据类型

    2021-09-03 11:02:18
  • Spring MVC入门_动力节点Java学院整理

    2023-11-03 20:35:31
  • Android控件之Spinner用法实例分析

    2022-08-06 08:36:33
  • Android实现全局悬浮框

    2021-11-22 12:15:40
  • springboot mybatis里localdatetime序列化问题的解决

    2023-06-25 06:58:18
  • Java访问WebService返回XML数据的方法

    2023-11-10 21:23:09
  • 用C语言实现五子棋小游戏

    2023-01-19 12:56:58
  • asp之家 软件编程 m.aspxhome.com