Spring Cloud Config Server For Local Development

Spring Cloud Config Server For Local Development
by Brad Jolicoeur
07/15/2020

Managing secrets on your development machine can be a challenge.  This is evident in the high number of secrets that are regularly found publicly available in GitHub.  I recently found that installing a containerized version of Spring Cloud Config Server on my workstation has made managing those secrets without including them in my source control alongside the code a little easier.

Spring Cloud Config Server is designed to store application configuration for distributed systems in a centralized location. For local development this means the configuration is not part of your application. This is good as it means your config is not checked in next to your code and especially good if your code repo is publicly available.

The easiest way to get Config Server running on your local machine for development is to use a Docker Image of Config Server and Docker Toolbox to run the docker image. While there are a number of ways you can get this running, I put together what I believe are the most straight forward steps to get you up and running.

If you are developing .NET applications like me, you will want to utilize SteelToe Configuration to interface with Config Server. SteelToe Configuration will merge the Config Server settings into your IConfiguration implementation.

Resources:

Instructions:

  1. Install Docker Toolbox https://docs.docker.com/toolbox/toolbox_install_windows/

  2. Install Docker Compose https://docs.docker.com/compose/

  3. Create folders for configuration

    • c:\data\configserver
    • c:\data\configserver\config
  4. In virtual box manager there will be a VM labeled 'default'. Go into settings and add a shared folder for c:\data with folder name of data.

    • the shared folder will allow the vm to access your config yml files
    • This needs to match up with your volumes property in the docker-compose.yml further down in the next step of the instructions.
         volumes:
             - /data/configserver/config:/config
    

    /data/ = the vm shared folder label in statement above

  5. Create docker compose in configserver folder

    • Path: c:\data\configserver
    • Name file: docker-compose.yml
    • File Contents:
    version: "3.3"
    services:
            configserver:
                    image: hyness/spring-cloud-config-server
                    ports:
                    - "8888:8888"
                    volumes:
                    - /data/configserver/config:/config
                    environment: 
                    - SPRING_PROFILES_ACTIVE=native
    
  6. Create example config file in the config folder. The file name will determine the app and environment the config file will be served up by. In this example the 'demo' app and 'development' environment.

    • Path: c:\data\configserver\config
    • Name file: demo-development.yml
    • File Contents:
    ---
    Features:
            Wowfeature: true
    
  7. Launch the docker Quickstart Terminal (shortcut on desktop)

    1. Watch for the IP address, mine is 192.168.99.100
  8. In the Terminal window change directory to c:\data\configserver

    • Cmd: cd c:/data/configserver
  9. Load the container with docker compose

    • Cmd: docker-compose up
  10. Assuming your Docker IP address is the same go to the following URL

       {"name":"demo","profiles":["development"],"label":null,"version":null,"state":null,"propertySources":[{"name":"file:config/demo-development.yml","source":{"Features.Wowfeature":true}}]}
    

Once you have this set up, you can load up the docker container with docker-compose up -d which will run in disconnected mode in the background. Any changes you make to the config files will automatically get picked up by config server

To configure your .NET Core project to use Config server

  1. Add Nuget Steeltoe.Extensions.Configuration.ConfigServerCore

  2. Register the configuration

    configApp.AddJsonFile("appsettings.json", optional: true);
    configApp.AddJsonFile(
        $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
        optional: true);
    configApp.AddConfigServer(new ConfigServerClientSettings() { Environment = environmentName, TokenRenewRate = 3000 });
    
  3. In your appsettings.development.json config file add the config below:

    1. Note the application name must match your application name and the ip address needs to match your docker toolbox ip address.
    "spring": {
    "application": {
        "name": "demo"
    },
    "cloud": {
        "config": {
        "uri": "http://192.168.99.100:8888",
        "validate_certifates": false
        }
    }
    }
    

I use the appsettings.secret.json to store the config server location and add appsettings.secret.json to .gitignore so that it does not end up in my repo.

Please, leave a comment below if you found this helpful or you have questions about any of the steps.