Skip to content

Latest commit

 

History

History
195 lines (160 loc) · 7.59 KB

File metadata and controls

195 lines (160 loc) · 7.59 KB

Lab 9: Centralized Configuration with Spring Cloud Config

Objective

Set up a Spring Cloud Config Server to manage configurations for UserService, ProductService, and OrderService. Centralizing configuration will allow updates without redeploying each service.


Steps

1. Create a New Folder for the Config Server

  • Inside the MicroservicesProject workspace, create a new folder named ConfigServer.

2. Generate Config Server with Spring Initializr

  • 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
  • Save the project in the ConfigServer folder.

2.1 Add dependencies

        <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>

3. Enable Config Server

  • Open ConfigServerApplication.java in ConfigServer/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);
        }
    }

4. Configure Config Server Properties

  • In ConfigServer/src/main/resources, open application.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.

5. Set Up the Git Configuration 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

6. Commit and Push Configuration Files

  • Commit the configuration files and push them to the Git repository.

7. Configure Each Microservice to Use Config Server

  • For each microservice (UserService, ProductService, OrderService), open application.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

8. Run Config Server in VS Code

  • Run the app mvn spring-boot:run
  • Ensure it is running on http://localhost:8888.

9. Verify Configurations from Config Server

  • 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.

10. Run Eureka Server and Microservices

  • Ensure EurekaServer, UserService, ProductService, and OrderService are running in VS Code.
  • Verify that each service is successfully registered in Eureka.

11. Allow Configurations changes Without Restarting (Using Actuator)

  • Open UserService’s application.properties and add:

        management.endpoints.web.exposure.include=*
        management.endpoints.web.base-path=/actuator
  • Open ApiGateway’s application.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}
    

12. Containerize Config Server with Docker

  • In the ConfigServer directory, create a Dockerfile:
    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 .

13. Update Docker Compose for 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

14. Update Microservices to Use Config Server in Docker Compose

  • Modify each service’s application.properties to connect to the Dockerized Config Server:
    spring.cloud.config.uri=http://config-server:8888

15. Run Docker Compose

  • Start all services, including Config Server, with Docker Compose:
    docker-compose up -d

16. Verify Configurations in Docker

  • Access http://localhost:8888/user-service/default to verify that the UserService configuration is being retrieved from the Dockerized Config Server.

17. Modify a Configuration in Git Repository

  • Open the Git repository for config-repo and change a property in user-service.properties (e.g., change the server port or logging level).
  • Commit and push the changes to Git.

18. Refresh Configurations Without Restarting

  • In Postman, send a POST request to http://localhost:8080/api/users/actuator/refresh to apply the updated configuration without restarting UserService.

19. Verify Configuration Update in UserService

  • 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.

20. Summary of Lab

  • 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.

Extra Exercise (20 minutes)

  1. Create a Profile-Specific Configuration:

    • In the Git repository, create a profile-specific configuration file for UserService, such as user-service-dev.properties.
    • Add unique settings to this file and test retrieving the configuration with http://localhost:8888/user-service/dev.
  2. 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.
  3. 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.