Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
<module>service-locator</module>
<module>service-stub</module>
<module>service-to-worker</module>
<module>server-side-service-discovery</module>
<module>session-facade</module>
<module>sharding</module>
<module>single-table-inheritance</module>
Expand Down
172 changes: 172 additions & 0 deletions server-side-service-discovery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Server-Side Service Discovery Pattern

## Intent

The Server-Side Service Discovery pattern is a microservice architecture pattern that provides a centralized mechanism for services to register themselves and for consumers to discover available services dynamically. This pattern enhances system scalability and flexibility by decoupling service consumers from the physical locations of service providers.

## Explanation

Real-world example

> Consider a large e-commerce platform with multiple microservices like Product Service, Order Service, Payment Service, and Inventory Service. Instead of hardcoding the network locations of each service, they register themselves with a central Service Registry (like Netflix Eureka). When the Order Service needs to check product availability, it queries the Service Registry to discover available instances of the Product Service. The registry returns healthy instances, and a load balancer distributes the request among them. This approach allows services to scale up/down dynamically, fail gracefully, and be discovered automatically without manual configuration.

In plain words

> Server-Side Service Discovery provides a centralized registry where services register themselves and consumers can discover and communicate with available services dynamically.

Wikipedia says

> Service discovery is the automatic detection of devices and services offered by these devices on a computer network. In the context of microservices, service discovery refers to the mechanism by which services find and communicate with each other.

## Programmatic Example

This implementation demonstrates the Server-Side Service Discovery pattern using Spring Cloud Netflix Eureka. The example includes:

### 1. Service Registry (Eureka Server)

```java
@SpringBootApplication
@EnableEurekaServer
public class ServiceRegistryApp {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistryApp.class, args);
}
}
```

### 2. Service Providers (Product Service & Order Service)

```java
@SpringBootApplication
@EnableEurekaClient
public class ProductServiceApp {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApp.class, args);
}
}
```

### 3. Service Consumer with Load Balancing

```java
@Configuration
public class ServiceConsumerConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

@Service
public class ServiceDiscoveryService {
private final RestTemplate restTemplate;
private final EurekaClient eurekaClient;

public String callService(String serviceName, String endpoint) {
try {
String serviceUrl = "http://" + serviceName + endpoint;
return restTemplate.getForObject(serviceUrl, String.class);
} catch (Exception e) {
return "Error calling service: " + e.getMessage();
}
}
}
```

## Key Components

### Service Registry
- **Purpose**: Central repository for service instance information
- **Implementation**: Netflix Eureka Server
- **Features**: Service registration, health monitoring, instance management

### Service Provider
- **Purpose**: Services that register themselves with the registry
- **Examples**: Product Service, Order Service
- **Features**: Auto-registration, health checks, graceful shutdown

### Service Consumer
- **Purpose**: Applications that discover and consume services
- **Features**: Service discovery, load balancing, fault tolerance
- **Implementation**: Uses Eureka Client and Spring Cloud LoadBalancer

### Load Balancer
- **Purpose**: Distributes requests among available service instances
- **Implementation**: Spring Cloud LoadBalancer
- **Strategies**: Round-robin, random, weighted

## Running the Example

### Prerequisites
- Java 21+
- Maven 3.8+

### Step 1: Start the Service Registry
```bash
cd service-registry
mvn spring-boot:run
```
The Eureka dashboard will be available at: http://localhost:8761

### Step 2: Start Service Providers
```bash
# Terminal 1 - Product Service
cd product-service
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081 --spring.application.name=product-service"

# Terminal 2 - Order Service
cd order-service
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8082 --spring.application.name=order-service"
```

### Step 3: Start Service Consumer
```bash
cd service-consumer
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8080 --spring.application.name=service-consumer"
```

### Step 4: Test the Pattern
```bash
# Discover all services
curl http://localhost:8080/api/services

# Get products through service discovery
curl http://localhost:8080/api/products

# Get orders through service discovery
curl http://localhost:8080/api/orders

# Check service health
curl http://localhost:8080/api/services/product-service/health
```

## Pattern Benefits

1. **Dynamic Service Discovery**: Services can be discovered at runtime without hardcoded configurations
2. **Load Balancing**: Automatic distribution of requests across available instances
3. **Fault Tolerance**: Failed instances are automatically removed from the registry
4. **Scalability**: New service instances are automatically discovered and included
5. **Health Monitoring**: Only healthy services participate in request handling
6. **Decoupling**: Services are decoupled from physical network locations

## Pattern Drawbacks

1. **Single Point of Failure**: Service registry becomes critical infrastructure
2. **Network Overhead**: Additional network calls for service discovery
3. **Complexity**: Adds operational complexity to the system
4. **Consistency**: Potential delays in service registry updates
5. **Network Partitions**: Service registry unavailability affects all services

## Related Patterns

- **Client-Side Service Discovery**: Service consumers are responsible for discovering services
- **Circuit Breaker**: Provides fault tolerance when calling discovered services
- **API Gateway**: Often combined with service discovery for external access
- **Health Check**: Essential for maintaining accurate service registry information

## Credits

- [Microservices Patterns by Chris Richardson](https://microservices.io/patterns/service-registry.html)
- [Spring Cloud Netflix Documentation](https://spring.io/projects/spring-cloud-netflix)
- [Building Microservices by Sam Newman](https://samnewman.io/books/building_microservices/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Order Service Configuration for Server-Side Service Discovery

# Server Configuration
server.port=8082
spring.application.name=order-service

# Eureka Client Configuration
eureka.client.service-url.default-zone=http://localhost:8761/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.prefer-ip-address=true
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=30

# Health Check Configuration
management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=always

# Logging Configuration
logging.level.com.netflix.eureka=DEBUG
logging.level.com.netflix.discovery=DEBUG
95 changes: 95 additions & 0 deletions server-side-service-discovery/order-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).

The MIT License
Copyright © 2014-2022 Ilkka Seppälä

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>server-side-service-discovery</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.26.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order-service</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.4.4</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<mainClass>com.iluwatar.orderservice.OrderServiceApp</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.orderservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Order Service Application that registers itself with the Service Registry.
* This demonstrates another Service Provider component of the Server-Side Service Discovery pattern.
*/
@SpringBootApplication
public class OrderServiceApp {

/**
* Main method to start the Order Service application.
*
* @param args command line arguments
*/
public static void main(String[] args) {
SpringApplication.run(OrderServiceApp.class, args);
}
}
Loading
Loading