0Pricing
Spring Boot 4 Microservices & REST APIs · Ders

Olay Odaklı Mikro Hizmet Entegrasyonu

Kafka kullanarak mikro hizmetler arasında eş zamansız iletişim modelleri tasarlayın ve uygulayın.

Olay Odaklı Mikro Hizmet Entegrasyonu, CoddyKit'te ücretsiz bir Spring Boot 4 Microservices & REST APIs dersidir. Bu, 3 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Spring Boot 4 Microservices & REST APIs öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Boot 4 Microservices & REST APIs kursu toplamda 3 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Event-Driven Microservices

Welcome to Event-Driven Microservice Integration! In this lesson, we'll learn how microservices can communicate asynchronously using events.

An event-driven architecture (EDA) is a software design pattern where services communicate by producing and consuming "events." These events are records of something that happened.

This approach helps create highly decoupled, scalable, and resilient systems.

Benefits of Asynchronous

Why choose event-driven communication over direct API calls (synchronous)?

  • Decoupling: Services don't need to know about each other's existence. They only care about events.
  • Resilience: If a consuming service is down, the events are stored and processed later, preventing cascading failures.
  • Scalability: Producers can publish events without waiting for consumers, and multiple consumers can process events in parallel.

Kafka as Event Bus

Apache Kafka acts as a central "event bus" or message broker in many event-driven architectures.

Producers send events to Kafka topics, and consumers read events from these topics. Kafka stores these events durably, ensuring no data loss.

This allows services to communicate without direct connections, simplifying their design and deployment.

An OrderCreated Event

An event is a lightweight message indicating that something significant has occurred. It typically includes the event type, a timestamp, and relevant data.

Let's define a simple OrderCreatedEvent that our OrderService might publish when a new order is placed.

public class OrderCreatedEvent {
  private String orderId;
  private String customerId;
  private double amount;
  private long timestamp;

  public OrderCreatedEvent(String orderId, String customerId, double amount) {
    this.orderId = orderId;
    this.customerId = customerId;
    this.amount = amount;
    this.timestamp = System.currentTimeMillis();
  }

  public String getOrderId() { return orderId; }
  public String getCustomerId() { return customerId; }
  public double getAmount() { return amount; }
  public long getTimestamp() { return timestamp; }

  @Override
  public String toString() {
    return "OrderCreatedEvent{" +
           "orderId='" + orderId + '\'' +
           ", customerId='" + customerId + '\'' +
           ", amount=" + amount +
           ", timestamp=" + timestamp +
           '}';
  }
}

Order Service as Producer

Our OrderService is responsible for creating new orders. Once an order is successfully created, it publishes an OrderCreatedEvent to a Kafka topic.

This event can then be consumed by other services, like an InventoryService, without the OrderService needing to know anything about them.

// Simulates a Kafka producer component
public class OrderProducer {
  public void sendOrderCreatedEvent(OrderCreatedEvent event) {
    // In a real Spring Boot app, this would use KafkaTemplate.send()
    System.out.println("Producer: Sending event to Kafka topic 'orders':");
    System.out.println("  " + event.toString());
  }
}

Try the Producer

Here's how you'd typically trigger the producer logic. Run this code to see the simulated event being "sent."

// Represents an event when an order is created
class OrderCreatedEvent {
  private String orderId;
  private String customerId;
  private double amount;
  private long timestamp;

  public OrderCreatedEvent(String orderId, String customerId, double amount) {
    this.orderId = orderId;
    this.customerId = customerId;
    this.amount = amount;
    this.timestamp = System.currentTimeMillis();
  }

  public String getOrderId() { return orderId; }
  public String getCustomerId() { return customerId; }
  public double getAmount() { return amount; }
  public long getTimestamp() { return timestamp; }

  @Override
  public String toString() {
    return "OrderCreatedEvent{" +
           "orderId='" + orderId + '\'' +
           ", customerId='" + customerId + '\'' +
           ", amount=" + amount +
           ", timestamp=" + timestamp +
           '}';
  }
}

// Simulates a Kafka producer component
class OrderProducer {
  public void sendOrderCreatedEvent(OrderCreatedEvent event) {
    System.out.println("Producer: Sending event to Kafka topic 'orders':");
    System.out.println("  " + event.toString());
  }
}

public class Main {
  public static void main(String[] args) {
    System.out.println("Order Service simulation started.");

    OrderProducer producer = new OrderProducer();

    // Simulate an order creation
    OrderCreatedEvent event = new OrderCreatedEvent("ORD-001", "CUST-123", 99.99);
    producer.sendOrderCreatedEvent(event);

    System.out.println("Order Service simulation finished.");
  }
}

