1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-22 03:38:41 +02:00
kratos/registry/registry.go

96 lines
2.4 KiB
Go
Raw Normal View History

2021-02-17 17:14:47 +08:00
package registry
import (
"context"
"fmt"
"sort"
)
2021-02-28 13:39:45 +08:00
// Registrar is service registrar.
type Registrar interface {
2021-02-17 17:14:47 +08:00
// Register the registration.
Register(ctx context.Context, service *ServiceInstance) error
2021-02-17 17:14:47 +08:00
// Deregister the registration.
Deregister(ctx context.Context, service *ServiceInstance) error
2021-02-28 13:39:45 +08:00
}
// Discovery is service discovery.
type Discovery interface {
// GetService return the service instances in memory according to the service name.
GetService(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
2021-02-17 17:14:47 +08:00
// Watch creates a watcher according to the service name.
2021-02-28 13:39:45 +08:00
Watch(ctx context.Context, serviceName string) (Watcher, error)
2021-02-17 17:14:47 +08:00
}
// Watcher is service watcher.
type Watcher interface {
2021-04-14 13:54:14 +08:00
// Next returns services in the following two cases:
2021-02-17 17:14:47 +08:00
// 1.the first time to watch and the service instance list is not empty.
// 2.any service instance changes found.
// if the above two conditions are not met, it will block until context deadline exceeded or canceled
Next() ([]*ServiceInstance, error)
2021-04-14 13:54:14 +08:00
// Stop close the watcher.
Stop() error
2021-02-17 17:14:47 +08:00
}
// ServiceInstance is an instance of a service in a discovery system.
type ServiceInstance struct {
// ID is the unique instance ID as registered.
2021-03-15 17:06:06 +08:00
ID string `json:"id"`
2021-02-17 17:14:47 +08:00
// Name is the service name as registered.
2021-03-15 17:06:06 +08:00
Name string `json:"name"`
2021-02-17 17:14:47 +08:00
// Version is the version of the compiled.
2021-03-15 17:06:06 +08:00
Version string `json:"version"`
2021-02-17 17:14:47 +08:00
// Metadata is the kv pair metadata associated with the service instance.
2021-03-15 17:06:06 +08:00
Metadata map[string]string `json:"metadata"`
// Endpoints are endpoint addresses of the service instance.
2021-02-17 17:14:47 +08:00
// schema:
// http://127.0.0.1:8000?isSecure=false
// grpc://127.0.0.1:9000?isSecure=false
2021-03-15 17:06:06 +08:00
Endpoints []string `json:"endpoints"`
2021-02-17 17:14:47 +08:00
}
func (i *ServiceInstance) String() string {
return fmt.Sprintf("%s-%s", i.Name, i.ID)
}
// Equal returns whether i and o are equivalent.
func (i *ServiceInstance) Equal(o interface{}) bool {
if i == nil && o == nil {
return true
}
if i == nil || o == nil {
return false
}
t, ok := o.(*ServiceInstance)
if !ok {
return false
}
if len(i.Endpoints) != len(t.Endpoints) {
return false
}
sort.Strings(i.Endpoints)
sort.Strings(t.Endpoints)
for j := 0; j < len(i.Endpoints); j++ {
if i.Endpoints[j] != t.Endpoints[j] {
return false
}
}
if len(i.Metadata) != len(t.Metadata) {
return false
}
for k, v := range i.Metadata {
if v != t.Metadata[k] {
return false
}
}
return i.ID == t.ID && i.Name == t.Name && i.Version == t.Version
}