Rony Nguyen
Rony Nguyen

Reputation: 1144

Spring cloud - Refresh scope - Get runtime value of configuration property of a spring-bean

I'd like to expose the current value of the configuration property of a spring-bean using spring-actuator https://docs.spring.io/spring-boot/docs/current/actuator-api/htmlsingle/#env

GET /actuator/env/{props}

I have 2 services:

The cloud config have 2 configurations are maximum-upload-allow = 1M and file-type = png

Application service load those configs from the cloud-config service

like:

@Configuration @Getter
public class ApplicationConfigurationProperty {
  @Value("${maximum-upload-allow}")
  private String maximumImageSize;
}

@Configuration  @RefreshScope  @Getter
public class FileConfigurationProperty {
  @Value("${file-type}")
  private String fileType;
}

Now when I update configuration value maximum-upload-allow = 2M and file-type = jpg

Then I do refresh-scope by call bus-refresh https://cloud.spring.io/spring-cloud-static/spring-cloud-bus/2.1.4.RELEASE/multi/multi__bus_endpoints.html

I'd like to see my configurations using spring-actuator are:

GET /actuator/env/maximum-upload-allow => 1M (because No refreshscope)

GET /actuator/env/file-type => jpg (I marked as refreshscope)

but actually, spring-actuator return both new values (2M and jpg)

Q: How I can get my runtime value of maximum-upload-allow is 1M (current value because of NO RefreshScope here?

--- Update

@Configuration @Setter @Getter @ConfigurationProperties
public class ApplicationConfigurationProperty {
  @Value("${maximum-upload-allow}")
  private String maximumImageSize;
}

This configuration value refreshed without @RefreshScope annotation

I think these is correct behaviours mention here https://cloud.spring.io/spring-cloud-static/spring-cloud-bus/2.1.4.RELEASE/multi/multi__bus_endpoints.html

Thank you in advance.

Upvotes: 1

Views: 1196

Answers (1)

Rony Nguyen
Rony Nguyen

Reputation: 1144

I found a simple solution is to implement a new actuator endpoint which is load @RefreshScope and use Reflection to read bean's properties

Upvotes: 0

Related Questions