java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot动态修改配置

SpringBoot动态修改配置的十种方法

作者:风象南

在SpringBoot应用中,配置信息通常通过application.properties或application.yml文件静态定义,应用启动后这些配置就固定下来了,但我们常常需要在不重启应用的情况下动态修改配置,本文将介绍SpringBoot中10种实现配置动态修改的方法,需要的朋友可以参考下

引言

在SpringBoot应用中,配置信息通常通过application.propertiesapplication.yml文件静态定义,应用启动后这些配置就固定下来了。

但我们常常需要在不重启应用的情况下动态修改配置,以实现灰度发布、A/B测试、动态调整线程池参数、切换功能开关等场景。

本文将介绍SpringBoot中10种实现配置动态修改的方法。

1. @RefreshScope结合Actuator刷新端点

Spring Cloud提供的@RefreshScope注解是实现配置热刷新的基础方法。

实现步骤

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter</artifactId>
</dependency>
management.endpoints.web.exposure.include=refresh
@RefreshScope
@RestController
public class ConfigController {
    
    @Value("${app.message:Default message}")
    private String message;
    
    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
}
curl -X POST http://localhost:8080/actuator/refresh

优缺点

优点

缺点

2. Spring Cloud Config配置中心

Spring Cloud Config提供了一个中心化的配置服务器,支持配置文件的版本控制和动态刷新。

实现步骤

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
spring.cloud.config.server.git.uri=https://github.com/your-repo/config
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
# bootstrap.properties
spring.application.name=my-service
spring.cloud.config.uri=http://localhost:8888
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

优缺点

优点

缺点

3. 基于数据库的配置存储

将配置信息存储在数据库中,通过定时任务或事件触发机制实现配置刷新。

实现方案

CREATE TABLE app_config (
    config_key VARCHAR(100) PRIMARY KEY,
    config_value VARCHAR(500) NOT NULL,
    description VARCHAR(200),
    update_time TIMESTAMP
);
@Service
public class DatabaseConfigService {
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    private Map<String, String> configCache = new ConcurrentHashMap<>();
    
    @PostConstruct
    public void init() {
        loadAllConfig();
    }
    
    @Scheduled(fixedDelay = 60000)  // 每分钟刷新
    public void loadAllConfig() {
        List<Map<String, Object>> rows = jdbcTemplate.queryForList("SELECT config_key, config_value FROM app_config");
        for (Map<String, Object> row : rows) {
            configCache.put((String) row.get("config_key"), (String) row.get("config_value"));
        }
    }
    
    public String getConfig(String key, String defaultValue) {
        return configCache.getOrDefault(key, defaultValue);
    }
}

优缺点

优点

缺点

4. 使用ZooKeeper管理配置

利用ZooKeeper的数据变更通知机制,实现配置的实时动态更新。

实现步骤

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>5.1.0</version>
</dependency>
@Component
public class ZookeeperConfigManager {
    
    private final CuratorFramework client;
    private final Map<String, String> configCache = new ConcurrentHashMap<>();
    
    @Autowired
    public ZookeeperConfigManager(CuratorFramework client) {
        this.client = client;
        initConfig();
    }
    
    private void initConfig() {
        try {
            String configPath = "/config";
            if (client.checkExists().forPath(configPath) == null) {
                client.create().creatingParentsIfNeeded().forPath(configPath);
            }
            
            List<String> keys = client.getChildren().forPath(configPath);
            for (String key : keys) {
                String fullPath = configPath + "/" + key;
                byte[] data = client.getData().forPath(fullPath);
                configCache.put(key, new String(data));
                
                // 添加监听器
                NodeCache nodeCache = new NodeCache(client, fullPath);
                nodeCache.getListenable().addListener(() -> {
                    byte[] newData = nodeCache.getCurrentData().getData();
                    configCache.put(key, new String(newData));
                    System.out.println("Config updated: " + key + " = " + new String(newData));
                });
                nodeCache.start();
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize config from ZooKeeper", e);
        }
    }
    
    public String getConfig(String key, String defaultValue) {
        return configCache.getOrDefault(key, defaultValue);
    }
}

优缺点

优点

缺点

5. Redis发布订阅机制实现配置更新

利用Redis的发布订阅功能,实现配置变更的实时通知。

实现方案

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
@Component
public class RedisConfigManager {
    
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    private final Map<String, String> configCache = new ConcurrentHashMap<>();
    
    @PostConstruct
    public void init() {
        loadAllConfig();
        subscribeConfigChanges();
    }
    
