Configuration with Pivotal Cloud Foundry and Spring Cloud Config

Brad Jolicoeur


One of the services included with Cloud Foundry is Spring Config Server. In conjunction with Steeltoe, which is an open source library to support .NET applications on Pivotal Cloud Foundry, you can specify some of your config to come from a git repository. This has my interest because it is simple and includes versioning and audit history for changes. At first, this seems to go against best practices as storing secrets in your source control is a big no-no. That said, Spring Cloud Config has the ability to encrypt/decrypt sensitive configuration values which solves that issue. 

I recently put together an example to see how well this works out. This article contains the steps I took to get my example project up and running. 

There are a few different components to this solution and it took me a little while to work out what each component's responsibility is. 

  • Pivotal Cloud Foundry: This is the glue that pulls everything together and provisions the containers, routes and services
  • Spring Cloud Config: This is the service that is responsible for pulling the config from Git 
  • Steeltoe: An open source library Pivotal provides to make creating distributed solutions easier to create and in this case helps pull config from the different sources including Spring Cloud Config.
  • GitHub: Hosts the Git repository with my configuration



To get started I created a configuration repo on GitHub. Since this is just a demo, there is no need to keep this secret, but I believe you can configure this to work with a secured git repo.

Once I had the repo created, I added a yaml config file with the same name as my project, as seen in the image below.

Next I created a json configuration file that will provide Spring Cloud Config with the path to your git repo with the configuration file. The image below has the path to my repo. It also specifies the branch "master" in the label field. This can be any branch, so if you are setting up for a development environment, you could direct this to your "development" branch or any other branch if you choose. 

I first created the cloudConfig.json file in Visual Studio, but the CF CLI did not seem to be able to read the encoding. I recreated this file in notepad and then this worked as expected.

Open up a cmd window and cd to the directory where your cloudConfig.json file is. Then login to CF with the command cf login.

After logging in with CF CLI, execute the command cf create-service p-config-server trial config-server. This will create your config server with the name 'config-server'. Note that the plan I used was 'trial'. This means that the config server is free, but will be deleted after 7 days as it is a trial. 

After the service is created, you can update it with the cloudConfig.json file by executing the command cf update-service config-service -c cloudConfig.json. 

Once this is completed, you can verify the configuration was applied in App Man by clicking the Manage link under the config-server service and checking the configuration. It should look something like the image below.

Now that we have the configuration server up and running, we can change our focus to the project. 

First, we need to update the manifest.yml with the config-server service. This will bind our project to the Spring Cloud Config service automatically when we push the project.  Note that 'config-server' is what I named the service when I created it above. 

Then I added the following Nuget packages to the project I created in my last article. The two Pivotal packages are where all the magic is for this example.

After adding the packages, I opened up startup.cs and added the following configuration to be able to inject the configuration into my classes. I also created a class to contain my new configuration properties called ConfigServerData. 

Config server data matches the structure in the appsettings.json file and the pcf-web-demo.yml file in my GitHub repo. When running in debug on my local workstation the config will come from the appsettings.json and when deployed it will use the pcf-web-demo.yml file. 

As you can see the appsettings.json file has Features and Wowfeature and the Wowfeature value is 'true'. The pcf-web-demo.yml has Wowfeature set to 'false' so we can see if it works.

Next I open up program.cs and add the cloud foundry configuration and configure logging.

Almost there! Now we want to see the results of our hard work so we need to update the controller and the view.

In the HomeController.cs I set up the configuration to be injected into the controller.

Once I have the configuration injected I grabbed a couple of settings to show them in the page. 

Once I implemented the ViewBag properties in the view, I was able to run the project on my workstation to show the output. As you can see Wowfeature is equal to 'true' which is our appsettings.json value.

Now run cf push in the CF CLI. If everything goes well, when you run your project after it is deployed, the Wowfeature will be 'false' from the configuration file on GitHub.

So a lot of small steps, but once everything is set up it should all just work. Well for at least the 7 days of your config server trial of Spring Cloud Config.  For a more durable configuration solution you will want to select a paid plan for Spring Cloud Config or if you are running PCF in your own environment, you will need to configure a service tile for Spring Cloud Config that meets your requirements.