spring boot @refreshscope doesn't update datasource

孤人 提交于 2019-12-11 05:25:57

问题


I am trying to update DB datasource if the environment variable for DB.URL changes. Below is my class,

@SpringBootApplication
@ConfigurationProperties(value="myapp")
public class MyApp {
@Value("${myapp.db.url}")
String databaseURL;

@Value("${myapp.db.username}")
String databaseUsername;

@Value("${myapp.db.password}")
String databasePassword;

public static void main(String[] args) {
    SpringApplication.run(MyApp.class, args);
}

@Bean
@RefreshScope
@Primary
public DataSource getDataSource() {
    return DataSourceBuilder.create().username(databaseUsername).password(databasePassword).url(databaseURL)
            .driverClassName("org.postgresql.Driver").build();
}
}

But when I update environment DB.URL it doesn't make request to new DB.

I have referred the documentation as it is possible to update datasource, http://projects.spring.io/spring-cloud/spring-cloud.html#_refresh_scope

What is missing in my class?


回答1:


You need to move this @RefreshScope annotation on class context MyApp:

@SpringBootApplication
@ConfigurationProperties(value="myapp")
@RefreshScope
public class MyApp {
  ...
}

And also, make sure to do request POST on service: http://{your.api.url}/actuator/refresh, in order to refresh it after you had changed the properties.



来源:https://stackoverflow.com/questions/45562183/spring-boot-refreshscope-doesnt-update-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!