Documentation
¶
Overview ¶
Package nns 提供 Nacos Naming Service 高级封装,是 ekit 服务治理的官方推荐入口。
基于 registry 接口构建,提供 mock、静态实例、缓存、singleflight、多级降级等治理能力。 新服务应通过 NNSClient 接入,而非直接使用 registry/* 薄适配。
核心特性 ¶
- 构造注入:每次 New 返回独立实例,推荐通过构造注入传递,不依赖全局状态
- 服务初始化:自动加载本地和依赖服务配置
- HTTP/gRPC Endpoint:自动生成服务端点
- 服务注册/注销:与 Nacos Naming 集成
- 实例订阅:实时监听服务实例变更
- 多级降级:静态实例 > 缓存 > 注册中心
- 框架适配:通过 ServiceClient() 暴露给 nns/kitex、nns/hertz 适配器
快速接入 ¶
创建 NNSClient(推荐通过构造注入传递):
ctx := context.Background()
nnsc := nns.New(cfg)
err := nnsc.Init(ctx, "my-service", []string{"user-service", "order-service"})
if err != nil {
log.Fatal(err)
}
defer nnsc.Close()
注册服务:
err = nnsc.Register(ctx, port, registry.WithMetadata(version.Metadata()))
获取服务实例:
instances, err := nnsc.GetInstances(ctx, "user-service")
创建 HTTP Endpoint:
endpoint, err := nnsc.MakeHTTPEndpoint("user-service")
框架适配:
// Kitex
cli, _ := echo.NewClient("dep",
client.WithResolver(nnskitex.NewResolver(nnsc.ServiceClient())))
svr, _ := echo.NewServer(handler,
server.WithRegistry(nnskitex.NewRegistry(nnsc.ServiceClient())))
// Hertz
cli.Use(sd.Discovery(nnshertz.NewResolver(nnsc.ServiceClient())))
svr := server.New(..., server.WithRegistry(nnshertz.NewRegistry(nnsc.ServiceClient())))
配置选项 ¶
- WithCacheExpiry: 缓存过期时间,默认 30s
- WithMockMode: 启用模拟模式(本地开发)
- WithStaticInstances: 静态实例配置
- WithLocalServiceConfig: 本地服务配置(不从 Nacos 获取)
与 bootstrap 集成 ¶
使用 bootstrap.Run 并设置 DepServices 自动初始化 NNS:
b, _ := bootstrap.New(bootstrap.WithServiceName("my-service"),
bootstrapnacos.WithNNS(nacosCfg, port, []string{"dep.service"})) // bootstrap/nacos
b.Run(func(app *bootstrap.App) error {
// ...
})
Index ¶
- type NNSClient
- func (n *NNSClient) AddStaticInstance(serviceName string, instances ...registry.Instance)
- func (n *NNSClient) Close()
- func (n *NNSClient) Deregister(ctx context.Context, port int, opts ...registry.RegisterOptionFunc) error
- func (n *NNSClient) GetAllInstances(ctx context.Context, serviceName string) ([]registry.Instance, error)
- func (n *NNSClient) GetInstances(ctx context.Context, serviceName string) ([]registry.Instance, error)
- func (n *NNSClient) GetService(ctx context.Context, serviceName string) (*metadata.ServiceBasic, error)
- func (n *NNSClient) Init(ctx context.Context, serviceName string, depServices []string) error
- func (n *NNSClient) Register(ctx context.Context, port int, opts ...registry.RegisterOptionFunc) error
- func (n *NNSClient) ServiceClient() *client.ServiceClient
- func (n *NNSClient) SetStaticInstances(serviceName string, instances []registry.Instance)
- func (n *NNSClient) Subscribe(ctx context.Context, serviceName string, callback func([]registry.Instance)) error
- func (n *NNSClient) Unsubscribe(ctx context.Context, serviceName string) error
- type NNSOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NNSClient ¶
type NNSClient struct {
// contains filtered or unexported fields
}
NNSClient NNS 服务客户端,封装了服务发现、负载均衡和端点管理功能。
门面是 provider 选择点:nns.New(cfg) 负责把 Nacos 配置构造为 registry/nacos 实现并注入 provider 无关的 ServiceClient(2026-09-19 重构:默认构造从 nns/client 上提至此,治理核心不再绑定 provider)。
func New ¶
New 创建 NNS 客户端。 cfg 是 Nacos 连接配置,opts 可选配置 mock 模式、静态实例、缓存过期等。 返回的 NNSClient 需调用 Init 初始化后使用。
Provider 默认构造(非 mock 时):创建 naming/config SDK 客户端并注入 registry/nacos 的 Discoverer/Subscriber/Registrar/ConfigStore;构造失败 只 Warn 降级(缓存/静态实例路径),不阻断启动——与既有降级语义一致。 默认路径创建的 SDK 客户端归本实例所有,Close 时释放。
func (*NNSClient) AddStaticInstance ¶
AddStaticInstance 向指定服务追加静态实例地址。
func (*NNSClient) Close ¶
func (n *NNSClient) Close()
Close 关闭 NNSClient 持有的所有资源(ServiceClient 的配置/实例订阅、 回调状态、门面默认路径创建的 Nacos SDK 客户端)。 Close 是终结操作:默认路径下 SDK gRPC 连接已关闭,复用需重新 New。 端点工厂自 Round-4 起移至 ekit-nns-endpoint,其生命周期由构造方管理。
func (*NNSClient) Deregister ¶
func (n *NNSClient) Deregister(ctx context.Context, port int, opts ...registry.RegisterOptionFunc) error
Deregister 从注册中心注销当前服务实例。
func (*NNSClient) GetAllInstances ¶
func (n *NNSClient) GetAllInstances(ctx context.Context, serviceName string) ([]registry.Instance, error)
GetAllInstances 获取指定服务的全部实例列表(包括不健康的实例)。
func (*NNSClient) GetInstances ¶
func (n *NNSClient) GetInstances(ctx context.Context, serviceName string) ([]registry.Instance, error)
GetInstances 获取指定服务的健康实例列表。
func (*NNSClient) GetService ¶
func (n *NNSClient) GetService(ctx context.Context, serviceName string) (*metadata.ServiceBasic, error)
GetService 获取指定服务的基本配置信息。
func (*NNSClient) Register ¶
func (n *NNSClient) Register(ctx context.Context, port int, opts ...registry.RegisterOptionFunc) error
Register 向注册中心注册当前服务实例。
func (*NNSClient) ServiceClient ¶
func (n *NNSClient) ServiceClient() *client.ServiceClient
ServiceClient 返回底层 *client.ServiceClient,供 nns/kitex、nns/hertz 等适配器使用。
推荐用法:
nnskitex.NewResolver(nnsc.ServiceClient()) nnshertz.NewRegistry(nnsc.ServiceClient())
不应绕过此访问器直接访问内部 ServiceClient 字段。
func (*NNSClient) SetStaticInstances ¶
SetStaticInstances 设置指定服务的静态实例列表(覆盖原有)。
type NNSOption ¶
type NNSOption func(*clientOptions)
NNSOption NNSClient 的配置选项函数类型。
func WithLocalServiceConfig ¶
func WithLocalServiceConfig(serviceName string, basic *metadata.ServiceBasic) NNSOption
WithLocalServiceConfig 配置本地服务配置(不从 Nacos 获取) 用于本地开发或测试环境,直接指定服务的超时、重试等配置
func WithMockMode ¶
WithMockMode 设置是否启用 Mock 模式,Mock 模式下不连接 Nacos。
Directories
¶
| Path | Synopsis |
|---|---|
|
Package client 提供 NNS(enjoydream Naming Service)治理核心: 服务发现、配置管理、实例订阅与注册的 provider 无关实现。
|
Package client 提供 NNS(enjoydream Naming Service)治理核心: 服务发现、配置管理、实例订阅与注册的 provider 无关实现。 |
|
Package metadata 定义了 NNS(enjoydream Naming Service)中使用的核心元数据类型和常量。
|
Package metadata 定义了 NNS(enjoydream Naming Service)中使用的核心元数据类型和常量。 |