简体   繁体   中英

Use Spring Boot Cloud Config to change application.properties in classpath

I have to change my custom defined spring properties (defined via @ConfigurationProperties beans) during runtime of my Spring Boot application.

Is there any elegant way of doing this using Spring Cloud Config?

I don't want to use an external application.properties in a git repository (as the spring boot application gets shipped to customers and I dont' want to create a git repository for everyone of them).

I just want to access and change the local application.properties (the one in the classpath, located in src/main/resources) file in my Spring container or (if thats not possible) in the Spring Cloud Config Server, which I could embed into my Spring Boot app. Is that possible somehow?

BTW: The goal is to create a visual editor for the customers, so that they can change the application.properties during runtime in their spring boot app.

Spring Boot supports profile based application configuration. Just add application-<profile>.properties file. Then just when running the application select a profile depending on the environment making use of spring.profiles.active .

-Dspring.profiles.active=dev

This will run the application with application-dev.properties file (overriding the default application.properties , ie you can just leave the common stuff in the default file and change the rest depending on the env)

On a side note, having a repo for configuration is not a must. You could just place them in the class path and give a search-location .

spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:configs/

It actually is possible and in the end quite easy to achieve. It just took me a whole day to get all the information together. Maybe this helps someone:

Just add the @EnableConfigServer annotation to your spring boot main class to add an embedded spring cloud config server to your application.

Then, add the following line to your applicaiton.properties to tell the config server where to locate your config files

cloud.config.server.native.search-locations: classpath:/

These lines are needed to be able to alter your config during runtime:

management.endpoints.web.exposure.include: '*'
endpoint.env.post.enabled: true

Now you have to run your application using the "native" profile as your active spring boot profile (you can of course add other profiles if needed)

Now you can use spring cloud config endpoints to get and alter your config during runtime.

See https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_server.html#_file_system_backend for more info

Maybe what your looking for could be achieved with Spring cloud config and spring cloud bus. It's explained here: https://cloud.spring.io/spring-cloud-config/reference/html/#_push_notifications_and_spring_cloud_bus

In summary, any change on configuration sent an event to the spring cloud bus and you can then reload app context or configuration with new properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM