Config Server usage in Microservices Architecture with help of Steeltoe & PCF in .Net 5

Whenever we’re implementing Cloud-Native microservices we need to follow the 12 factors. Within In 12 factors Config is also one factor. Store config in the environment. Application config’s vary from every ENV (i.e. QA, UAT & PROD). Some of config values are same for all ENV so that if we hard code those values into code means it’s violation of 12 factors principles of microservices of config.

To fulfill this principle (i.e. config) in cloud native microservice applications Spring Cloud Config server come into picture.

Spring Cloud Config:
Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments. The default implementation of the server storage backend uses git, so it easily supports labelled versions of configuration environments as well as being accessible to a wide range of tooling for managing the content.

Why Config Server Provider:

The Config Server is an application configuration service that gives you a central place to manage an application’s configuration values externally across all environments. As an application moves through the deployment pipeline from development to test and into production, you can use the config server to manage the configuration between environments and be certain that the application has everything it needs to run when you migrate it. The config server easily supports labelled versions of environment-specific configurations and is accessible to a wide range of tooling for managing its content.

Note: Please make sure to avoid sensitive information store in config server related version control repository.

Use case:

Create a centralized application configuration setting’s in GitHub repository. For cloud-native applications we need to use Pivotal Cloud Foundry (PCF) for efficient way of speedup build, deploy and operate the software. Create a small microservice which is consuming the application configuration from Spring Cloud Config server & update the configuration values in config server without restarting our microservice. Those updated configuration values should be reflected on our microservice.

very basic level example

Note: It’s only very basic level example for usage of config server in microservices.

Prerequisites:

