springboot集成携程apollo配置中心

好久没写博客啦,今天分享一波springboot 集成携程的apollo配置中心,2019,希望大家步步高升,工资翻倍。

好了进入正题?

因为集成apollo配置中心的demo比较多,本文重点介绍,如果监听apollo配置变化,实时变更配置。

1.首先pom.xml新增依赖:

 <dependency>

            <groupId>com.ctrip.framework.apollo</groupId>

            <artifactId>apollo-client</artifactId>

            <version>0.9.0<</version>

     </dependency>

2.编写apollo配置ApolloConfiguration(项目里面使用了lombok,如果你的项目没有用的@Slf4j 可能会报错哦 ,没有接触到的小伙伴可以去看看,这里我就不着重介绍,因为本文重点是apollo配置中心使用)

 

import org.springframework.beans.factory.InitializingBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

import com.ctrip.framework.apollo.Config;

import com.ctrip.framework.apollo.ConfigChangeListener;

import com.ctrip.framework.apollo.ConfigService;

import com.ctrip.framework.apollo.model.ConfigChange;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;

 

import lombok.extern.slf4j.Slf4j;

 

@Configuration

@EnableApolloConfig({"CSS_SERVICE_PLATFORM"})

//特别注意这里的CSS_SERVICE_PLATFORM是apollo私有的命名空间,所谓私有的apollo私有命名空间创建在下面介绍

@Slf4j

public class ApolloConfiguration implements ConfigChangeListener, InitializingBean {

 

    /**

     * 启用 apollo 配置中心②

     */

    @Bean

    public Config apolloConfig() {

        Config config = ConfigService.getConfig("CSS_SERVICE_PLATFORM");

        config.addChangeListener(this);//设置监听

        return config;

    }

 

    /**

     * 监听配置变化

     */

    @Override

    public void onChange(ConfigChangeEvent changeEvent) {

        log.info("[changeHandler]Changes for namespace {}", changeEvent.getNamespace());

        for (String key : changeEvent.changedKeys()) {

            ConfigChange change = changeEvent.getChange(key);

            log.info("[changeHandler]Change - key: {}, oldValue: {}, newValue: {}, changeType: {}", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType());

        }

    }

 

 

    @Override

    public void afterPropertiesSet() throws Exception {

 

    }

}

 

3.在src/main/resouces下创建一个META-INF文件夹,并创建一个app.properties的配置文件。(作用能就是告诉这个应用,应该去apollo找appid=CSS_SERVICE_PLATFORM的应用的配置。和上面的私有的命名空间是两个概念,一定要分清,这个是appid(是apollo中应用的名字),而私有命名空间相当于一个名字叫做CSS_SERVICE_PLATFORM的properties

 

4.我们打开apollo的控制台开始新建一个名字叫做CSS_SERVICE_PLATFORM的应用。

 

5.第4步完成以后,我们就会进入下一个界面,接着创建我们刚才说的私有的命名空间CSS_SERVICE_PLATFORM。

 

6.提醒第5部,我们在加完配置以后,一定要点发布,不然是不会生效的。还有所有的配置写在私有的命名空间(css_service_platform)。在第5部的图里面,我们可以看到很多环境的配置,那java到底是下载哪个环境的配置呢,需要我们指定。

A.通过配置文件指定,mac电脑的我就不说了,windows的电脑在c盘。创建一个C:/opt/settings/server.properties的文件,文件内容为:env=DEV/QA/PRD/等等。

B.通过jvm参数来启动我们eclipse中点击应用右键-》run as-》run configurations。配置好了点击run运行。

 

7.以上完整的apollo配置已经完成。

现在我们要实现应用在运行中,动态监听apollo配置并实时更新配置。因为没有特别大的难点,我就直接贴代码了。

 

import com.ctrip.framework.apollo.ConfigChangeListener;

import com.ctrip.framework.apollo.model.ConfigChange;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

@ConfigurationProperties(prefix = SeedProperties.SEED_PREFIX)

@Data

public class SeedProperties implements ConfigChangeListener {

    public static final String SEED_PREFIX = "seed.api.config";

    private String baseUrl;

    private String seedSendUrl;

     private String clientId;

    private String securitykey;

 

    @Override

    public void onChange(ConfigChangeEvent configChangeEvent) {

        for(String key: configChangeEvent.changedKeys())

        {

            ConfigChange change = configChangeEvent.getChange(key);

            if(configChangeEvent.isChanged(SEED_PREFIX + "baseUrl"))

            {

                baseUrl = change.getNewValue();

            }

            if(configChangeEvent.isChanged(SEED_PREFIX + "seedSendUrl"))

            {

                seedSendUrl = change.getNewValue();

            }

            if(configChangeEvent.isChanged(SEED_PREFIX + "clientId"))

            {

                clientId = change.getNewValue();

            }

            if(configChangeEvent.isChanged(SEED_PREFIX + "securitykey"))

            {

                securitykey = change.getNewValue();

            }

        }

    }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值