Apollo 自動刷新

一、引入依賴java

 

二、配置文件web

(1)apollo-env.propertiesredis

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

(2) application.propertiesspring

apollo.bootstrap.enabled=true

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

三、自動刷新日誌級別bootstrap

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

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)刷新代碼以下: ide

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動態修改日誌級別】自動刷新 結束");

    }

}

四、自動刷新 ConfigurationProperties 標註的類屬性this

(1)須要自動刷新的類spa

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.coupon.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;
    }
}
相關文章
相關標籤/搜索