queue

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 8 Imported by: 0

README

kratos-kit queue transport

transport/queue 将现有 github.com/liujitcn/kratos-kit/queue 适配为 Kratos transport.Server,不修改原 queue 模块。

创建

默认使用本地内存队列:

queueSrv, err := queueTransport.NewServer(queueTransport.WithMemory(128))

使用 Redis:

queueSrv, err := queueTransport.NewServer(queueTransport.WithRedis(redisConfig, queueConfig))

注册处理器后,将 queueSrv 传给 kratos.Server(queueSrv),应用会统一调用 StartStop。也可以使用 WithQueue 注入一个已有的 queue.Queue 实例。

const orderStream queueTransport.Stream = "orders"

queueSrv.Register(orderStream, func(message queueData.Message) error {
	return handleOrder(message)
})

示例中的导入别名:

queueTransport "github.com/liujitcn/kratos-kit/transport/queue"
queueData "github.com/liujitcn/kratos-kit/queue/data"

安装:

go get github.com/liujitcn/kratos-kit/transport/queue@latest

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server 将现有 queue.Queue 适配为 Kratos transport.Server。

func NewServer

func NewServer(opts ...ServerOption) (*Server, error)

NewServer 创建队列 transport,默认使用本地内存队列。

func (*Server) Append

func (s *Server) Append(stream Stream, message data.Message) error

Append 向指定流追加消息。

func (*Server) Queue

func (s *Server) Queue() basequeue.Queue

Queue 返回底层队列实例。

func (*Server) Register

func (s *Server) Register(stream Stream, fn data.ConsumerFunc)

Register 注册指定流的消费处理函数。

func (*Server) Start

func (s *Server) Start(_ context.Context) error

Start 启动队列消费,并阻塞到队列停止。

func (*Server) Stop

func (s *Server) Stop(_ context.Context) error

Stop 停止队列消费,并执行构造阶段登记的清理函数。

type ServerOption

type ServerOption func(*options)

ServerOption 配置队列 transport。

func WithMemory

func WithMemory(poolSize int64) ServerOption

WithMemory 选择本地内存队列,并设置内存队列池大小。

func WithQueue

func WithQueue(instance queue.Queue) ServerOption

WithQueue 注入已有队列实例,便于复用队列或进行测试。

func WithRedis

func WithRedis(redisConf *configv1.Data_Redis, queueConf *configv1.Data_Queue) ServerOption

WithRedis 选择 Redis 队列,并设置 Redis 与队列配置。

type Stream

type Stream string

Stream 标识一个队列流名称。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL