Building My First Microservice - Part 5 (Externalizing Configurations with GitHub)

In the previous post, we learned how to set up a local Spring Cloud Config Server instance to externalize configuration properties for our applications. While the local file-based approach is useful for development and testing purposes, it lacks the benefits of version control, collaboration, and centralized management that are essential for a production-ready microservices architecture.

In this post, we'll take our configuration management to the next level by setting up a Spring Cloud Config Server that retrieves configurations from a GitHub repository. By leveraging GitHub's powerful version control and collaboration features, we can streamline the management of configurations across multiple microservices and environments.

Benefits of using GitHub for Configuration Management:

1. Version Control: GitHub's built-in version control system allows us to track changes made to our configuration files over time. We can easily revert to previous versions if necessary, compare changes, and maintain a detailed history of all modifications.

2. Collaboration: Multiple developers can work on configurations simultaneously, thanks to GitHub's collaboration features. This promotes teamwork and facilitates code reviews, ensuring that configurations are thoroughly reviewed and tested before deployment.

3. Centralized Management: Instead of managing configurations separately for each microservice, we can store all configurations in a single, centralized GitHub repository. This simplifies the management process and ensures consistency across our entire microservices ecosystem.

4. Branching and Merging: GitHub's branching model allows us to create separate branches for different environments (e.g., development, staging, production) or specific features or bug fixes. This enables safe and controlled changes to configurations without affecting the main codebase.

5. Auditing and Accountability: GitHub's commit history and pull request system provide a clear audit trail, making it easy to track who made changes, when, and why. This level of accountability is crucial for maintaining the integrity of our configurations.


Setting up the Spring Cloud Config Server with GitHub:

1. Create a GitHub Repository: Log in to your GitHub account and create a new repository specifically for storing your microservices configurations.

2. Configuration Files: Add your configuration files (e.g., 'application.yml', 'environment.yml') to the newly created GitHub repository. These files should define properties specific to your microservices, such as database connection details, messaging broker settings, API keys, and other environment-specific or service-specific configurations.

Example:


# Database connection details
spring:
datasource:
url: jdbc:h2:mem:testdb # Replace with your actual database connection URL
username: user
password: password

# Messaging broker settings (e.g., RabbitMQ)
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest # Replace with your actual credentials

# API keys (replace with placeholder or actual values based on your needs)
api:
key1: your_api_key_1
key2: your_api_key_2

# Server configuration
server:
port: 8080

# Logging level (optional)
logging:
level: INFO


3. Push Configurations to GitHub: Commit and push the changes to the remote repository.

4. Spring Cloud Config Server Setup: Create a new Spring Boot project or add the necessary dependencies to an existing project. Include the 'spring-cloud-config-server' and 'spring-boot-starter-git' dependencies in your project's 'pom.xml' file:


<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-git</artifactId>
</dependency>


5. Configuration: In your project's 'application.yml' (or 'application.properties') file, define the configurations for your Config Server, including the port, application name, and the URL of your GitHub repository where the configuration files are stored:


server:
port: 8191

spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/<username>/<your-config-repo>.git
search-paths:
- 'config-data'


Replace '<username>' with your GitHub username and '<your-config-repo>' with the name of your configuration repository. The 'search-paths' property specifies the directory within the repository where your configuration files are stored. If your configuration files are in the root directory, you can omit this property.

6. Enable the Spring Cloud Config Server: In your main application class, add the '@EnableConfigServer' annotation to enable the Spring Cloud Config Server functionality:


@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}

}


7. Run the Config Server: Run your Spring Boot application to start the Config Server.

8. Consuming Configurations in a Microservice: In your microservice project, add the 'spring-cloud-starter-config' dependency to the 'pom.xml' file:


<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>


Use the @Value' annotation to inject configuration properties from the Config Server:


@RestController
public class MyService {

@Value("${message}")
private String message;

@GetMapping("/greeting")
public String greeting() {
return message;
}
}


Replace '${message}' with the actual property name defined in your configuration file on GitHub.

In your microservice's 'application.yml' (or 'application.properties') file, specify the location of the Config Server:


spring:
config:
import: optional:configserver:http://localhost:8191


Replace the URL and port with the appropriate values for your Config Server.

9. Profiles (Optional): Spring Cloud Config Server supports the concept of profiles, allowing you to manage different sets of configurations for different environments (e.g., development, staging, production) or specific use cases. You can create separate configuration files for each profile following the naming convention '<service-name>-<profile>.yml' (e.g., 'my-service-dev.yml', 'my-service-prod.yml').


To activate a specific profile, you can set the 'spring.profiles.active' property in your microservice's configuration file or as an environment variable:


spring:
profiles:
active: dev


By following these steps, you can seamlessly integrate your microservices with the Spring Cloud Config Server, leveraging GitHub's powerful version control and collaboration features for managing configurations. Any changes made to the configuration files in the GitHub repository will be automatically picked up by the Config Server, and your microservices will dynamically receive the updated configurations without requiring a restart or redeployment.

In the next post, we'll explore advanced features and considerations for using the Spring Cloud Config Server, such as encryption and decryption, refreshing configurations, configuration overrides, monitoring, and scaling strategies.

Post a Comment

Previous Post Next Post