Spring Config server add property at runtime

五迷三道 提交于 2020-01-13 04:37:47

问题


I want to add some property at runtime in spring config server and it should be available to all client applications with @Value annotation.

I wont have this property predefine because I am going calculate that value in spring config server and add to environment.

Can you please help me understand what is best way to achieve this.


回答1:


Spring cloud configuration contains a feature named 'RefreshScope' which allows to refresh properties and beans of a running application.

If you read about spring cloud config, it looks like it can only load properties from a git repository, but that is not true.

You can use RefreshScope to reload properties from a local file without any need to connect to an external git repository or HTTP requests.

Create a file bootstrap.properties with this content:

# false: spring cloud config will not try to connect to a git repository
spring.cloud.config.enabled=false
# let the location point to the file with the reloadable properties
reloadable-properties.location=file:/config/defaults/reloadable.properties

Create a file reloadable.properties at the location you defined above. You can leave it empty, or add some properties. In this file you can later, at runtime, change or add properties.

Add a dependency to

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>

All beans, that are using properties, that may be changed during runtime, should be annotated with @RefreshScope like this:

@Bean
@RefreshScope
Controller controller() {
    return new Controller();
}

Create a class

public class ReloadablePropertySourceLocator implements PropertySourceLocator
{
       private final String location;

       public ReloadablePropertySourceLocator(
           @Value("${reloadable-properties.location}") String location) {
           this.location = location;
        }

    /**
     * must create a new instance of the property source on every call
     */
    @Override
    public PropertySource<?> locate(Environment environment) {
        try {
            return new ResourcePropertySource(location);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Configure Spring to bootstrap the configuration using that class.

Create (or extend) the META-INF/spring.factories file in your resource folder:

org.springframework.cloud.bootstrap.BootstrapConfiguration=your.package.ReloadablePropertySourceLocator

This bean will read the properties from the reloadable.properties. Spring Cloud Config will reload it from disk, when you refresh the application.

Add runtime, edit reloadable.properties as you like, then refresh the spring context. You can do that by sending a POST request to the /refresh endpoint, or in Java by using ContextRefresher:

@Autowired
ContextRefresher contextRefresher;
...
contextRefresher.refresh();

This should also work, if you choose to use it in parallel to properties from a remote git repository.



来源:https://stackoverflow.com/questions/43935183/spring-config-server-add-property-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!