    private void loadAllConfig() {
        Set<String> keys = redisTemplate.keys("config:*");
        if (keys != null) {
            for (String key : keys) {
                String value = redisTemplate.opsForValue().get(key);
                configCache.put(key.replace("config:", ""), value);
            }
        }
    }
    
    private void subscribeConfigChanges() {
        redisTemplate.getConnectionFactory().getConnection().subscribe(
            (message, pattern) -> {
                String[] parts = new String(message.getBody()).split("=");
                if (parts.length == 2) {
                    configCache.put(parts[0], parts[1]);
                }
            },
            "config-channel".getBytes()
        );
    }
    
    public String getConfig(String key, String defaultValue) {
        return configCache.getOrDefault(key, defaultValue);
    }
    
    // 更新配置的方法(管理端使用)
    public void updateConfig(String key, String value) {
        redisTemplate.opsForValue().set("config:" + key, value);
        redisTemplate.convertAndSend("config-channel", key + "=" + value);
    }
}

优缺点

优点

缺点

6. 自定义配置加载器和监听器

通过自定义Spring的PropertySource和文件监听机制,实现本地配置文件的动态加载。

实现方案

@Component
public class DynamicPropertySource implements ApplicationContextAware {
    
    private static final Logger logger = LoggerFactory.getLogger(DynamicPropertySource.class);
    private ConfigurableApplicationContext applicationContext;
    private File configFile;
    private Properties properties = new Properties();
    private FileWatcher fileWatcher;
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (ConfigurableApplicationContext) applicationContext;
        try {
            configFile = new File("config/dynamic.properties");
            if (configFile.exists()) {
                loadProperties();
                registerPropertySource();
                startFileWatcher();
            }
        } catch (Exception e) {
            logger.error("Failed to initialize dynamic property source", e);
        }
    }
    
    private void loadProperties() throws IOException {
        try (FileInputStream fis = new FileInputStream(configFile)) {
            properties.load(fis);
        }
    }
    
    private void registerPropertySource() {
        MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
        PropertiesPropertySource propertySource = new PropertiesPropertySource("dynamic", properties);
        propertySources.addFirst(propertySource);
    }
    
    private void startFileWatcher() {
        fileWatcher = new FileWatcher(configFile);
        fileWatcher.setListener(new FileChangeListener() {
            @Override
            public void fileChanged() {
                try {
                    Properties newProps = new Properties();
                    try (FileInputStream fis = new FileInputStream(configFile)) {
                        newProps.load(fis);
                    }
                    
                    // 更新已有属性
                    MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
                    PropertiesPropertySource oldSource = (PropertiesPropertySource) propertySources.get("dynamic");
                    if (oldSource != null) {
                        propertySources.replace("dynamic", new PropertiesPropertySource("dynamic", newProps));
                    }
                    
                    // 发布配置变更事件
                    applicationContext.publishEvent(new EnvironmentChangeEvent(Collections.singleton("dynamic")));
                    
                    logger.info("Dynamic properties reloaded");
                } catch (Exception e) {
                    logger.error("Failed to reload properties", e);
                }
            }
        });
        fileWatcher.start();
    }
    
    // 文件监听器实现(简化版)
    private static class FileWatcher extends Thread {
        private final File file;
        private FileChangeListener listener;
        private long lastModified;
        
        public FileWatcher(File file) {
            this.file = file;
            this.lastModified = file.lastModified();
        }
        
        public void setListener(FileChangeListener listener) {
            this.listener = listener;
        }
        
        @Override
        public void run() {
            try {
                while (!Thread.interrupted()) {
                    long newLastModified = file.lastModified();
                    if (newLastModified != lastModified) {
                        lastModified = newLastModified;
                        if (listener != null) {
                            listener.fileChanged();
                        }
                    }
                    Thread.sleep(5000);  // 检查间隔
                }
            } catch (InterruptedException e) {
                // 线程中断
            }
        }
    }
    
    private interface FileChangeListener {
        void fileChanged();
    }
}

优缺点

优点

缺点

7. Apollo配置中心

携程开源的Apollo是一个功能强大的分布式配置中心,提供配置修改、发布、回滚等完整功能。

实现步骤

<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>2.0.1</version>
</dependency>
# app.properties
app.id=your-app-id
apollo.meta=http://apollo-config-service:8080
@SpringBootApplication
@EnableApolloConfig
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
@Component
public class SampleService {
    
    @Value("${timeout:1000}")
    private int timeout;
    
