Spring启动时重启数据源

时间:2017-09-04 07:08:19

标签: java spring spring-mvc spring-boot spring-boot-actuator

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

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

你能告诉我在Spring启动时做到这一点的最好方法吗?

4 个答案:

答案 0 :(得分:4)

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

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

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

database.properties

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

创建数据源,

@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);
}

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

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>

答案 1 :(得分:1)

您可以使用Spring的动态数据源路由并检查它是否有帮助?它是一种非常古老的技术,如果符合您的目的,可能会派上用场。

但请注意 - 这是数据源路由,而不是新的数据源配置。

https://spring.io/blog/2007/01/23/dynamic-datasource-routing/

答案 2 :(得分:1)

在我的项目中,我使用了multitenancy。基本上我在这样的属性中定义了几个数据源:

primary.datasource.url=jdbc:postgresql://localhost:5432/db_name?currentSchema=schema_name
primary.datasource.username=user
primary.datasource.password=password
primary.datasource.driverClassName=org.postgresql.Driver
primary.datasource.driver-class-name=org.postgresql.Driver

secondary.datasource.url=jdbc:postgresql://localhost:5432/other_db?currentSchema=schema
secondary.datasource.username=user
secondary.datasource.password=password
secondary.datasource.driverClassName=org.postgresql.Driver
secondary.datasource.driver-class-name=org.postgresql.Driver

default.datasource.url=jdbc:postgresql://localhost:5432/default_db?currentSchema=public
default.datasource.username=user
default.datasource.password=password
default.datasource.driverClassName=org.postgresql.Driver
default.datasource.driver-class-name=org.postgresql.Driver

然后在配置类中定义了多个数据源:

@Bean
@Primary
@ConfigurationProperties(prefix="primary.datasource")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="secondary.datasource")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="default.datasource")
public DataSource defaultDataSource(){
    return DataSourceBuilder.create().build();
}

并根据thisthis文章配置了多租户。
优点:

  • 简易租户开关,可以手动触发,甚至配置为在请求(过滤器)中的某个特定标头上触发。
  • 可以配置为在模式或数据库之间切换。
  • 动态发生(您不必重新启动bean)

缺点:

  • 您必须在属性文件中定义所有数据库可能性。
  • 你必须关闭模式验证,因为它会变得疯狂。

答案 3 :(得分:0)

看看this article希望它有用:)