'How to trigger a refresh event?

From https://spring.io/guides/gs/centralized-configuration/:

You can force a bean to refresh its configuration (that is, to pull updated values from the Config Server) by annotating the MessageRestController with the Spring Cloud Config @RefreshScope and then triggering a refresh event.

How can we trigger this refresh event (for classes annotated with @ConfigurationProperties and @RefreshScope)? We don't want to use the spring actuator /refresh endpoint. What is necessary here, that the application fetches the config from configserver?



Solution 1:[1]

You can fire a RefreshEvent using an autowired ApplicationEventPublisher. Spring Cloud has a listener for this event in RefreshEventListener

@Autowired
private ApplicationEventPublisher eventPublisher;

public void fireRefreshEvent() {
  eventPublisher.publishEvent(new RefreshEvent(this, "RefreshEvent", "Refreshing scope");
}

References:

1st Reference

2nd reference

Solution 2:[2]

RefreshEndpoint bean can be autowired and used to refresh cloud configurations

Solution 3:[3]

To refresh your configuration properties to all of your microservices you can message broker ( Spring cloud bus ) through that you can publish the changes to all of your microservices.

Reference:

https://springbootdev.com/2018/07/17/spring-cloud-config-refreshing-the-config-changes-with-spring-cloud-bus-part-2/

Solution 4:[4]

Spring's RefreshEndpoint just calls refresh() on a ContextRefresher (see the source code). The simplest way to achieve the same without using the endpoint, is to do exactly the same.

@Autowired
private ContextRefresher contextRefresher;

public Set<String> refresh() {
    return contextRefresher.refresh();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yunus Temurlenk
Solution 2 Usman Anwar
Solution 3 GnanaJeyam
Solution 4 Karsten Gabriel