registry

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: MIT Imports: 10 Imported by: 0

README

registry

registry 包是 go-consul 中的注册中心实现,负责将 go-micro/registry 的核心模型映射到 Consul。

核心能力

  • NewRegister:创建注册器
  • (*RegisterInstance).Install:注册当前服务实例
  • (*RegisterInstance).Uninstall:注销当前服务实例
  • NewDiscover:创建发现器(网关使用)
  • (*DiscoverInstance).GetService:按 rpc method 获取服务节点
  • (*DiscoverInstance).Watcher:持续刷新本地发现缓存
  • (*DiscoverInstance).Unwatch:停止发现监听
  • (*DiscoverInstance).WatchEvent:订阅服务变更回调

配置模型

  • ServiceConf
    • InstanceIdNamespaceNetworkKernel
    • TTLMaxRetryWeight
  • GatewayConf
    • Network

Bootstrap() 会补齐默认值,避免零值导致运行期问题。

元数据映射策略

注册时会把核心信息编码到 Consul Meta,包括:

  • namespace
  • envapp_idversion
  • network_snnetwork_external
  • kernel_languagekernel_version
  • weightrun_date
  • methods(方法映射 JSON)
  • node(完整节点 JSON)

健康检查默认使用 TCP 检查,并设置 DeregisterCriticalServiceAfter 自动摘除异常实例。

快速示例

package main

import (
	consulx "github.com/fireflycore/go-consul"
	"github.com/fireflycore/go-consul/registry"
	micro "github.com/fireflycore/go-micro/registry"
)

func main() {
	client, err := consulx.New(&consulx.Conf{Address: "127.0.0.1:8500"})
	if err != nil {
		panic(err)
	}

	reg, err := registry.NewRegister(client, &micro.Meta{
		Env:     "prod",
		AppId:   "user-service",
		Version: "v1.0.0",
	}, &registry.ServiceConf{
		Network: &micro.Network{Internal: "127.0.0.1:9001"},
		Kernel:  &micro.Kernel{},
		TTL:     10,
		Weight:  100,
	})
	if err != nil {
		panic(err)
	}

	_ = reg.Install(&micro.ServiceNode{
		Methods: map[string]bool{
			"/acme.user.v1.UserService/Login": true,
		},
	})
	defer reg.Uninstall()
}

网关发现示例

package main

import (
	"context"
	"time"

	consulx "github.com/fireflycore/go-consul"
	"github.com/fireflycore/go-consul/registry"
	micro "github.com/fireflycore/go-micro/registry"
)

func main() {
	client, err := consulx.New(&consulx.Conf{Address: "127.0.0.1:8500"})
	if err != nil {
		panic(err)
	}

	dis, err := registry.NewDiscover(client, &micro.Meta{
		Env: "prod",
	}, &registry.ServiceConf{
		Namespace: "/microservice/demo",
		TTL:       10,
		Network:   &micro.Network{},
		Kernel:    &micro.Kernel{},
	})
	if err != nil {
		panic(err)
	}

	go dis.Watcher()
	defer dis.Unwatch()
	dis.WatchEvent(func(event *micro.ServiceEvent) {
		_ = event
	})

	time.Sleep(500 * time.Millisecond)

	nodes, appID, err := dis.GetService("/acme.user.v1.UserService/Login")
	if err != nil {
		panic(err)
	}

	_, _, _ = context.Background(), appID, nodes
}

说明

  • 发现器会维护两级索引:method -> appIdappId -> nodes,供网关快速路由。
  • 发现器会按 ServiceConf.Namespace 过滤实例,避免跨业务命名空间串流量。
  • WatchEvent 回调对外透传 go-micro/registry.ServiceEvent
  • 事件增删判定以 app_id + instance_id 为准,不保留旧判定路径。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDiscover

func NewDiscover(client *api.Client, meta *micro.Meta, conf *ServiceConf) (micro.Discovery, error)

NewDiscover 创建发现器并返回统一发现接口。

func NewRegister

func NewRegister(client *api.Client, meta *micro.Meta, conf *ServiceConf) (micro.Register, error)

NewRegister 创建一个 Consul 注册器实例并返回统一注册接口。

Types

type DiscoverInstance

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

DiscoverInstance 封装 Consul 网关发现逻辑。

func (*DiscoverInstance) GetService

func (s *DiscoverInstance) GetService(method string) ([]*micro.ServiceNode, string, error)

GetService 根据 method 查询服务节点列表。

func (*DiscoverInstance) Unwatch

func (s *DiscoverInstance) Unwatch()

Unwatch 停止监听流程。

func (*DiscoverInstance) WatchEvent

func (s *DiscoverInstance) WatchEvent(callback micro.WatchEventFunc)

WatchEvent 注册服务变更回调。

func (*DiscoverInstance) Watcher

func (s *DiscoverInstance) Watcher()

Watcher 启动发现刷新与阻塞监听。

type EventType

type EventType int

EventType 表示发现快照差异事件类型。

const (
	// EventAdd 表示新增实例事件。
	EventAdd EventType = iota
	// EventUpdate 表示实例变更事件。
	EventUpdate
	// EventDelete 表示实例删除事件。
	EventDelete
)

type GatewayConf

type GatewayConf struct {
	// 网卡
	Network *micro.Network `json:"network"`
}

GatewayConf 定义网关相关配置。

func (*GatewayConf) Bootstrap

func (gc *GatewayConf) Bootstrap()

Bootstrap 补齐网关网络配置默认值。

type RegisterInstance

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

RegisterInstance 封装 Consul 服务注册生命周期。

func (*RegisterInstance) Install

func (s *RegisterInstance) Install(service *micro.ServiceNode) error

Install 将服务实例注册到 Consul。

func (*RegisterInstance) Uninstall

func (s *RegisterInstance) Uninstall() error

Uninstall 将当前实例从 Consul 注销。

type ServiceConf

type ServiceConf struct {
	// 实例Id
	InstanceId string `json:"instance_id"`
	// 命名空间
	Namespace string `json:"namespace"`
	// 网卡
	Network *micro.Network `json:"network"`
	// 内核
	Kernel *micro.Kernel `json:"kernel"`

	// 最大重试次数, 间隔时间是TTL*5
	MaxRetry uint32 `json:"max_retry"`
	// 心跳/租约 TTL(秒), 最少是10s
	TTL uint32 `json:"ttl"`
	// 权重
	Weight int `json:"weight"`
}

ServiceConf 定义服务实例在 Consul 注册场景下的配置。

func (*ServiceConf) Bootstrap

func (sc *ServiceConf) Bootstrap()

Bootstrap 补齐 namespace/ttl/maxRetry/network/kernel 等默认值,避免下游逻辑出现零值陷阱

type ServiceEvent

type ServiceEvent struct {
	// Type 表示事件类型。
	Type EventType
	// Service 表示发生变化的节点对象。
	Service *micro.ServiceNode
}

ServiceEvent 是发现层事件载体。

Jump to

Keyboard shortcuts

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