Spring boot app on Cloud with external properties

2019-09-14 13:43发布

问题:

I have a spring boot application which needs to use a property file. This application will be on the cloud. So, the property file will also be on the coud.

I am using cloud foundry to communicate with the cloud and use 'cf push' to push my spring-boot application.

My question : 1. Where and how to store a properties file? Can it be stored on the cloud? How? Or does it need to be stored on git and reference it in your application? 2. How to use this properties file which is at the external location ?

回答1:

You cant have file system access in cloud foundry. You have to keep the config file in classpath itself and load the properties from classpath which is also the default behaviour with Spring boot. Spring boot application.properties file (which is inside src/main/resource) goes inside your jar and springs loads it. Hence when you build a embedded jar/war it goes inside classpath and works directly.

The cloud foundry way of handling properties is through VCAP Variables. You can externalize these properties using the deplyoment manifest which is used during the cf push. Inside your manifest yml file define properties as below.

env:
    myprop1: myvalue1
    myprop2: myvalue2

Using this approach all the properties for your app would be available as normal System Properties. You can access them in your application as System.getProperties() inside your Java code.

https://docs.run.pivotal.io/devguide/deploy-apps/environment-variable.html#VCAP-APPLICATION 

https://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html

The major drawback with these two approaches is that the application needs to be rebuilt/repushed to the cloud foundry. The application cant read change in the properties dynamically once application is up and running.

To overcome this Spring-cloud has a project cloud-config. This can be backed by GIT or few other types of Datasource like File System/ Vault etc. This run as a different configuration service. You can define the config server inside your application properties file which makes application read properties from the Config service.

https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html

Mostly when u need to centralize the properties in a microservice architecture you can go for config service approach.



回答2:

As rightly pointed out, it is best suited to use the Spring-Cloud Config service. The service would have the credentials to hit a Git Repo (url,username and password mostly) and then you can get the properties.

Usually one would define deployment files( manifest files with profiles). So, if you go with a Test environment giving Dspring.profiles.active = Test, your application would use this to load up.

The other side of the story is to have multiple profiles defined in CloudConfigService as well. You can switch between those by using spring.cloud.config.label = youLabel. Using ConfigService does help since it also provides a easy way to toggle between configs using the profile selection