registry

package
v0.0.2 Latest Latest
Warning

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

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

Documentation

Overview

Package registry defines the service registration and discovery abstraction for onexmesh. It strictly separates the server-side Registrar from the client-side Discovery (interface segregation), and plugs concrete backends (Polaris, Etcd, Kubernetes, ...) in through a factory registry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BackendNames

func BackendNames() []string

BackendNames returns the sorted names of all registered backends.

func RegisterBackend

func RegisterBackend(name string, f BackendFactory)

RegisterBackend registers a BackendFactory under name. Backend packages call this from init().

func RegisterDiscovery

func RegisterDiscovery(name string, f DiscoveryFactory)

RegisterDiscovery registers a DiscoveryFactory under name.

func RegisterRegistrar

func RegisterRegistrar(name string, f RegistrarFactory)

RegisterRegistrar registers a RegistrarFactory under name. Backend packages call this from init().

func Registered

func Registered(name string) bool

Registered reports whether name is a registered backend, considering the self-describing Backend registry as well as the legacy registrar/discovery factories. It is used by validation to avoid hardcoding backend whitelists.

Types

type Backend

type Backend interface {
	// Name is the backend identifier, used as the registry type and as the
	// nested flag prefix (e.g. "etcd" -> --registry.etcd.endpoints).
	Name() string
	// AddFlags binds the backend's configuration to flags under prefix. The
	// backend owns a typed Options struct and binds directly to it.
	AddFlags(fs *pflag.FlagSet, prefix string)
	// NewRegistrar builds a server-side Registrar from the parsed options.
	// host/port/protocol identify the local instance endpoint; backends that
	// embed them in Options (consul/nacos/eureka/polaris) set them, while
	// others (etcd/kubernetes) ignore them because registration carries the
	// endpoints in the ServiceInstance.
	NewRegistrar(host string, port int, protocol string) (Registrar, error)
	// NewDiscovery builds a client-side Discovery from the parsed options.
	NewDiscovery() (Discovery, error)
}

Backend is a self-describing registry backend. It contributes its own CLI flags, holds a typed Options value, and constructs its Registrar/Discovery from them. Concrete backends register a BackendFactory via RegisterBackend in init(); the options layer discovers them through this registry rather than importing each backend, so adding a backend no longer requires touching the options package (open/closed principle).

func NewBackend

func NewBackend(name string) (Backend, error)

NewBackend constructs a fresh Backend by name.

type BackendFactory

type BackendFactory func() Backend

BackendFactory constructs a Backend with its default Options.

type Discovery

type Discovery interface {
	// GetService returns the current instances for a service name.
	GetService(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
	// Watch returns a Watcher that streams changes for the service.
	Watch(ctx context.Context, serviceName string) (Watcher, error)
	// Close releases the discovery's underlying resources (SDK clients and their
	// connections). It should be called only after any watchers obtained from
	// this discovery are stopped.
	Close() error
}

Discovery is the client-side discovery contract.

func CreateDiscovery

func CreateDiscovery(name string, opts any) (Discovery, error)

CreateDiscovery creates a Discovery by name.

type DiscoveryFactory

type DiscoveryFactory func(opts any) (Discovery, error)

DiscoveryFactory constructs a Discovery from a backend-specific options value.

type Registrar

type Registrar interface {
	// Register registers the instance with the registry and starts any
	// keepalive/heartbeat needed to keep it alive.
	Register(ctx context.Context, instance *ServiceInstance) error
	// Deregister removes the instance and stops any keepalive.
	Deregister(ctx context.Context, instance *ServiceInstance) error
}

Registrar is the server-side registration contract.

func CreateRegistrar

func CreateRegistrar(name string, opts any) (Registrar, error)

CreateRegistrar creates a Registrar by name. It returns an error when the name is not registered or the factory fails.

type RegistrarFactory

type RegistrarFactory func(opts any) (Registrar, error)

RegistrarFactory constructs a Registrar from a backend-specific options value. Concrete backends register their factory in init() and type-assert the options to their own typed Options struct.

type ServiceInstance

type ServiceInstance struct {
	// ID uniquely identifies the instance (e.g. hostname, or the backend's
	// assigned instance id). May be empty for backends that derive it.
	ID string
	// Name is the logical service name, e.g. "edu.course.student-api".
	Name string
	// Version is the service version.
	Version string
	// Metadata carries arbitrary key/value tags.
	Metadata map[string]string
	// Endpoints lists the reachable addresses with scheme prefixes, e.g.
	// ["grpc://127.0.0.1:39090", "http://127.0.0.1:8080"].
	Endpoints []string
}

ServiceInstance describes a single service instance as seen by a registry.

type Watcher

type Watcher interface {
	// Next returns the next snapshot of instances, blocking until one arrives
	// or the watcher is stopped.
	Next() ([]*ServiceInstance, error)
	// Stop terminates the watcher and releases its resources.
	Stop() error
}

Watcher streams instance changes for a watched service.

Directories

Path Synopsis
Package all blank-imports every built-in registry backend so their init() functions self-register a registry.Backend (and the legacy registrar/discovery factories).
Package all blank-imports every built-in registry backend so their init() functions self-register a registry.Backend (and the legacy registrar/discovery factories).
Package cache wraps a registry.Discovery with a resilient read-through cache inspired by go-micro's registry cache.
Package cache wraps a registry.Discovery with a resilient read-through cache inspired by go-micro's registry cache.
Package consul implements onexmesh registry.Registrar and registry.Discovery backed by HashiCorp Consul (github.com/hashicorp/consul/api).
Package consul implements onexmesh registry.Registrar and registry.Discovery backed by HashiCorp Consul (github.com/hashicorp/consul/api).
Package etcd implements onexmesh registry.Registrar and registry.Discovery backed by etcd, using leases for automatic instance expiry (TTL) and prefix watch for change notifications.
Package etcd implements onexmesh registry.Registrar and registry.Discovery backed by etcd, using leases for automatic instance expiry (TTL) and prefix watch for change notifications.
Package eureka implements onexmesh registry.Registrar and registry.Discovery against a Netflix Eureka server over its REST API, using net/http only.
Package eureka implements onexmesh registry.Registrar and registry.Discovery against a Netflix Eureka server over its REST API, using net/http only.
Package kubernetes implements onexmesh registry.Discovery by discovering Pods through Kubernetes EndpointSlices.
Package kubernetes implements onexmesh registry.Discovery by discovering Pods through Kubernetes EndpointSlices.
Package nacos implements onexmesh registry.Registrar and registry.Discovery backed by the Nacos service discovery (github.com/nacos-group/nacos-sdk-go).
Package nacos implements onexmesh registry.Registrar and registry.Discovery backed by the Nacos service discovery (github.com/nacos-group/nacos-sdk-go).
Package polaris implements onexmesh registry.Registrar and registry.Discovery backed by the Polaris service mesh (github.com/polarismesh/polaris-go).
Package polaris implements onexmesh registry.Registrar and registry.Discovery backed by the Polaris service mesh (github.com/polarismesh/polaris-go).
Package static implements onexmesh registry.Discovery over a fixed address list, so a client can talk to a known ip:port without a service registry.
Package static implements onexmesh registry.Discovery over a fixed address list, so a client can talk to a known ip:port without a service registry.

Jump to

Keyboard shortcuts

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