spring boot 集成 ctrip apollo 实现动态配置更新

简介: spring boot 集成 ctrip apollo 实现动态配置更新

1.apollo 安装

apolloconfig/apollo-build-scripts: Apollo Quick Start Build Scripts (github.com)

window 安装借助:git 进行

2.spring boot 接入

<dependency>
            <groupId>com.ctrip.framework.apollo</groupId>
            <artifactId>apollo-client</artifactId>
            <version>1.1.0</version>
</dependency>

application.properties

#appId 同来区别不同的配置
app.id=SampleApp
#apollo服务器地址
apollo.meta=http://localhost:8080

代码

package com.example.demo.config;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableApolloConfig
public class AppConfig {
    @Bean
    public TestJavaConfigBean javaConfigBean() {
        return new TestJavaConfigBean();
    }
}
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
public class TestJavaConfigBean {
    @Value("${timeout:100}")
    private int timeout;
    private int batch;
    @Value("${batch:200}")
    public void setBatch(int batch) {
        this.batch = batch;
    }
    public int getTimeout() {
        return timeout;
    }
    public int getBatch() {
        return batch;
    }
}

使用

package com.example.demo.schedule;
import com.example.demo.config.TestJavaConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class ScheduledTest {
    @Autowired
    private TestJavaConfigBean testJavaConfigBean;
    @Scheduled(cron = "0/5 * * * * ?")
    public void executeFileDownLoadTask() {
        // 间隔2分钟,执行任务
        int timeout = testJavaConfigBean.getTimeout();
        System.out.println("定时任务1:" + timeout);
    }
}

结果:

自动监听:

@Configuration
public class LoggerConfig {
    private static final Logger logger = LoggerFactory.getLogger(LoggerConfig.class);
    private static final String LOGGER_TAG = "logging.level.";
    @Autowired
    private LoggingSystem loggingSystem;
    @ApolloConfig
    private Config config;
    @ApolloConfigChangeListener
    private void configChangeListter(ConfigChangeEvent changeEvent) {
        refreshLoggingLevels();
    }
    @PostConstruct
    private void refreshLoggingLevels() {
        Set<String> keyNames = config.getPropertyNames();
        for (String key : keyNames) {
            if (StringUtils.containsIgnoreCase(key, LOGGER_TAG)) {
                String strLevel = config.getProperty(key, "info");
                LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());
                loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level);
                logger.info("{}:{}", key, strLevel);
            }
        }
    }
}

SpringBoot 整合 apollo


相关文章
|
8天前
|
消息中间件 Java Kafka
Springboot集成高低版本kafka
Springboot集成高低版本kafka
|
15天前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
47 0
|
15天前
|
Java 调度 Spring
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
38 0
|
20天前
|
NoSQL Java Redis
SpringBoot集成Redis
SpringBoot集成Redis
153 0
|
27天前
|
NoSQL Java Redis
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
小白版的springboot中集成mqtt服务(超级无敌详细),实现不了掐我头!!!
218 1
|
9天前
|
安全 数据安全/隐私保护
Springboot+Spring security +jwt认证+动态授权
Springboot+Spring security +jwt认证+动态授权
|
4天前
|
缓存 自动驾驶 测试技术
如何进行有效的Apollo测试:单元测试和集成测试指南
如何进行有效的Apollo测试:单元测试和集成测试指南
34 13
|
4天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
16 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
8天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
12 0
|
15天前
|
Java 应用服务中间件
Springboot启动的时候初始化的线程池默认配置tomcat
Springboot启动的时候初始化的线程池默认配置tomcat
12 1