问题
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