Set up a Spring Cloud Config Server to manage configurations for UserService
, ProductService
, and OrderService
. Centralizing configuration will allow updates without redeploying each service.
- Inside the MicroservicesProject workspace, create a new folder named
ConfigServer
.
- Open the Command Palette (View > Command Palette or
Ctrl+Shift+P
). - Select Spring Initializr: Generate a Maven Project.
- Configure the options as follows:
- Group Id:
com.microservices
- Artifact Id:
config-server
- Name:
ConfigServer
- Group Id:
- Save the project in the
ConfigServer
folder.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- Open
ConfigServerApplication.java
inConfigServer/src/main/java/com/microservices/configserver
. - Add the
@EnableConfigServer
annotation:@SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
- In
ConfigServer/src/main/resources
, openapplication.properties
. - Set the server port and add properties for the Git repository:
server.port=8888 spring.application.name=config-server eureka.client.service-url.defaultZone=http://localhost:8761/eureka spring.cloud.config.server.git.uri=https://github.com/your-git-repo/config-repo spring.cloud.config.server.git.clone-on-start=true
- Replace
https://github.com/your-git-repo/config-repo
with the URL of your Git repository.
- Create a new Git repository named
config-repo
. - Inside the repository, create configuration files for each service:
user-service.properties
product-service.properties
order-service.properties
- Add configuration settings, such as:
# Example for user-service.properties server.port=8081 spring.datasource.url=jdbc:mysql://localhost:3306/user_db spring.datasource.username=root spring.datasource.password=your_password
- Commit the configuration files and push them to the Git repository.
- For each microservice (
UserService
,ProductService
,OrderService
), openapplication.properties
. - Replace the local configurations with a reference to the Config Server:
spring.application.name=user-service spring.cloud.config.uri=http://config-server:8888 eureka.client.service-url.defaultZone=http://eureka-server:8761/eureka
- Run the app
mvn spring-boot:run
- Ensure it is running on
http://localhost:8888
.
- Open a browser and access configurations for each service to verify:
http://localhost:8888/user-service/default
http://localhost:8888/product-service/default
http://localhost:8888/order-service/default
- Each endpoint should display the configuration properties for the corresponding service.
- Ensure EurekaServer, UserService, ProductService, and OrderService are running in VS Code.
- Verify that each service is successfully registered in Eureka.
-
Open
UserService
’sapplication.properties
and add:management.endpoints.web.exposure.include=* management.endpoints.web.base-path=/actuator
-
Open
ApiGateway
’sapplication.properties
and add:spring.cloud.gateway.routes[0].id=user-service spring.cloud.gateway.routes[0].uri=lb://user-service spring.cloud.gateway.routes[0].predicates[0]=Path=/api/users/** spring.cloud.gateway.routes[0].filters[0]=RewritePath=/api/users/(?<segment>.*), /${segment}
- In the
ConfigServer
directory, create aDockerfile
:FROM openjdk:17 EXPOSE 8888 ADD target/config-server-0.0.1-SNAPSHOT.jar config-server.jar ENTRYPOINT ["java", "-jar", "config-server.jar"]
- Build the Docker image for Config Server:
docker build -t config-server .
- In the MicroservicesProject directory, open
docker-compose.yml
. - Add the Config Server configuration:
config-server: build: ./ConfigServer/config-server ports: - "8888:8888" depends_on: - eureka-server
- Modify each service’s
application.properties
to connect to the Dockerized Config Server:spring.cloud.config.uri=http://config-server:8888
- Start all services, including Config Server, with Docker Compose:
docker-compose up -d
- Access
http://localhost:8888/user-service/default
to verify that theUserService
configuration is being retrieved from the Dockerized Config Server.
- Open the Git repository for
config-repo
and change a property inuser-service.properties
(e.g., change the server port or logging level). - Commit and push the changes to Git.
- In Postman, send a
POST
request tohttp://localhost:8080/api/users/actuator/refresh
to apply the updated configuration without restartingUserService
.
- Verify that the new configuration in
user-service.properties
has been applied. For example, check logs for the updated logging level if that was changed.
- Review what was covered:
- Set up a centralized Config Server using Spring Cloud Config.
- Connected microservices to retrieve configuration from the Config Server.
- Verified configuration changes without redeploying services.
-
Create a Profile-Specific Configuration:
- In the Git repository, create a profile-specific configuration file for
UserService
, such asuser-service-dev.properties
. - Add unique settings to this file and test retrieving the configuration with
http://localhost:8888/user-service/dev
.
- In the Git repository, create a profile-specific configuration file for
-
Configure Config Server to Use Multiple Repositories:
- Modify
application.properties
in Config Server to support multiple Git repositories:spring.cloud.config.server.git.repos.userRepo.uri=https://github.com/your-git-repo/user-config spring.cloud.config.server.git.repos.productRepo.uri=https://github.com/your-git-repo/product-config
- Verify that each service can retrieve configurations from its dedicated repository.
- Modify
-
Add Health Check Endpoint:
- Expose a health check endpoint in Config Server using Actuator.
- Access
http://localhost:8888/actuator/health
to verify the health status of the Config Server.