Documentation
¶
Overview ¶
Package nacos 提供 Nacos 配置中心实现
Package nacos 基于 Nacos 提供注册中心实现。
该包将 Nacos 与 go-boot 服务发现和注册中心接口集成, 使用 Nacos 官方 SDK 实现服务注册、发现和订阅。
定义:
- NacosRegistry: 注册中心实现了 center.Registry 接口
- Config: Nacos 配置
- Option: 配置选项函数
快速开始:
// 创建 Nacos 注册中心
registry, err := nacos.NewNacosRegistry(
nacos.WithAddress("127.0.0.1:8848"),
)
// 注册服务
registry.Register(ctx, center.InstanceInfo{
ServiceName: "my-service",
Host: "127.0.0.1",
Port: 8080,
})
Index ¶
- type Config
- type NacosConfigCenter
- type NacosRegistry
- func (r *NacosRegistry) Deregister(_ context.Context, info center.InstanceInfo) error
- func (r *NacosRegistry) Discover(_ context.Context, serviceName string) ([]center.InstanceInfo, error)
- func (r *NacosRegistry) Register(_ context.Context, info center.InstanceInfo) error
- func (r *NacosRegistry) Watch(ctx context.Context, serviceName string) (<-chan []center.InstanceInfo, error)
- type Option
- func WithAddress(addr string) Option
- func WithContainer(ctn core.Container) Option
- func WithGroup(group string) Option
- func WithHeartbeatInterval(interval time.Duration) Option
- func WithLogLevel(level string) Option
- func WithNamespace(namespaceID string) Option
- func WithTimeout(timeout time.Duration) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Address string
NamespaceID string
Group string
LogLevel string
Timeout time.Duration
HeartbeatInterval time.Duration
Container core.Container
}
Config Nacos 注册中心配置。
type NacosConfigCenter ¶
type NacosConfigCenter struct {
// contains filtered or unexported fields
}
func NewNacosConfigCenter ¶
func NewNacosConfigCenter(cfg *config.ConfigCenterConfig) (*NacosConfigCenter, error)
func (*NacosConfigCenter) Load ¶
func (n *NacosConfigCenter) Load() (config.ConfigData, error)
Load 加载所有配置数据
func (*NacosConfigCenter) Watch ¶
func (n *NacosConfigCenter) Watch(key string, callback func(config.ConfigData)) error
Watch 监控配置变更
type NacosRegistry ¶
type NacosRegistry struct {
// contains filtered or unexported fields
}
NacosRegistry 基于 Nacos 实现的注册中心。 使用 Nacos 官方 SDK 实现服务注册、发现和订阅。
func NewNacosRegistry ¶
func NewNacosRegistry(opts ...Option) (*NacosRegistry, error)
NewNacosRegistry 创建 Nacos 注册中心实例。 支持通过 Option 配置地址、命名空间、分组、日志级别等参数。
func (*NacosRegistry) Deregister ¶
func (r *NacosRegistry) Deregister(_ context.Context, info center.InstanceInfo) error
Deregister 从 Nacos 中注销指定服务实例。
func (*NacosRegistry) Discover ¶
func (r *NacosRegistry) Discover(_ context.Context, serviceName string) ([]center.InstanceInfo, error)
Discover 发现指定服务的所有健康实例。
func (*NacosRegistry) Register ¶
func (r *NacosRegistry) Register(_ context.Context, info center.InstanceInfo) error
Register 向 Nacos 注册一个服务实例。
func (*NacosRegistry) Watch ¶
func (r *NacosRegistry) Watch(ctx context.Context, serviceName string) (<-chan []center.InstanceInfo, error)
Watch 监听指定服务的实例变化。 内部调用 Nacos Subscribe 注册回调,当实例列表变化时通过返回的 channel 通知。 当 context 被取消时,自动调用 Unsubscribe 清理订阅。
type Option ¶
type Option func(*Config)
Option Nacos 配置的函数式选项。
func WithHeartbeatInterval ¶
WithHeartbeatInterval 设置实例心跳间隔。
Click to show internal directories.
Click to hide internal directories.