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 ¶
- func BackendNames() []string
- func DecodeOptions(raw map[string]any, out any) error
- func RegisterBackend(name string, f BackendFactory)
- func RegisterDiscovery(name string, f DiscoveryFactory)
- func RegisterRegistrar(name string, f RegistrarFactory)
- func Registered(name string) bool
- type Backend
- type BackendFactory
- type Discovery
- type DiscoveryFactory
- type Registrar
- type RegistrarFactory
- type ServiceInstance
- type Watcher
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 DecodeOptions ¶ added in v0.0.3
DecodeOptions overlays configuration-file values onto a backend's typed Options. It is the single implementation behind every backend's Decode, so a backend gains file configuration with one line rather than a hand-written field mapping per option.
The matcher ignores case and hyphens, so the YAML key "dial-timeout" and the Go field DialTimeout are the same option. Without that every multi-word option would decode to nothing and leave its default in place — a failure with no symptom, since the service starts and merely ignores what it was configured with. That is the same shape of mistake this layer exists to remove.
The decoder config mirrors viper's, so a value written in a config file behaves here as it does everywhere else in the application: a number or a boolean may arrive as a string, and a duration may be written "10s".
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 ¶
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)
// Decode overlays configuration-file values onto the backend's Options.
// Keys are flag names with the prefix removed, e.g. "addr" for
// --registry.polaris.addr. It is what makes a backend configurable from a
// YAML file at all: the options layer cannot reach a backend's typed
// Options by itself, because that is exactly the dependency this registry
// exists to avoid. Implementations are one call to DecodeOptions.
Decode(raw map[string]any) error
// 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 ¶
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.
type DiscoveryFactory ¶
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.
type RegistrarFactory ¶
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. |