Spring启动时重启数据源 [英] Spring boot reset datasource on the fly

查看:173
本文介绍了Spring启动时重启数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spring配置文件或自定义数据库属性文件中更改DB属性(如DB名称,密码或主机名)时更新Spring Boot中的数据源。当属性发生变化时,应用程序必须通过监听属性的更改来自行更新。



一旦数据库配置发生变化,我就会使用Spring执行器来重启bean。但是用户必须明确地发布一个post请求才能重新启动。通过监听更改并更新数据源,必须避免此步骤。



您能告诉我在Spring启动时执行此操作的最佳方法吗?

解决方案

找到了一种即时更新数据源的方法,



我给了外部spring配置文件,其中包含应用程序的数据库属性,然后使用@RefreshScope为数据源bean刷新属性。



线程监视文件更改并调用执行器refresh()方法。



database.properties

  dburl = jdbc:// localhost: 5432 / dbname 
dbusername = user1
dbpassword = userpwd

创建数据源,

  @RefreshScope 
公共类DBPropRefresh {
@Value($ {dburl})
private String dbUrl;

@Value($ {dbusername})
private String dbUserName;

@Value($ {dbpassword})
private String dbPassword;

@Bean
@RefreshScope
public DataSource getDatasource(){
返回新的DatasourceBuilder()。create()。url(dbUrl).username(dbUserName)。密码(DBPASSWORD);
}

将外部配置文件提供给应用程序,



java -jar myapplication.jar --spring.config.location = database.properties



<我创建了一个Java线程类来监视database.properties文件的更改。关注 https://dzone.com/articles/how-watch-file-system -changes
当有变化时,它会调用refreshEndPoint.refresh()。



在pom.xml中,

 < dependency> 
< groupId> org.springframework.boot< / groupId>
< artifactId> spring-boot-starter-actuator< / artifactId>
< version> 1.5.6.RELEASE< / version>
< / dependency>


I am trying to update datasource in Spring Boot when the DB property like DB name, password or hostname changes in the spring configuration file or custom DB property file. When the property changes the application has to update by its own by listening changes to property.

I was using Spring actuator to /restart beans once the DB configuration is changed. But user has to explicitly make a post request to restart. This step has to be avoided by listening to the changes and update datasource.

Can you tell me the best way to do this in Spring boot?

解决方案

Found a way to update datasource on-the-fly,

I have given external spring config file which contains DB properties to the application and then refreshed the properties using @RefreshScope for the datasource bean.

A thread monitors the file changes and makes a call to actuator refresh() method.

database.properties

dburl=jdbc://localhost:5432/dbname
dbusername=user1
dbpassword=userpwd

Creating datasource,

@RefreshScope
public class DBPropRefresh{
@Value("${dburl}")
private String dbUrl;

@Value("${dbusername}")
private String dbUserName;

@Value("${dbpassword}")
private String dbPassword;

@Bean
@RefreshScope
public DataSource getDatasource(){
return new DatasourceBuilder().create().url(dbUrl).username(dbUserName).password(dbPassword);
}

Giving external config file to the application,

java -jar myapplication.jar --spring.config.location=database.properties

I have created a Java thread class to monitor database.properties file changes. Followed https://dzone.com/articles/how-watch-file-system-changes When there are changes then it makes call to refreshEndPoint.refresh().

In pom.xml,

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>1.5.6.RELEASE</version>
    </dependency>

这篇关于Spring启动时重启数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