Spring Config Integration With a PCF Application: A Step-by-Step Guide
A developer takes us through the process of integrating a Spring config file with a PCF application, including all the code you need to get started.
Join the DZone community and get the full member experience.Join For Free
Why Spring Configuration?
Spring Cloud Config provides support for externalized configuration in a distributed system. It allows you to dynamically push updates to configuration properties to the application without needing to restart or redeploy.
Step-by-Step Guide to Integrate With a PCF Application
Step 1: Add a Maven Dependency
Step 2: Add refreshscope and EnableAutoConfiguration to the Model Class
If you are autowiring this model object in any other class then make sure to add refreshscope to that class as well.
Step 3: Enable Actuator Endpoints
By default only the
/info endpoints are enabled. Enable other endpoints one-by-one by declaring explicitly or enabling them all by using a wildcard character.
Add the below line to your
application.properties file to enable the
You may additionally disable default security for this tutorial by adding the below code to
or by adding the below lines of code to your Spring Boot application:
Note: For this tutorial, security is disabled in order to access actuator endpoints. In a prod environment you must consider security.
Step 4: Add a Property to the application.properties File
Step 5: Build and Deploy the App to Cloud Foundry
Run the above command from the root folder of the application. Use the
manifest.yml file to declare your app name:
Step 6: Create an Instance of Spring Server in CF
Select Spring Config from PCF Marketplace to create an instance.
Use JSON to enter GitLab information:
Use the below format to enter your JSON data:
uri- the HTTP URL to the GitLab URL.
searchPaths- the GitLab URL subfolder that the application should search through for the config file. If this is not specified then the app will search in the current folder specified by the "uri" attributes.
username- user account to login to the above GitLab URL. It must have read access to the GitLab URL from where it will read the config file.
password- password attached to the GitLab user name.
label- specify which GitLab branch to use to read the config file. If not specified then it defaults to the "master branch."
Step 7: Bind Your App to the Above Service Instance
If the service instance is created successfully then it can be viewed from the Service Instance manager:
If it is successfully bound to the app then you should see a log entry as below:
Step 1: Add/Update the Property Value in a Config File in GitLab
- Create a file named
aspringconfig-demo-cloud.properties(filename follows the naming convention
<application_name>-<active-profile-name>.properties) under the folder that was provided in the
serachPathsproperty above. Be sure to put this file under the GitLab URL provided in the
- Add a new value for
- Commit the file to GitLab.
Step 2: Call the Actuator/Refresh Endpoint to Refresh/Reload the Property
The property that was changed in Step 1 will be returned by this call. Your application now has a reference to the updated value:
How Does it Work Internally?
When a Spring bean is declared with
@refreshscope, a proxy wrapper gets created for that bean. This proxy class interacts with underlying an bean class to return the value.
When a refresh event occurs in Spring:
- invalidate the proxy class.
- reload the config file.
- recreate the underlying bean class.
- return the new value.
Though this feature has the benefit of updating to a new value at runtime, care should be taken considering that on a refresh event Spring beans become invalid until re-creation. If a user is using the bean at that time they may experience a 500 error or similar error.
- Precedence rules for GitLab config files are also the same as in a regular Spring Boot application:
- Active profiles take precedence over defaults.
- Profile-specific property files override the application-named property file which overrides non-specific property files such as the
If the app name is "foo" and profile name is "dev" then
foo-dev.properties overrides the
foo.properties file. The
foo.properties file overrides the
When multiple files are specified, the latter ones can override the values of earlier ones.
Opinions expressed by DZone contributors are their own.