Documentation
¶
Index ¶
- Constants
- type Mqtt
- func (m *Mqtt) Clone() internal.Configurable
- func (m *Mqtt) Get() interface{}
- func (m *Mqtt) Set(data interface{})
- func (m *Mqtt) Validate() error
- func (m *Mqtt) WithAutoReconnect(autoReconnect bool) *Mqtt
- func (m *Mqtt) WithCleanSession(cleanSession bool) *Mqtt
- func (m *Mqtt) WithClientID(clientID string) *Mqtt
- func (m *Mqtt) WithConnectTimeout(connectTimeout int) *Mqtt
- func (m *Mqtt) WithEndpoint(endpoint string) *Mqtt
- func (m *Mqtt) WithKeepAlive(keepAlive int) *Mqtt
- func (m *Mqtt) WithMaxReconnectInterval(maxReconnectInterval int) *Mqtt
- func (m *Mqtt) WithModuleName(moduleName string) *Mqtt
- func (m *Mqtt) WithPassword(password string) *Mqtt
- func (m *Mqtt) WithPingTimeout(pingTimeout int) *Mqtt
- func (m *Mqtt) WithProtocolVersion(protocolVersion uint) *Mqtt
- func (m *Mqtt) WithUsername(username string) *Mqtt
- func (m *Mqtt) WithWillTopic(willTopic string) *Mqtt
- func (m *Mqtt) WithWriteTimeout(writeTimeout int) *Mqtt
- type Nats
- func (n *Nats) Clone() internal.Configurable
- func (n *Nats) Get() interface{}
- func (n *Nats) IsEnabled() bool
- func (n *Nats) Set(data interface{})
- func (n *Nats) Validate() error
- func (n *Nats) WithConnectTimeout(seconds int) *Nats
- func (n *Nats) WithDrainTimeout(seconds int) *Nats
- func (n *Nats) WithDuplicatesWindow(seconds int64) *Nats
- func (n *Nats) WithEnabled(enabled bool) *Nats
- func (n *Nats) WithFlushTimeout(seconds int) *Nats
- func (n *Nats) WithJetStream(enabled bool) *Nats
- func (n *Nats) WithMaxAge(seconds int64) *Nats
- func (n *Nats) WithMaxBytes(bytes int64) *Nats
- func (n *Nats) WithMaxPingsOut(max int) *Nats
- func (n *Nats) WithMaxReconnects(max int) *Nats
- func (n *Nats) WithModuleName(moduleName string) *Nats
- func (n *Nats) WithName(name string) *Nats
- func (n *Nats) WithPassword(password string) *Nats
- func (n *Nats) WithPingInterval(seconds int) *Nats
- func (n *Nats) WithReconnectBufSize(bytes int64) *Nats
- func (n *Nats) WithReconnectJitter(seconds int) *Nats
- func (n *Nats) WithReconnectWait(seconds int) *Nats
- func (n *Nats) WithReplicas(replicas int) *Nats
- func (n *Nats) WithRetention(retention string) *Nats
- func (n *Nats) WithStorage(storage string) *Nats
- func (n *Nats) WithToken(token string) *Nats
- func (n *Nats) WithURL(url string) *Nats
- func (n *Nats) WithUsername(username string) *Nats
- func (n *Nats) WithWorkerPoolSize(size int) *Nats
- func (n *Nats) WithWorkerQueueSize(size int) *Nats
Constants ¶
const ( NatsStorageFile = "file" // 文件存储(持久化,生产默认) NatsStorageMemory = "memory" // 内存存储(易失,低延迟场景) )
JetStream 存储后端(对应官方 nats.StorageType)
const ( NatsRetentionLimits = "limits" // 基于限制保留(MaxAge/MaxBytes 约束) NatsRetentionInterest = "interest" // 基于兴趣保留(全部消费者确认后删除) NatsRetentionWorkQueue = "workqueue" // 工作队列(单个消费者确认即删除) )
JetStream 保留策略(对应官方 nats.RetentionPolicy)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Mqtt ¶
type Mqtt struct {
Endpoint string `mapstructure:"endpoint" yaml:"endpoint" json:"endpoint" validate:"required,url"` // Mqtt 代理服务器端点地址
ClientID string `mapstructure:"client-id" yaml:"client-id" json:"clientId"` // 客户端标识符
ProtocolVersion uint `mapstructure:"protocol-version" yaml:"protocol-version" json:"protocolVersion" validate:"required"` // Mqtt 协议版本号,4 是 3.1.1,3 是 3.1
KeepAlive int `mapstructure:"keep-alive" yaml:"keep-alive" json:"keepAlive" validate:"required,min=1"` // 保活时间间隔,最小值为 1 秒
MaxReconnectInterval int `mapstructure:"max-reconnect-interval" yaml:"max-reconnect-interval" json:"maxReconnectInterval" validate:"min=1"` // 最大连接间隔时间,单位:秒,最小值为 1 秒
PingTimeout int `mapstructure:"ping-timeout" yaml:"ping-timeout" json:"pingTimeout" validate:"min=1"` // ping 超时时间,单位:秒,最小值为 1 秒
WriteTimeout int `mapstructure:"write-timeout" yaml:"write-timeout" json:"writeTimeout" validate:"min=1"` // 写超时时间,单位:秒,最小值为 1 秒
ConnectTimeout int `mapstructure:"connect-timeout" yaml:"connect-timeout" json:"connectTimeout" validate:"min=1"` // 连接超时时间,单位:秒,最小值为 1 秒
Username string `mapstructure:"username" yaml:"username" json:"username"` // 连接到代理服务器的用户名
Password string `mapstructure:"password" yaml:"password" json:"password"` // 密码
CleanSession bool `mapstructure:"clean-session" yaml:"clean-session" json:"cleanSession"` // 设置客户端掉线服务端是否清除 session
AutoReconnect bool `mapstructure:"auto-reconnect" yaml:"auto-reconnect" json:"autoReconnect"` // 断开后是否重新连接
WillTopic string `mapstructure:"will-topic" yaml:"will-topic" json:"willTopic"` // 遗言发送的 topic
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名称
}
Mqtt 结构体用于配置 MQTT 相关参数
func (*Mqtt) WithAutoReconnect ¶
WithAutoReconnect 设置是否自动重连
func (*Mqtt) WithCleanSession ¶
WithCleanSession 设置是否清除会话
func (*Mqtt) WithClientID ¶
WithClientID 设置客户端标识符
func (*Mqtt) WithConnectTimeout ¶
WithConnectTimeout 设置连接超时时间
func (*Mqtt) WithEndpoint ¶
WithEndpoint 设置MQTT代理服务器端点地址
func (*Mqtt) WithKeepAlive ¶
WithKeepAlive 设置保活时间间隔
func (*Mqtt) WithMaxReconnectInterval ¶
WithMaxReconnectInterval 设置最大重连间隔
func (*Mqtt) WithModuleName ¶
WithModuleName 设置模块名称
func (*Mqtt) WithPingTimeout ¶
WithPingTimeout 设置ping超时时间
func (*Mqtt) WithProtocolVersion ¶
WithProtocolVersion 设置MQTT协议版本
func (*Mqtt) WithWillTopic ¶
WithWillTopic 设置遗言主题
func (*Mqtt) WithWriteTimeout ¶
WithWriteTimeout 设置写超时时间
type Nats ¶
type Nats struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled" json:"enabled"` // 是否启用
URL string `mapstructure:"url" yaml:"url" json:"url" validate:"required"` // NATS 服务器地址,如 nats://127.0.0.1:4222
Name string `mapstructure:"name" yaml:"name" json:"name"` // 客户端名称,用于服务端识别
Username string `mapstructure:"username" yaml:"username" json:"username"` // 用户名(可选)
Password string `mapstructure:"password" yaml:"password" json:"password"` // 密码(可选)
Token string `mapstructure:"token" yaml:"token" json:"token"` // Token 鉴权(可选)
// === 连接级(对齐官方 nats.Options,0 值走官方库默认) ===
ConnectTimeout int `mapstructure:"connect-timeout" yaml:"connect-timeout" json:"connectTimeout" validate:"min=1"` // 连接超时(秒),官方默认 2s
ReconnectWait int `mapstructure:"reconnect-wait" yaml:"reconnect-wait" json:"reconnectWait" validate:"min=1"` // 重连等待时间(秒),官方默认 2s
ReconnectJitter int `mapstructure:"reconnect-jitter" yaml:"reconnect-jitter" json:"reconnectJitter"` // 重连抖动上限(秒,0=官方默认 100ms)
ReconnectBufSize int64 `mapstructure:"reconnect-buf-size" yaml:"reconnect-buf-size" json:"reconnectBufSize"` // 重连期间发布缓冲区上限(字节,0=官方默认 8MB)
MaxReconnects int `mapstructure:"max-reconnects" yaml:"max-reconnects" json:"maxReconnects"` // 最大重连次数,-1 表示无限
PingInterval int `mapstructure:"ping-interval" yaml:"ping-interval" json:"pingInterval"` // 心跳 ping 间隔(秒,0=官方默认 120s)
MaxPingsOut int `mapstructure:"max-pings-out" yaml:"max-pings-out" json:"maxPingsOut"` // 未回应 ping 上限(0=官方默认 2)
FlushTimeout int `mapstructure:"flush-timeout" yaml:"flush-timeout" json:"flushTimeout"` // Flush 等待超时(秒,0=官方默认 10s)
DrainTimeout int `mapstructure:"drain-timeout" yaml:"drain-timeout" json:"drainTimeout"` // Drain 排空超时(秒,0=官方默认 30s)
// === JetStream 流级运维参数(对齐官方 nats.StreamConfig;流名由业务建流时决策) ===
JetStream bool `mapstructure:"jet-stream" yaml:"jet-stream" json:"jetStream"` // 是否启用 JetStream 持久化
Replicas int `mapstructure:"replicas" yaml:"replicas" json:"replicas"` // 副本数(0=单副本;超出集群规模时建流报错)
Storage string `mapstructure:"storage" yaml:"storage" json:"storage"` // 存储后端:file|memory(空=file)
Retention string `mapstructure:"retention" yaml:"retention" json:"retention"` // 保留策略:limits|interest|workqueue(空=limits)
MaxAge int64 `mapstructure:"max-age" yaml:"max-age" json:"maxAge"` // 消息最大保留时长(秒,0=不限)
MaxBytes int64 `mapstructure:"max-bytes" yaml:"max-bytes" json:"maxBytes"` // 流最大体积(字节,0=不限)
DuplicatesWindow int64 `mapstructure:"duplicates-window" yaml:"duplicates-window" json:"duplicatesWindow"` // Publish 去重窗口(秒,0=官方默认 120s)
ModuleName string `mapstructure:"module-name" yaml:"module-name" json:"moduleName"` // 模块名称
WorkerPoolSize int `mapstructure:"worker-pool-size" yaml:"worker-pool-size" json:"workerPoolSize"` // 全局消费者 WorkerPool 大小(0 表示不初始化)
WorkerQueueSize int `mapstructure:"worker-queue-size" yaml:"worker-queue-size" json:"workerQueueSize"` // 全局消费者 WorkerPool 任务队列大小(0 表示不初始化)
}
Nats 结构体用于配置 NATS 相关参数
NATS 是一款高性能、轻量级的云原生消息系统,常用于:
- 分布式服务间低延迟消息广播
- 事件驱动架构中的发布/订阅
- 启用 JetStream 后支持消息持久化、重放
func (*Nats) WithConnectTimeout ¶
WithConnectTimeout 设置连接超时时间(秒)
func (*Nats) WithDrainTimeout ¶
WithDrainTimeout 设置 Drain 排空超时(秒,0=官方默认)
func (*Nats) WithDuplicatesWindow ¶
WithDuplicatesWindow 设置 JetStream Publish 去重窗口(秒,0=官方默认)
func (*Nats) WithFlushTimeout ¶
WithFlushTimeout 设置 Flush 等待超时(秒,0=官方默认)
func (*Nats) WithJetStream ¶
WithJetStream 设置是否启用 JetStream
func (*Nats) WithMaxAge ¶
WithMaxAge 设置 JetStream 消息最大保留时长(秒,0=不限)
func (*Nats) WithMaxBytes ¶
WithMaxBytes 设置 JetStream 流最大体积(字节,0=不限)
func (*Nats) WithMaxPingsOut ¶
WithMaxPingsOut 设置未回应 ping 上限(0=官方默认)
func (*Nats) WithMaxReconnects ¶
WithMaxReconnects 设置最大重连次数
func (*Nats) WithModuleName ¶
WithModuleName 设置模块名称
func (*Nats) WithPingInterval ¶
WithPingInterval 设置心跳 ping 间隔(秒,0=官方默认)
func (*Nats) WithReconnectBufSize ¶
WithReconnectBufSize 设置重连期间发布缓冲区上限(字节,0=官方默认)
func (*Nats) WithReconnectJitter ¶
WithReconnectJitter 设置重连抖动上限(秒,0=官方默认)
func (*Nats) WithReconnectWait ¶
WithReconnectWait 设置重连等待时间(秒)
func (*Nats) WithReplicas ¶
WithReplicas 设置 JetStream 副本数
func (*Nats) WithRetention ¶
WithRetention 设置 JetStream 保留策略(limits|interest|workqueue)
func (*Nats) WithStorage ¶
WithStorage 设置 JetStream 存储后端(file|memory)
func (*Nats) WithWorkerPoolSize ¶
WithWorkerPoolSize 设置全局消费者 WorkerPool 大小
func (*Nats) WithWorkerQueueSize ¶
WithWorkerQueueSize 设置全局消费者 WorkerPool 任务队列大小