본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

바인딩

원문 보기 위키 갱신

바인딩 (Bindings)

이 문서는 프리뷰 기능에 대한 문서예요.

바인딩 컴포넌트를 만드는 데는 몇 가지 기본 단계만 거치면 돼요.

출처: Dapr 문서

본문

바인딩 패키지 가져오기

components/inputbinding.go 파일을 만들고 상태 저장소 관련 패키지에 대한 import 문을 추가해요.

package components

import (
"context"
"github.com/dapr/components-contrib/bindings"
)

입력 바인딩: InputBinding 인터페이스 구현하기

InputBinding 인터페이스를 구현하는 타입을 만들어요.

type MyInputBindingComponent struct {
}

func (component *MyInputBindingComponent) Init(meta bindings.Metadata) error {
// Called to initialize the component with its configured metadata...
}

func (component *MyInputBindingComponent) Read(ctx context.Context, handler bindings.Handler) error {
// Until canceled, check the underlying store for messages and deliver them to the Dapr runtime...
}

Read() 메서드에 대한 호출은 메시지를 가져오는 수명이 긴(long-lived) 메커니즘을 설정하되 즉시 nil(또는 그 메커니즘을 설정할 수 없다면 오류)을 반환할 것으로 기대돼요. 그 메커니즘은 취소될 때(예를 들어 ctx.Done() 또는 ctx.Err() != nil을 통해) 종료되어야 해요. 컴포넌트의 기본 저장소에서 메시지를 읽으면 handler 콜백을 통해 Dapr 런타임으로 전달되는데, 이 콜백은 (Dapr 런타임이 제공하는) 애플리케이션이 메시지 처리를 확인(acknowledge)할 때까지 반환되지 않아요.

func (b *MyInputBindingComponent) Read(ctx context.Context, handler bindings.Handler) error {
go func() {
for {
err := ctx.Err()

if err != nil {
return
}

messages := // Poll for messages...

for _, message := range messages {
handler(ctx, &bindings.ReadResponse{
// Set the message content...
})
}

select {
case <-ctx.Done():
case <-time.After(5 * time.Second):
}
}
}()

return nil
}

출력 바인딩: OutputBinding 인터페이스 구현하기

OutputBinding 인터페이스를 구현하는 타입을 만들어요.

type MyOutputBindingComponent struct {
}

func (component *MyOutputBindingComponent) Init(meta bindings.Metadata) error {
// Called to initialize the component with its configured metadata...
}

func (component *MyOutputBindingComponent) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
// Called to invoke a specific operation...
}

func (component *MyOutputBindingComponent) Operations() []bindings.OperationKind {
// Called to list the operations that can be invoked.
}

입력 및 출력 바인딩 컴포넌트

컴포넌트는 입력 바인딩이자 출력 바인딩이 될 수 있어요. 두 인터페이스를 모두 구현하고 그 컴포넌트를 두 바인딩 유형으로 등록하면 돼요.

바인딩 컴포넌트 등록하기

메인 애플리케이션 파일(예: main.go)에서 애플리케이션에 바인딩 컴포넌트를 등록해요.

package main

import (
"example/components"
dapr "github.com/dapr-sandbox/components-go-sdk"
"github.com/dapr-sandbox/components-go-sdk/bindings/v1"
)

func main() {
// Register an import binding...
dapr.Register("my-inputbinding", dapr.WithInputBinding(func() bindings.InputBinding {
return &components.MyInputBindingComponent{}
}))

// Register an output binding...
dapr.Register("my-outputbinding", dapr.WithOutputBinding(func() bindings.OutputBinding {
return &components.MyOutputBindingComponent{}
}))

dapr.MustRun()
}

다음 단계

  • 플러그형 컴포넌트 Go SDK의 고급 기법
  • 다음 구현 방법 더 알아보기:상태
  • Pub/sub

더 알아보기 (Learn more)