# 이벤트 기반 아키텍처

## 이벤트 기반 아키텍처 (EDA)

> 이벤트를 발행하고 수신해서 처리하는 구조

* Event-Driven Architecture, EDA
* 시스템의 구성 요소간에 발생하는 이벤트 중심으로 설계
* 분산 시스템과 마이크로서비스 아키텍처에서 주로 사용

<figure><img src="https://1912740209-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3GVYdZvmqQyBVq29ebqk%2Fuploads%2F8Z5cjHpTMdqJhZMLh6uo%2F%E1%84%89%E1%85%B3%E1%84%8F%E1%85%B3%E1%84%85%E1%85%B5%E1%86%AB%E1%84%89%E1%85%A3%E1%86%BA%202024-10-06%20%E1%84%8B%E1%85%A9%E1%84%92%E1%85%AE%205.04.38.png?alt=media&#x26;token=744b20da-0880-4368-aaeb-1a23ca431dc9" alt=""><figcaption></figcaption></figure>

### 아키텍처 구성

* **이벤트 발행자**
  * 이벤트를 발생시키는 구성 요소 또는 서비스
* **이벤트 소비자**
  * 특정 이벤트에 대해 구독
  * 이벤트가 발생하면 이를 처리하는 구성 요소 또는 서비스

### 장점

* **느슨한 결합** - 생산자와 소비자 분리
* **확장성** - 쉽게 소비자를 추가 가능
* **실시간성** - 이벤트 수신 즉시 소비자가 처리
* **탄력성** - 필요한 리소스에 따라 쉽게 늘리거나 줄일 수 있다.
* **비동기성** - 수신자는 각자 작업을 수행

{% hint style="danger" %}
단점

* 중간에 비동기성으로 발행자와 소비자가 따로 있고 각각의 역할이 구분되어있기 때문에 데이터가 유실될 가능성이 존재
  {% endhint %}

### 코드 예시

#### 이벤트

```java
// 이벤트 클래스
class MyEvent {
  private String message;
  
  public MyEvent(String message) {
    this.message = message;
  }
  
  public String getMessage() {
    return message;
  }
}
```

#### 이벤트 발행자

```java
// 이벤트 발행자
class EventPublisher {
  private List<EventListener> listener = new ArrayList<>();
  
  // 이벤트를 발행하는 메서드
  public void publishEvent(MyEvent event) {
    for (EventListener listener : listeners) {
      listener.onEvent(event);
    }
  }
  
  // 이벤트 구독자 등록
  public void addEventListener(EventListener listener) {
    listeners.add(listener);
  }
}
```

#### 이벤트 구독자

```java
// 이벤트 구독자 인터페이스
interface EventListener {
  void onEvent(MyEvent event);
}

// 이벤트 구독자
class EventSubscriber implements EventListener {
  @Override
  public void onEvent(MyEvent event) {
    System.out.println("이벤트 수신: " + event.getMessage());
  }
}
```

#### 이벤트 발행과 수신

```java
public class EventExample {
  public static void main(String[] args) {
    // 이벤트 발행자 생성
    EventPublisher eventPublisher = new EventPublisher();
    
    // 이벤트 구독자 생성 및 등록
    EventSubscriber subscriber1 = new EventSubscriber();
    eventPublisher.addEventListener(subscriber1);
    
    EventSubscriber subscriber2 = new EventSubscriber();
    eventPublisher.addEventListener(subscriber2);
    
    // 이벤트 발행
    MyEvent event = new MyEvent("안녕하세요!");
    eventPublisher.publishEvent(event);
  }
}
```

#### Keywords

* 카프카
* 레비댄큐


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://taewoongs-organization.gitbook.io/jtwjs-dev-wiki/dev_note/system-design/architecture/undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
