SpringFramework應(yīng)用接入Apollo配置中心的示例分析

這篇文章將為大家詳細(xì)講解有關(guān)SpringFramework應(yīng)用接入Apollo配置中心的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺(tái)小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了撫遠(yuǎn)免費(fèi)建站歡迎大家使用!

環(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)致無效,該注解由 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;

}

關(guān)于“SpringFramework應(yīng)用接入Apollo配置中心的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

本文標(biāo)題:SpringFramework應(yīng)用接入Apollo配置中心的示例分析
URL鏈接:http://bm7419.com/article36/jjehsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、靜態(tài)網(wǎng)站關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)