Set up a Eureka server to enable service discovery, allowing UserService
, ProductService
, and OrderService
to register dynamically and communicate without hardcoded URLs.
- Inside the MicroservicesProject workspace, create a new folder named
EurekaServer
.
- 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
- Group Id:
- Save the project in the
EurekaServer
folder.
-
In the
EurekaServer
project, openpom.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>
-
Open
EurekaServerApplication.java
inEurekaServer/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); } }
-
In
EurekaServer/src/main/resources
, openapplication.properties
and add the following configuration:server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
-
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.
-
Open the
pom.xml
file forUserService
,ProductService
, andOrderService
. -
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.
- Open the
application.properties
file for each microservice and configure it to register with Eureka.
```properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=user-service
```
```properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=product-service
```
```properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.application.name=order-service
```
```java
@FeignClient(name = "user-service")
public interface UserClient {
@GetMapping("/{id}")
User getUserById(@PathVariable("id") Long id);
}
```
```java
@FeignClient(name = "product-service")
public interface ProductClient {
@GetMapping("/{id}")
Product getProductById(@PathVariable("id") Long id);
}
```
-
In the
EurekaServer
directory, create aDockerfile
: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 .
-
Open
docker-compose.yml
in the MicroservicesProject directory and add the Eureka Server configuration:eureka-server: build: ./EurekaServer ports: - "8761:8761"
-
In each microservice’s
application.properties
, update theeureka.client.service-url.defaultZone
property:eureka.client.service-url.defaultZone=http://eureka-server:8761/eureka
-
Start all services, including the Eureka Server, with Docker Compose:
docker-compose up -d
- Open the Eureka dashboard at
http://localhost:8761
and verify thatUserService
,ProductService
, andOrderService
appear as registered instances.
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.
-
Customize Service Registration Names:
- Change the service names in each microservice’s
application.properties
(e.g.,user-service
touser-service-app
) and verify that they appear with new names in the Eureka dashboard.
- Change the service names in each microservice’s
-
View Service Health Checks:
- Stop one of the microservices and observe how Eureka handles the failure on its dashboard.
-
Dockerize with Replicas:
- Modify
docker-compose.yml
to run multiple replicas ofUserService
andProductService
. Verify that Eureka registers all instances and test load distribution by calling theOrderService
endpoint multiple times in Postman.
- Modify