Skip to main content

Apache Kafka plugin for Spakky framework

Project description

Spakky Kafka

Spakky Framework를 위한 Apache Kafka 플러그인입니다.

설치

pip install spakky-kafka

또는 Spakky extra로 설치합니다:

pip install spakky[kafka]

설정

SPAKKY_KAFKA__ prefix를 가진 환경변수를 설정합니다:

export SPAKKY_KAFKA__GROUP_ID="my-consumer-group"
export SPAKKY_KAFKA__CLIENT_ID="my-app"
export SPAKKY_KAFKA__BOOTSTRAP_SERVERS="localhost:9092"
export SPAKKY_KAFKA__AUTO_OFFSET_RESET="earliest"  # earliest, latest, none

SASL 인증 (선택)

export SPAKKY_KAFKA__SECURITY_PROTOCOL="SASL_SSL"
export SPAKKY_KAFKA__SASL_MECHANISM="PLAIN"
export SPAKKY_KAFKA__SASL_USERNAME="username"
export SPAKKY_KAFKA__SASL_PASSWORD="password"

Topic 설정 (선택)

export SPAKKY_KAFKA__NUMBER_OF_PARTITIONS="3"
export SPAKKY_KAFKA__REPLICATION_FACTOR="1"

사용법

이벤트 발행

from spakky.core.common.mutability import immutable
from spakky.domain.models.event import AbstractIntegrationEvent
from spakky.event.event_publisher import IEventPublisher
from spakky.core.pod.annotations.pod import Pod

@immutable
class UserCreatedEvent(AbstractIntegrationEvent):
    user_id: int
    email: str

@Pod()
class UserService:
    def __init__(self, publisher: IEventPublisher) -> None:
        self.publisher = publisher

    def create_user(self, email: str) -> User:
        user = User(email=email)
        self.publisher.publish(UserCreatedEvent(user_id=user.id, email=email))
        return user

이벤트 수신

from spakky.event.stereotype.event_handler import EventHandler, on_event

@EventHandler()
class UserEventHandler:
    def __init__(self, notification_service: NotificationService) -> None:
        self.notification_service = notification_service

    @on_event(UserCreatedEvent)
    async def on_user_created(self, event: UserCreatedEvent) -> None:
        await self.notification_service.send_welcome_email(event.email)

비동기 변형

비동기 애플리케이션에서는 IAsyncEventPublisher를 사용합니다:

from spakky.event.event_publisher import IAsyncEventPublisher

@Pod()
class AsyncUserService:
    def __init__(self, publisher: IAsyncEventPublisher) -> None:
        self.publisher = publisher

    async def create_user(self, email: str) -> User:
        user = User(email=email)
        await self.publisher.publish(UserCreatedEvent(user_id=user.id, email=email))
        return user

분산 트레이싱

spakky-tracing은 필수 의존성으로 자동 설치됩니다. ITracePropagator가 컨테이너에 등록되어 있으면 이벤트 발행/소비 시 TraceContext가 자동으로 전파됩니다.

  • 발행 측: IEventTransport.send() 시 현재 TraceContext를 Kafka 메시지 헤더에 주입합니다
  • 소비 측: 수신 메시지에서 TraceContext를 추출하여 자식 스팬을 생성합니다
  • 헤더가 없으면 새로운 루트 트레이스를 시작합니다

주요 기능

  • 자동 topic 생성: 이벤트 타입 이름을 기준으로 topic 생성
  • 동기/비동기 지원: 동기 및 비동기 publisher/consumer 모두 지원
  • Background service 패턴: consumer polling을 background service로 실행
  • Pydantic 직렬화: 이벤트를 Pydantic으로 직렬화/역직렬화
  • Confluent Kafka client: 안정적인 confluent-kafka library 기반
  • 분산 트레이싱: 서비스 간 trace 전파를 위한 spakky-tracing 통합

구성 요소

컴포넌트 설명
KafkaEventTransport 동기 event transport(IEventTransport)
AsyncKafkaEventTransport 비동기 event transport(IAsyncEventTransport)
KafkaEventConsumer 동기 event consumer(background service)
AsyncKafkaEventConsumer 비동기 event consumer(background service)
KafkaConnectionConfig 환경변수 기반 설정

라이선스

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spakky_kafka-6.5.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spakky_kafka-6.5.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file spakky_kafka-6.5.0.tar.gz.

File metadata

  • Download URL: spakky_kafka-6.5.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spakky_kafka-6.5.0.tar.gz
Algorithm Hash digest
SHA256 9f28b8f80d608ce748d88bae71759938f9f8be51c3dffba223f6204133ee6840
MD5 159af71c39c7addc562b6184135deab9
BLAKE2b-256 d16b1d60df7d05dd9fa38146d0c3d4bbbd89ef01e217aa7c12fd09c4dc51f5f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_kafka-6.5.0.tar.gz:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spakky_kafka-6.5.0-py3-none-any.whl.

File metadata

  • Download URL: spakky_kafka-6.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for spakky_kafka-6.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60f10f2ca79cf11ecef64600a577906b57a97400c182d88b917a2e828b7f7755
MD5 1ff42c1fa0fdcf4c6434ea63d933d32a
BLAKE2b-256 dde5ac237deae056aad2cac8277dc2c4ccc5eca85df6b4da19151fc9ed19dc7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for spakky_kafka-6.5.0-py3-none-any.whl:

Publisher: release.yml on E5presso/spakky-framework

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page