Config Server
Config Server
The Config Server provides a centralized configuration management system for externalizing configurations in a distributed system.
- Centralized configuration management
- Supports external storage systems (Git, Vault)
- Dynamic configuration updates across services
- Helps in maintaining consistency and reducing configuration duplication

configurations examples
config-repo/
user-service.properties
microservice-two.properties
user-management-service.properties
application.properties (shared config for all services)
application-dev.properties (dev-specific config)
application-prod.properties (prod-specific config)
user-service.properties (Product Service Configuration)
theses files just for infos
#YOU CAN PUT ANYTHING IN THIS CONFIGURATION FILE LIKE
a=1
test=abc
spring.application.name=user-service
spring.profiles.active=dev
#must be real and the server must be runing
spring.datasource.url=jdbc:mysql://localhost:8112/springbootdbs
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
server.port=8081
server.servlet.context-path=/api
jwt.secret=mySecretKey
jwt.expiration=3600000
jwt.issuer=myapp.com
email.service.host=smtp.mailtrap.io
email.service.port=587
email.service.username=user@example.com
email.service.password=password
email.service.from=no-reply@example.com
error.messages.user-not-found=User not found
error.messages.registration-failed=Registration failed
logging.level.root=INFO
logging.level.com.example.microserviceone=DEBUG
service.inventory.url=http://inventory-service:8081/api/inventory
service.inventory.timeout=5000
service.pricing.api-key=ABCD1234EFGH5678
security.oauth2.client-id=product-service-client
security.oauth2.client-secret=superSecretClientKey
security.oauth2.token-uri=https://authserver.company.com/oauth/token
features.enable-discounts=true
microservice-two.properties (Order Service Configuration)
# Configuration specific to Microservice-Two (Order Service)
# Datasource Configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/order_db
spring.datasource.username=order_user
spring.datasource.password=orderPassword789
spring.datasource.driver-class-name=org.postgresql.Driver
# Logging Configuration
logging.level.root=WARN
logging.level.com.example.microservicetwo=DEBUG # Detailed logging for this microservice
# Payment Service Configuration
service.payment.gateway-url=https://payments.gateway.com/v2/process
service.payment.api-key=5678XYZ0987QRST # API key for payment processing
# Notification Service Configuration
service.notification.url=http://notification-service:8082/api/notify # Notification service URL
service.notification.retry-attempts=3 # Number of retry attempts for notifications
# Security Configuration
security.jwt.secret=anotherVerySecretKey
security.jwt.expiration=3600000 # 1 hour expiration for JWT tokens
# Feature Toggles
features.enable-cancellation=true # Feature toggle for allowing order cancellation
user-management-service.properties (User Management Service Configuration)
# Configuration specific to User Management Service
# Datasource Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/user_db
spring.datasource.username=user_manager
spring.datasource.password=userPassword123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Logging Configuration
logging.level.root=INFO
logging.level.com.example.usermanagement=DEBUG # Detailed logging for user management service
# Security Configuration
security.password.strength=10 # Password strength requirement
security.password.reset-token-expiration=3600000 # 1 hour for reset token expiration
# Email Service Configuration
service.email.smtp-server=smtp.example.com
service.email.port=587
service.email.username=email_user
service.email.password=emailPassword456
# Feature Toggles
features.allow-user-registration=true # Feature toggle for user registration
Exemple

- Config Server: This will serve as the central place for external configuration.
- microservice-one (Product Service Configuration): A simple service that fetches its configuration from the Config Server.
- microservice-two Another service that fetches its configuration from the Config Server.
Step 1: Create the Config Server
Créer un nouveau Spring boot project avec : Spring Initializr.avec les dependencies:
- Spring Web
- Spring Cloud Config Server
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud Config Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

