0%
Communication Between Microservices

Communication Between Microservices

The different communication strategies

10-15 min

Communication Between Microservices

In this chapter, we will explore the different communication strategies between microservices. We will see how to implement and use each approach with Spring Cloud.

1. REST Communication with OpenFeign

Configuration

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Implementation

@FeignClient(name = "order-service", url = "${order.service.url}")
public interface OrderClient {
    @GetMapping("/api/orders/{id}")
    OrderDTO getOrder(@PathVariable("id") Long id);
    
    @PostMapping("/api/orders")
    OrderDTO createOrder(@RequestBody CreateOrderRequest request);
}

Usage

@Service
@RequiredArgsConstructor
public class OrderService {
    private final OrderClient orderClient;
    
    public OrderDTO getOrderDetails(Long orderId) {
        return orderClient.getOrder(orderId);
    }
}

2. Messaging with RabbitMQ

Configuration

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!