MrkK
MrkK

Reputation: 903

Spring config server refresh properties from local config file

In my Spring Cloud application I would like to let user to change some default settings. I keep the default properties in application.yml inside the jar and start my application with

-Dspring.config.additional-location=/some_location/properties_override.yml

When the user changes properties_override.yml I wanted to reload configuration without restarting the application. Spring config server seems to be an ideal mechanism for it, but I cannot figure out if it can work with the properties located locally on the file system. When I am looking at spring.cloud.config it seems to only only support git, jdbc and svn servers. None of these mechanisms is an option for me I am wondering if thee is a way to use Spring Cloud Config to reload configuration from a local file on the file system?

Using "native" profile seems to be a right way to go, but it created another problem: I need two entries in cloud.config.server.native.searchLocations: one for application.yml on the classpath (in module's src\main\resources\config folder) and the second one on the file path. Setting searchLocations to:

 searchLocations: classpath:/config,file:/some_location/properties_override.yml 

The properties in application.yml are not picked up.

If my application.yml I have entry

custom:
  property: foo

There is a Spring bean which has property tagged with

@Value("${custom.property"})

Without searchLocations in application.yml the property is being resolved. With searchLocations I am getting an exception:

 java.lang.IllegalArgumentException: Could not resolve placeholder 'custom.property' in value "${custom.property}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]

Upvotes: 4

Views: 5387

Answers (1)

Gurinder
Gurinder

Reputation: 993

Setting up a filesystem-based repository is the easiest way to accomplish this. Use spring.profiles.active=native this should work. Here is an example using a yml file:

server:
  port: 8888
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: file:///Users/gurinder/projects/application-name/configserver/src/main/resources/config/yourservice

And also check this link stackoverflow-question. In case you want to refresh your properties with the updated one. First, you need to use annotation @RefreshScope on the Spring boot Main application class or on that class which is holding those properties which you need to refresh. Second, You Call this URL :http://<yourserver>:8080/refresh which will refresh the values and replace them with newer ones.

Upvotes: 3

Related Questions