application.properties
server.port=8888
# Spring Cloud Config Server settings
spring.cloud.config.server.native.searchLocations=file:///C:/Users/user/Desktop/EMsi/configServer/config-repo/
spring.profiles.active=native
file:///C:/Users/user/Desktop/EMsi/configServer/config-repo/microservice-one.properties
dabatase configuration must be real and MYSQL server must be runing
microservice-one is the name of a client microservice
spring.datasource.url=jdbc:mysql://localhost:3306/YOU HAVE TO CREATE A DB
spring.datasource.username=root
spring.datasource.password=
# Optional settings
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
logging.level.root=INFO
logging.level.com.example.microserviceone=DEBUG
service.inventory.url=http://inventory-service:8081/api/inventory
service.inventory.timeout=5000
service.pricing.api-key=ABCD1234EFGH5678
security.oauth2.client-id=product-service-client
security.oauth2.client-secret=superSecretClientKey
security.oauth2.token-uri=https://authserver.company.com/oauth/token
features.enable-discounts=true
Enable Config Server
import org.springframework.boot.SpringApplication; // Import de la classe SpringApplication, utilisée pour démarrer une application Spring Boot
import org.springframework.boot.autoconfigure.SpringBootApplication; // Import de l'annotation SpringBootApplication, qui active la configuration automatique de Spring Boot
import org.springframework.cloud.config.server.EnableConfigServer; // Import de l'annotation EnableConfigServer, qui active les fonctionnalités du serveur de configuration
@SpringBootApplication // Indique que cette classe est une application Spring Boot
@EnableConfigServer // Active les fonctionnalités du serveur de configuration Spring Cloud
public class ConfigServerApplication { // Déclaration de la classe principale de l'application
public static void main(String[] args) { // Méthode principale qui sert de point d'entrée pour l'application
SpringApplication.run(ConfigServerApplication.class, args); // Démarre l'application Spring Boot
}
}

Create Configuration Files
Create Client microservice-ONE
Créer les trois projets Spring Boot Services user-service,microservice-two avec les dependencies:- Spring Web
- Spring Cloud Client config
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Configuration Service user-service application.properties
#you have to use the same name as the file microservice-one.properties
spring.application.name=microservice-one
spring.cloud.config.uri=http://localhost:8888
spring.config.import=optional:configserver:
server.port=8881
Implement Client Services :user-service
package com.emsi.servicBB;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class ServicBbApplication {
public static void main(String[] args) {
SpringApplication.run(ServicBbApplication.class, args);
}
}
ProductServiceConfig
package com.emsi.servicBB.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ProductServiceConfig {
@Value("${spring.datasource.url}")
private String databaseUrl;
@Value("${spring.datasource.username}")
private String databaseUsername;
@Value("${spring.datasource.password}")
private String databasePassword;
@Value("${spring.datasource.driver-class-name}") // Driver class
private String driverClassName;
@Value("${service.inventory.url}")
private String inventoryServiceUrl;
@Value("${security.oauth2.client-id}")
private String clientId;
@Value("${security.oauth2.client-secret}")
private String clientSecret;
// Other properties...
// Getters for the properties
public String getDatabaseUrl() {
return databaseUrl;
}
public String getDatabaseUsername() {
return databaseUsername;
}
public String getDatabasePassword() {
return databasePassword;
}
public String getInventoryServiceUrl() {
return inventoryServiceUrl;
}
public String getClientId() {
return clientId;
}
public String getClientSecret() {
return clientSecret;
}
// Add other getters as needed
}
ProductServiceConfig
package com.emsi.servicBB;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.emsi.servicBB.config.ProductServiceConfig;
@RestController
public class ProductController {
private final ProductServiceConfig config;
public ProductController(ProductServiceConfig config) {
this.config = config;
}
@GetMapping("/config")
public String getConfig() {
return "Database URL: " + config.getDatabaseUrl() +
", Inventory Service URL: " + config.getInventoryServiceUrl();
}
}
Run the Applications
Run the Config Server: Start the Config Server application.
http://localhost:8881/config