1. Pivotal Cloud Foundry (PCF) account (i.e. PCF part of VMware Tanzu — https://login.run.pivotal.io/login It may differ )
2. CF CLI Installation (i.e. https://docs.run.pivotal.io/cf-cli/install-go-cli.html ) — Windows Platform choice.
3. .Net 5 web- API project (i.e. we can choose lower versions/different project also )
4. steeltoe Packages from NuGet
5. Within Pivotal Cloud Foundry we need Spring Cloud Config Server service in marketplace.
6. Create a .net core supported version of build packs with PCF.

Step 1:

Install CF CLI on your windows ENV (i.e. https://docs.pivotal.io/pivotalcf/2-3/cf-cli/install-go-cli.html ) & set path for CF CLI. Login to CF CLI through CMD by using your PCF account API (i.e. You can go to Tools option in PCF). After login choose your org & space.

CF CLI Installation

Step 2:

a. Create a configuration settings file (i.e. configInfo-qa.yml) GitHub repository like below.

GitHub config file

b. Create a Spring Cloud Config Server service within your space by using CF CLI (i.e. Using CMD) like below (i.e. https://docs.pivotal.io/spring-cloud-services/3-1/common/config-server/managing-service-instances.html)

cf create-service p.config-server standard config-server -c "{\"git\": { \"uri\": \"https://github.com/RamanaReddyV/ConfigServer.git\" } }"

For specific branch setup

cf create-service p.config-server standard config-server -c "{\"git\": { \"uri\": \"https://github.com/RamanaReddyV/ConfigServer.git\", \"label\": \"develop\" } }"

Step 3:
Create a Sample web api project (i.e. M_Service2) by using Visual studio like below

Sample M_Service2 web API

Add below NuGet packages.

Add highlighted packages

Note: Here remaining packages I added as part of Service Registry pattern.

Update Program.cs file like below.

Program.cs

Here GetLoggerFactory() is optional. It’s used to capture the logs within steeltoe lib’s.

Update the Startup.cs file like below

Startup.cs

Add small methods to read the Config server config values in WeatherForecastController.cs like below.

[ApiController][Route("[controller]")]public class WeatherForecastController : ControllerBase{private static readonly string[] Summaries = new[]{"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"};private readonly ILogger<WeatherForecastController> _logger;private IOptionsSnapshot<ConfigInfo> IConfigServerData { get; set; }private IConfigurationRoot Config { get; set; }public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configInfo,IOptionsSnapshot<ConfigInfo> configServerInfo){_logger = logger;if (configServerInfo != null){IConfigServerData = configServerInfo;}Config = configInfo as IConfigurationRoot;}[Route("GetMService2")][HttpGet]public IActionResult GetMService2(){_logger.LogInformation(" ================== GetMService2 called =====================");return Ok(" M_Service2 Called..");}[Route("GetConfigInfo")][HttpGet]public IActionResult GetConfigInfo(){if (IConfigServerData != null && IConfigServerData.Value != null){var configValues = IConfigServerData.Value;return Ok("M_Service2 Microservice  :  Config Settings FROM SCS Config Server :::: DB Timeout : " + configValues.DBTimeout + " :: Token Expiry : " + configValues.TokenExpiryTime + " :: IsDbErrorLog : " + configValues.IsDBErrorLog + " :: ErrorLogMode : " + configValues.ErrorLogMode);}else{return Ok("No config loaded from Spring Cloud Config Server....");}}[Route("Refresh")][HttpGet]public IActionResult Refresh(){if (Config != null){Config.Reload();return Ok("Your Application config settings refreshed.....!");}else{return Ok("Your Application config NOT refreshed.....!");}}}

Here Refresh() method is get latest configuration from config server with help of steeltoe lib’s. With out restarting our application. After triggering Refresh() method If you observe PCF application logs you’re able to see getting latest config values from config server like below.

logs for Steeltoe after triggering Refresh() method

Add manifest.yml file into M_Service2 Web API project and update file according to your PCF deployment. Here very basic level configuration on PCF env deployment like below. config-server service will bind the Micro_Service_2 application after deploying into PCF.

manifest.yml

config-server is name of config server which is created in step 2

Update the appsettings.Development.json file like below

appsettings

Note: After deploying into PCF config server “uri” should be updated with bound service URL with help of VCAP_SERVICES. steeltoe lib’s can able to access those VCAP_SERVICES related information to our appsettings file.

Step 4:

Publish the M_Service2 API into PCF by using CF CLI . If you’re using CI/CD pipeline means we need to configure according to pipeline rules.

For now we can deploy through manual by using CF CLI.
a. Publish M_Service2 API into any local directory (i.e. C:\MS2\). Make sure manifest.yml should be in published location.
b. Login CF CLI & select corresponding Space with help of CMD.
c. After login into PCF navigate to published location within same CMD CF CLI login session (i.e. cd C:\MS2 )
d. After navigated into published location M_Service2 API then type cf push
e. You’re able to see deployment completed status logs of PCF in CMD.

Step 5:

Login into you’re PCF account you’re able see your deployed app within DEV space. Go to Micro_Service_2 App you’re able see dashboard like below & Go to Service option. In service option page you’re able to see you’re registered service apps.

Micro_Service_2 Dashboard
bound Services
Spring Cloud Config Server (i.e. very Basic Level Configuration Settings )

SYNCHRONIZE MIRRORS : It will pull the latest commit from GitHub repository. Once synchronized completed we’ll get the Sync status as passed with latest commit id will be replicated.

Note: Configuration section will be replicated according to your SCS config server.

Step 6:

Access your M_Service2 application through deployed URL from PCF.
(i.e. https://microservice2.testdev.com/index.html ) it’s look like below

M_Service 2 deployed app
GetConfigInfo() result
Refresh() result

This Refresh() method is reload the configuration from config server with help of steeltoe’s lib’s. You may also see steeltoe logs based on log tracing status.

Logs for steeltoe for Refresh() method

References :

1. https://steeltoe.io

2. https://docs.pivotal.io/spring-cloud-services/3-1/common/config-server/index.html

3. https://www.cloudfoundry.org/

Note:

I’m sharing this as per my knowledge. Anywhere, I did wrong. please let me know. I want to update myself.

=============== Happy Coding ===============

Senior Software Engineer