updating Beans marked as RefreshScope

2.1k views Asked by At

Here is my scenario: My micro service gets notified about some changes of its configuration from the central conf server. It can be partial update, or a full. I use @RefreshScope mark on relevant beans. The question is how to update marked beans, I mean reload them. Just to clarify: From Spring Cloud I use only RefreshScope.

Any ideas?

2

There are 2 answers

1
Kane On
  1. Add dependency org.springframework.boot:spring-boot-starter-actuator in your project.
  2. Refreshing configuration via call refresh endpoint.

For example, you configure your management endpoint like below. curl -X POST http://localhost:8001/manage/refresh will trigger refreshing changed configuration.

management: context-path: /manage port: 8001 security.enabled: false

0
Karim Masoud On

If you have different components that are affected by changes, then it's good to keep your configurations on repository and then you can add a publish-subscribe model for refreshing context in which all the affected components are to subscribe to an event that is published by your repository as a result of configurations change.

And for refreshing context we have two options:

  1. Hit refresh endpoint of your app by a post request.
  2. Get RefreshEndpoint bean by autowiring it, then apply refreshEndpoint.refresh(). This will refresh context at runtime.

In both solutions mark the beans of interest by @RefreshScope.