Microservices Communication
Methods of Microservices communication with each other primarily through two methods:
Table of Contents
1. Synchronous Communication:
- Uses protocols like HTTP/HTTPS or gRPC for direct communication.
- Typically involves REST APIs where one service makes a direct call to another service and waits for a response.
2. Asynchronous Communication:
- Uses message brokers like RabbitMQ, Apache Kafka, or Amazon SQS.
- Services communicate by publishing and subscribing to events, allowing for more decoupled interactions.
Synchronous Communication Example
Java Example:
Using Spring Boot, REST, and Feign Client for synchronous communication between services.
Service A (User Service)
```java
// UserController.java
@SpringBootApplication
@RestController
@RequestMapping("/users")
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
@GetMapping("/{id}")
public User getUserById(@PathVariable String id) {
return new User(id, "John Doe", "john.doe@example.com");
}
}
```
Service B (Order Service)
```java
// OrderController.java
@SpringBootApplication
@RestController
@RequestMapping("/orders")
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
@Autowired
private UserClient userClient;
@GetMapping("/{orderId}")
public Order getOrderById(@PathVariable String orderId) {
User user = userClient.getUserById("123");
return new Order(orderId, "Product 1", user);
}
}
// UserClient.java
@FeignClient(name = "user-service")
public interface UserClient {
@GetMapping("/users/{id}")
User getUserById(@PathVariable String id);
}
```
Configuration (for Feign Client in Order Service)
```yaml
feign:
hystrix:
enabled: true
ribbon:
eureka:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
```
Asynchronous Communication Example
Java Example:
Using Spring Boot and RabbitMQ for asynchronous communication between services.
Service A (User Service)
```java
// UserService.java
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
@Autowired
private RabbitTemplate rabbitTemplate;
@PostMapping("/users")
public void createUser(@RequestBody User user) {
// Logic to create user
rabbitTemplate.convertAndSend("userExchange", "user.created", user);
}
}
```
Service B (Order Service)
```java
// OrderService.java
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
@RabbitListener(queues = "userQueue")
public void handleUserCreated(User user) {
// Logic to handle user creation event
}
}
```
Configuration (for RabbitMQ)
```yaml
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
```
RabbitMQ Config
```java
@Configuration
public class RabbitConfig {
@Bean
public TopicExchange userExchange() {
return new TopicExchange("userExchange");
}
@Bean
public Queue userQueue() {
return new Queue("userQueue");
}
@Bean
public Binding bindingUserQueue(TopicExchange userExchange, Queue userQueue) {
return BindingBuilder.bind(userQueue).to(userExchange).with("user.created");
}
}
```