Apollo 自动刷新

1、引入依赖

 

2、配置文件

(1)apollo-env.properties

#环境
env=dev
# app id
app.id=coupon-center
# meta服务地址
dev.meta=http://127.0.0.1:26008

(2) application.properties

apollo.bootstrap.enabled=true

#引入多个namespaces
apollo.bootstrap.namespaces=\
devGroup.coupon.application,\
devGroup.coupon.db,\
devGroup.coupon.redis,\
devGroup.coupon.rabbit-mq,\
devGroup.coupon.logger

3、自动刷新日志级别

(1)日志配置文件(devGroup.coupon.logger):

logging.level.org.springframework.web.servlet = info
logging.level.springfox.documentation.spring.web = info
logging.level.org.springframework.web.servlet.PageNotFound = error
logging.level.org.springframework.jdbc = info

(2)刷新代码如下: 

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.annotation.Configuration;

/**
 * 自动刷新日志
 */
@Slf4j
@Configuration
public class ApolloLoggerConfig {
    private static final String LOGGER_TAG = "logging.level.";

    @Autowired
    private LoggingSystem loggingSystem;

    @ApolloConfigChangeListener("devGroup.coupon.logger")
    private void configChangeListter(ConfigChangeEvent changeEvent) {

        log.info("【Apollo动态修改日志级别】 自动刷新 开始");

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

            if (changedKey.startsWith(LOGGER_TAG)) {

                ConfigChange configChange = changeEvent.getChange(changedKey);
                String oldValue = configChange.getOldValue();
                String newValue = configChange.getNewValue();

                LogLevel level = LogLevel.valueOf(newValue.toUpperCase());
                loggingSystem.setLogLevel(changedKey.replace(LOGGER_TAG, ""), level);

                log.info("【Apollo动态修改日志级别】changedKey:【{}】, oldValue=【{}】, newValue:【{}】",changedKey, oldValue, newValue);
            }

        }

        log.info("【Apollo动态修改日志级别】自动刷新 结束");

    }

}

4、自动刷新 ConfigurationProperties 标注的类属性

(1)需要自动刷新的类

package com.moon.coupon.service.controller.test;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "spring.jdbc.master")
//@PropertySource("classpath:/jdbc.properties")
public class JdbcConfig {

    private String driver;

    private String name;

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

(2)实现自动刷新

import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ApolloConfigChanged implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Autowired
    private RefreshScope refreshScope;

    /**
     * 刷新的namespace的名字:devGroup.coupon.application
     * apollo自定义的几个namespace:{@link ConfigConsts}
     * @param changeEvent
     */
    @ApolloConfigChangeListener("devGroup.iqianjin-coupon-center.application")
    private void someChangeHandler(ConfigChangeEvent changeEvent) {
        log.info("================Apollo 自动刷新值 开始 ===========================");

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

            ConfigChange configChange = changeEvent.getChange(changedKey);
            String oldValue = configChange.getOldValue();
            String newValue = configChange.getNewValue();
            log.info("changedKey:【{}】,oldValue=【{}】, newValue:【{}】", changedKey, oldValue, newValue);
        }

        refreshProperties(changeEvent);

        log.info("================Apollo 自动刷新值 结束 ===========================");
    }

    public void refreshProperties(ConfigChangeEvent changeEvent) {
        // 更新相应的bean的属性值,主要是存在@ConfigurationProperties注解的bean
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
        //refreshScope.refreshAll();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}


 

转载于:https://my.oschina.net/u/3777515/blog/3053115

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值