是否可以从PropertyPlaceholderConfigurer
重新加载属性并刷新@Value
渲染器?
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws Exception {
final ClassPathResource classPathRessource = new ClassPathResource(PROPERTIES_FILE);
final Properties fileProperties = loadDbPropertiesFromInputStream(classPathRessource.getInputStream());
// Instantiate properties dataSource
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(fileProperties.getProperty(PROPERTY_KEY_DATABASE_DRIVER));
dataSource.setUrl(fileProperties.getProperty(PROPERTY_KEY_DATABASE_URL));
dataSource.setUsername(fileProperties.getProperty(PROPERTY_KEY_DATABASE_USERNAME));
dataSource.setPassword(fileProperties.getProperty(PROPERTY_KEY_DATABASE_PASSWORD));
// Init PropertyPlaceholderConfigurer
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setLocation(classPathRessource);
// Add properties from defined database
propertyPlaceholderConfigurer.setPropertiesArray(ConfigurationConverter.getProperties(getDatabaseConfiguration(dataSource)));
return propertyPlaceholderConfigurer;
}
属性是从数据库中加载的,如果数据库发生更改,我应该重新启动后端服务器。那么有什么办法可以避免这种情况?
谢谢
答案 0 :(得分:1)
您有两种选择:
在 pom.xml 中添加spring-boot-starter-actuator
依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
添加新更改后,如果要查看该更改,则必须调用以下网址:
http://ip:port/actuator/refresh
但是问题在于刷新发生在单个服务器上。
但是为此,您必须使用一些消息传递队列系统,例如Kafka,RabbitMQ等。
我将只显示RabbitMQ。
首先安装Rabbit MQ并运行
在application.properties中添加配置以连接到Rabbit MQ。
在这里阅读:https://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html
在 pom.xml 中添加spring-boot-starter-amqp
和spring-boot-starter-actuator
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
添加新更改后,如果要查看该更改,则必须调用以下网址:
http://ip:port/actuator/bus-refresh
加点:刷新发生在所有服务器上。
注意:您可以使用@RefreshScope
自动刷新@Value
渲染器。例子:
@Configuration
@RefreshScope
public class AppConfig {
@Value("${some.value}")
private String value;
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws Exception {
final ClassPathResource classPathRessource = new ClassPathResource(PROPERTIES_FILE);
final Properties fileProperties = loadDbPropertiesFromInputStream(classPathRessource.getInputStream());
// Instantiate properties dataSource
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(fileProperties.getProperty(PROPERTY_KEY_DATABASE_DRIVER));
dataSource.setUrl(fileProperties.getProperty(PROPERTY_KEY_DATABASE_URL));
dataSource.setUsername(fileProperties.getProperty(PROPERTY_KEY_DATABASE_USERNAME));
dataSource.setPassword(fileProperties.getProperty(PROPERTY_KEY_DATABASE_PASSWORD));
// Init PropertyPlaceholderConfigurer
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setLocation(classPathRessource);
// Add properties from defined database
propertyPlaceholderConfigurer.setPropertiesArray(ConfigurationConverter.getProperties(getDatabaseConfiguration(dataSource)));
return propertyPlaceholderConfigurer;
}
}
答案 1 :(得分:0)
是的,Spring在这种情况下提供了Spring云总线,您可以使用它来刷新数据而无需重新启动服务器,而且可以使用它来刷新应用程序的多个实例。