springboot apollo 自动刷新

目录

1、引入依赖

2、配置文件

3、设置配置文件

4、需要自动刷新的类

5、实现自动刷新


1、引入依赖

        
            com.ctrip.framework.apollo
            apollo-core
        
        
            com.ctrip.framework.apollo
            apollo-client
        
        
            org.springframework.cloud
            spring-cloud-context
        

2、配置文件

env:
  dev
app:
  id: common-dictionaries
apollo:
  meta: http://127.0.0.1:26008
  autoUpdateInjectedSpringProperties: true
  bootstrap:
    enabled: true
    namespaces: application,jdbc,redis

3、设置配置文件

redis.single.host = 127.0.0.1
redis.single.database = 101
redis.single.testOnBorrow = true
redis.single.testOnReturn = true

4、需要自动刷新的类

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.cloud.context.config.annotation.RefreshScope;

/**
 * 单机模式redis配置信息
 */
@Data
@RefreshScope
@Configuration
public class RedisSingleProperties {


    /**
     * 连接地址
     */
    @Value("${redis.single.host:#{null}}")
    private String host;

     /**
     * 端口号,默认6379
     */
     @Value("${redis.single.port:6379}")
    private int port;

    /**
     * 连接超时时间,单位毫秒,默认3秒
     */
    @Value("${redis.single.timeout:3000}")
    private int timeout;

    /**
     * 连接密码
     */
    @Value("${redis.single.password:#{null}}")
    private String password;

    /**
     * 连接的DB
     */
    @Value("${redis.single.database:0}")
    private int database;

    /**
     * 资源次最大连接数,默认8
     */
    @Value("${redis.single.maxTotal:8}")
    private int maxTotal = 8;

    /**
     * 资源池允许最大空闲连接数	,默认8
     */
    @Value("${redis.single.maxIdle:8}")
    private int maxIdle = 8;

    /**
     * 资源池确保最少空闲连接数	,默认0
     */
    @Value("${redis.single.minIdle:0}")
    private int minIdle = 0;

    /**
     * 是否开启jmx监控,可用于监控,默认为true,开启状态
     */
    @Value("${redis.single.jmxEnabled:true}")
    private boolean jmxEnabled;

    /**
     * 当资源池用尽后,调用者是否要等待。只有当为true时,下面的maxWaitMillis才会生效
     * 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
     */
    @Value("${redis.single.blockWhenExhausted:true}")
    private boolean blockWhenExhausted = true;

    /**
     * 当资源池连接用尽后,调用者的最大等待时间(单位毫秒)	-1:表示永不超时
     * 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
     */
    @Value("${redis.single.maxWaitMillis:-1}")
    private long maxWaitMillis;

    /**
     * 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
     */
    @Value("${redis.single.minEvictableIdleTimeMillis:1800000}")
    private long minEvictableIdleTimeMillis;

    /**
     * 向资源池借用连接时是否做连接有效性检查(ping),无效连接会被移除,默认false
     */
    @Value("${redis.single.testOnBorrow:false}")
    private boolean testOnBorrow = false;

    /**
     * 向资源池归还连接时是否做连接有效性测试(ping),无效连接会被移除	默认false
     */
    @Value("${redis.single.testOnReturn:false}")
    private boolean testOnReturn = false;
}

5、实现自动刷新



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.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;

import javax.annotation.Resource;

/**
 * 自动刷新apollo配置
 */
@Slf4j
@Component
public class ApolloConfigChanged implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Resource
    private RefreshScope refreshScope;

    @ApolloConfigChangeListener(value = {"application","jdbc","redis"})
    public void onChange(ConfigChangeEvent changeEvent){
        log.info("================Apollo 自动刷新值 开始 ===========================");

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

        refreshProperties(changeEvent);

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

    private void refreshProperties(ConfigChangeEvent changeEvent) {
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
        refreshScope.refreshAll();
    }

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

6、测试用例


import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 测试apollo自动刷新
 *
 */
@RefreshScope
@RestController
public class TestController {

    @Value("${application.name:applicationName}")
    private String applicationName;

    @Resource
    private RedisSingleProperties redisSingleProperties;


    @GetMapping("application")
    public String application(){
        return applicationName;
    }


    @GetMapping("redis")
    public String redis(){
        return redisSingleProperties.getHost();
    }


}

 

 

你可能感兴趣的:(java,apollo,自动刷新)