This is documentation on a preview feature.
绑定
创建一个绑定组件只需要几个基本步骤。
导入绑定包
创建文件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()
方法应该设置一个长期存在的机制来获取消息,但立即返回nil
(或错误,如果无法设置该机制)。 当被取消时(例如,通过ctx.Done()或ctx.Err() != nil
),机制应该结束。 当消息从组件的底层存储中读取时,它们通过handler
回调函数传递给Dapr运行时,直到应用程序(由Dapr运行时提供服务)确认处理消息后才返回。
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()
}
下一步
- Dapr 可插拔组件 Go SDK 的高级技巧
- 详细了解如何实现:
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.