Skip to content

Latest commit

 

History

History
203 lines (153 loc) · 6.27 KB

File metadata and controls

203 lines (153 loc) · 6.27 KB

Lab 6: Service Discovery with Eureka

Objective

Set up a Eureka server to enable service discovery, allowing UserService, ProductService, and OrderService to register dynamically and communicate without hardcoded URLs.


Steps

1. Create a New Folder for Eureka Server

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

2. Generate Eureka 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: eureka-server
    • Name: EurekaServer
  • Save the project in the EurekaServer folder.

3. Add Dependencies to pom.xml for Eureka Server

  • In the EurekaServer project, open pom.xml and add the following dependencies:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2023.0.3</version> <!-- Adjust version as needed -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <!-- Eureka Server Dependency -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

4. Enable Eureka Server

  • Open EurekaServerApplication.java in EurekaServer/src/main/java/com/microservices/eurekaserver.

  • Add the @EnableEurekaServer annotation:

    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }

5. Configure Eureka Server Properties

  • In EurekaServer/src/main/resources, open application.properties and add the following configuration:

    server.port=8761
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false

6. Run Eureka Server

  • Run the Eureka Server using:

    mvn spring-boot:run
  • Visit http://localhost:8761 to check that Eureka is running. You should see the Eureka dashboard.

7. Add Eureka Client Dependency to Each Microservice

  • Open the pom.xml file for UserService, ProductService, and OrderService.

  • Add the Eureka Client dependency:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  • Save each pom.xml file and refresh dependencies.

8. Configure Eureka Client Properties for Each Microservice

  • Open the application.properties file for each microservice and configure it to register with Eureka.

UserService application.properties

```properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=user-service
```

ProductService application.properties

```properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=product-service
```

OrderService application.properties

```properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=order-service
```

9. Update Feign Clients in OrderService to Use Service Names

UserClient.java

```java
@FeignClient(name = "user-service")
public interface UserClient {
    @GetMapping("/{id}")
    User getUserById(@PathVariable("id") Long id);
}
```

ProductClient.java

```java
@FeignClient(name = "product-service")
public interface ProductClient {
    @GetMapping("/{id}")
    Product getProductById(@PathVariable("id") Long id);
}
```

10. Containerize Eureka Server with Docker

  • In the EurekaServer directory, create a Dockerfile:

    FROM openjdk:17
    EXPOSE 8761
    ADD target/eureka-server-0.0.1-SNAPSHOT.jar eureka-server.jar
    ENTRYPOINT ["java", "-jar", "eureka-server.jar"]
  • Build the Docker image for Eureka Server:

    docker build -t eureka-server .

11. Update docker-compose.yml to Include Eureka Server

  • Open docker-compose.yml in the MicroservicesProject directory and add the Eureka Server configuration:

    eureka-server:
      build: ./EurekaServer
      ports:
        - "8761:8761"

12. Update Microservices to Use Eureka from Docker Compose

  • In each microservice’s application.properties, update the eureka.client.service-url.defaultZone property:

    eureka.client.service-url.defaultZone=http://eureka-server:8761/eureka

13. Run Docker Compose

  • Start all services, including the Eureka Server, with Docker Compose:

    docker-compose up -d

14. Verify Eureka Registration in Docker

  • Open the Eureka dashboard at http://localhost:8761 and verify that UserService, ProductService, and OrderService appear as registered instances.

Summary of Lab

In this lab, you:

  • Set up a Eureka server for dynamic service discovery.
  • Configured microservices to register with Eureka.
  • Enabled inter-service communication without hardcoded URLs.

Extra Exercise (20 Minutes)

  1. Customize Service Registration Names:

    • Change the service names in each microservice’s application.properties (e.g., user-service to user-service-app) and verify that they appear with new names in the Eureka dashboard.
  2. View Service Health Checks:

    • Stop one of the microservices and observe how Eureka handles the failure on its dashboard.
  3. Dockerize with Replicas:

    • Modify docker-compose.yml to run multiple replicas of UserService and ProductService. Verify that Eureka registers all instances and test load distribution by calling the OrderService endpoint multiple times in Postman.