Inventory Service as Consumer

Our InventoryService needs to know when new orders are placed so it can update stock levels. It listens for OrderCreatedEvents from the Kafka topic.

When an event arrives, the consumer processes it, perhaps by deducting items from inventory or initiating a fulfillment process.

// Simulates a Kafka consumer component
public class InventoryConsumer {
  public void listenOrderCreatedEvent(OrderCreatedEvent event) {
    // In a real Spring Boot app, this would be an @KafkaListener method
    System.out.println("Consumer: Received event from Kafka topic 'orders':");
    System.out.println("  " + event.toString());
    System.out.println("  Updating inventory for order " + event.getOrderId());
  }
}

Try the Consumer

This code simulates the InventoryConsumer listening for an event. In a real scenario, this would run continuously, processing incoming events.

// Represents an event when an order is created
class OrderCreatedEvent {
  private String orderId;
  private String customerId;
  private double amount;
  private long timestamp;

  public OrderCreatedEvent(String orderId, String customerId, double amount) {
    this.orderId = orderId;
    this.customerId = customerId;
    this.amount = amount;
    this.timestamp = System.currentTimeMillis();
  }

  public String getOrderId() { return orderId; }
  public String getCustomerId() { return customerId; }
  public double getAmount() { return amount; }
  public long getTimestamp() { return timestamp; }

  @Override
  public String toString() {
    return "OrderCreatedEvent{" +
           "orderId='" + orderId + '\'' +
           ", customerId='" + customerId + '\'' +
           ", amount=" + amount +
           ", timestamp=" + timestamp +
           '}';
  }
}

// Simulates a Kafka consumer component
class InventoryConsumer {
  public void listenOrderCreatedEvent(OrderCreatedEvent event) {
    System.out.println("Consumer: Received event from Kafka topic 'orders':");
    System.out.println("  " + event.toString());
    System.out.println("  Updating inventory for order " + event.getOrderId());
  }
}

public class Main {
  public static void main(String[] args) {
    System.out.println("Inventory Service simulation started.");

    InventoryConsumer consumer = new InventoryConsumer();

    // Simulate receiving an event (e.g., from Kafka)
    // This event would typically come from a Producer
    OrderCreatedEvent receivedEvent = new OrderCreatedEvent("ORD-001", "CUST-123", 99.99);
    consumer.listenOrderCreatedEvent(receivedEvent);

    System.out.println("Inventory Service simulation finished.");
  }
}

Key Considerations

When working with event-driven systems, two key concepts are important:

  • Eventual Consistency: Data across different services might not be instantly consistent. It will become consistent "eventually."
  • Idempotency: Consumers should be designed to handle duplicate events gracefully. Processing the same event multiple times should not change the outcome.

These are crucial for building robust asynchronous microservices.

Integration Check

Which of the following are key benefits of using an event-driven architecture for microservice integration compared to direct synchronous API calls?

Event-Driven Recap

Great job! In this lesson, you've learned about event-driven microservice integration:

  • The benefits of asynchronous communication like decoupling, resilience, and scalability.
  • How Kafka serves as an event bus.
  • The roles of producer and consumer microservices in publishing and processing events.
  • Important concepts like eventual consistency and idempotency.

You're now ready to design more robust and scalable microservice interactions!

Sıkça Sorulan Sorular

“Olay Odaklı Mikro Hizmet Entegrasyonu” dersi ücretsiz mi?

Evet — “Olay Odaklı Mikro Hizmet Entegrasyonu” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Spring Boot 4 Microservices & REST APIs kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Boot 4 Microservices & REST APIs kursu toplamda 3 dersten oluşur.

“Olay Odaklı Mikro Hizmet Entegrasyonu” dersinde ne öğreneceğim?

Kafka kullanarak mikro hizmetler arasında eş zamansız iletişim modelleri tasarlayın ve uygulayın. Spring Boot 4 Microservices & REST APIs ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Spring Boot 4 Microservices & REST APIs öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Spring Boot 4 Microservices & REST APIs, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 3 dersinin 3. dersidir.

“Olay Odaklı Mikro Hizmet Entegrasyonu” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Spring Boot 4 Microservices & REST APIs dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Spring Boot 4 Microservices & REST APIs dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Kafka Üreticilerine Giriş
  2. Kafka Tüketicileri Oluşturma
  3. Olay Odaklı Mikro Hizmet Entegrasyonu
← Spring Boot 4 Microservices & REST APIs Sayfasına Dön