    // 监听特定配置变更
    @ApolloConfigChangeListener
    public void onConfigChange(ConfigChangeEvent event) {
        if (event.isChanged("timeout")) {
            ConfigChange change = event.getChange("timeout");
            System.out.println("timeout changed from " + change.getOldValue() + " to " + change.getNewValue());
            // 可以在这里执行特定逻辑,如重新初始化线程池等
        }
    }
}

优缺点

优点

缺点

8. Nacos配置管理

阿里开源的Nacos既是服务发现组件,也是配置中心,广泛应用于Spring Cloud Alibaba生态。

实现步骤

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
# bootstrap.properties
spring.application.name=my-service
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
# 支持多配置文件
spring.cloud.nacos.config.extension-configs[0].data-id=database.properties
spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.extension-configs[0].refresh=true
@RestController
@RefreshScope
public class ConfigController {
    
    @Value("${useLocalCache:false}")
    private boolean useLocalCache;
    
    @GetMapping("/cache")
    public boolean getUseLocalCache() {
        return useLocalCache;
    }
}

优缺点

优点

缺点

9. Spring Boot Admin与Actuator结合

Spring Boot Admin提供了Web UI来管理和监控Spring Boot应用,结合Actuator的环境端点可以实现配置的可视化管理。

实现步骤

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.7.0</version>
</dependency>
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminServerApplication.class, args);
    }
}
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
spring.boot.admin.client.url=http://localhost:8080
management.endpoints.web.exposure.include=*
management.endpoint.env.post.enabled=true

Spring Boot Admin提供UI界面,可以查看和修改应用的环境属性。通过发送POST请求到/actuator/env端点修改配置。

优缺点

优点

缺点

10. 使用@ConfigurationProperties结合EventListener

利用Spring的事件机制和@ConfigurationProperties绑定功能,实现配置的动态更新。

实现方案

@Component
@ConfigurationProperties(prefix = "app")
@Setter
@Getter
public class ApplicationProperties {
    
    private int connectionTimeout;
    private int readTimeout;
    private int maxConnections;
    private Map<String, String> features = new HashMap<>();
    
    // 初始化客户端的方法
    public HttpClient buildHttpClient() {
        return HttpClient.newBuilder()
                .connectTimeout(Duration.ofMillis(connectionTimeout))
                .build();
    }
}
@Component
@RequiredArgsConstructor
public class ConfigRefresher {
    
    private final ApplicationProperties properties;
    private final ApplicationContext applicationContext;
    private HttpClient httpClient;
    
    @PostConstruct
    public void init() {
        refreshHttpClient();
    }
    
    @EventListener(EnvironmentChangeEvent.class)
    public void onEnvironmentChange() {
        refreshHttpClient();
    }
    
    private void refreshHttpClient() {
        httpClient = properties.buildHttpClient();
        System.out.println("HttpClient refreshed with timeout: " + properties.getConnectionTimeout());
    }
    
    public HttpClient getHttpClient() {
        return this.httpClient;
    }
    
    // 手动触发配置刷新的方法
    public void refreshProperties(Map<String, Object> newProps) {
        PropertiesPropertySource propertySource = new PropertiesPropertySource(
                "dynamic", convertToProperties(newProps));
        
        ConfigurableEnvironment env = (ConfigurableEnvironment) applicationContext.getEnvironment();
        env.getPropertySources().addFirst(propertySource);
        
        // 触发环境变更事件
        applicationContext.publishEvent(new EnvironmentChangeEvent(newProps.keySet()));
    }
    
    private Properties convertToProperties(Map<String, Object> map) {
        Properties properties = new Properties();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            properties.put(entry.getKey(), entry.getValue().toString());
        }
        return properties;
    }
}

优缺点

优点

缺点

方法比较与选择指南

方法易用性功能完整性适用规模实时性
@RefreshScope+Actuator★★★★★★★小型手动触发
Spring Cloud Config★★★★★★★中大型需配置
数据库存储★★★★★★★中型定时刷新
ZooKeeper★★★★★★中型实时
Redis发布订阅★★★★★★★中型实时
自定义配置加载器★★★★★小型定时刷新
Apollo★★★★★★★★中大型实时
Nacos★★★★★★★★中大型实时
Spring Boot Admin★★★★★★小型手动触发
@ConfigurationProperties+事件★★★★★★小型事件触发

总结

动态配置修改能够提升系统的灵活性和可管理性,选择合适的动态配置方案应综合考虑应用规模、团队熟悉度、基础设施现状和业务需求。

无论选择哪种方案,确保配置的安全性、一致性和可追溯性都是至关重要的。

以上就是SpringBoot动态修改配置的十种方法的详细内容,更多关于SpringBoot动态修改配置的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文