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


相关文章
|
18天前
|
消息中间件 Java Kafka
Springboot集成高低版本kafka
Springboot集成高低版本kafka
|
25天前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
276 0
|
18天前
|
安全 数据安全/隐私保护
Springboot+Spring security +jwt认证+动态授权
Springboot+Spring security +jwt认证+动态授权
|
15小时前
|
SQL Java 数据库连接
Spring脚手架集成分页插件
Spring脚手架集成分页插件
6 0
|
15小时前
|
Java Spring
Spring Boot脚手架集成校验框架
Spring Boot脚手架集成校验框架
6 0
|
1天前
|
Java Docker 容器
SpringBoot项目集成XXL-job
SpringBoot项目集成XXL-job
|
3天前
|
Java 关系型数据库 数据库
【SpringBoot系列】微服务集成Flyway
【4月更文挑战第7天】SpringBoot微服务集成Flyway
【SpringBoot系列】微服务集成Flyway
|
14天前
|
缓存 自动驾驶 测试技术
如何进行有效的Apollo测试:单元测试和集成测试指南
如何进行有效的Apollo测试:单元测试和集成测试指南
43 13
|
14天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
31 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
18天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
15 0