2015-05-27 22:22:02 +01:00
|
|
|
package etcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2015-09-26 17:09:03 +01:00
|
|
|
etcd "github.com/coreos/etcd/client"
|
2015-05-27 22:22:02 +01:00
|
|
|
"github.com/myodc/go-micro/registry"
|
2015-09-26 17:09:03 +01:00
|
|
|
"golang.org/x/net/context"
|
2015-05-27 22:22:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
prefix = "/micro-registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
type etcdRegistry struct {
|
2015-09-26 17:09:03 +01:00
|
|
|
client etcd.KeysAPI
|
2015-05-27 22:22:02 +01:00
|
|
|
|
|
|
|
sync.RWMutex
|
|
|
|
services map[string]*registry.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
func encode(s *registry.Service) string {
|
|
|
|
b, _ := json.Marshal(s)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decode(ds string) *registry.Service {
|
|
|
|
var s *registry.Service
|
|
|
|
json.Unmarshal([]byte(ds), &s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodePath(s, id string) string {
|
|
|
|
service := strings.Replace(s, "/", "-", -1)
|
|
|
|
node := strings.Replace(id, "/", "-", -1)
|
|
|
|
return filepath.Join(prefix, service, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func servicePath(s string) string {
|
|
|
|
return filepath.Join(prefix, strings.Replace(s, "/", "-", -1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) Deregister(s *registry.Service) error {
|
|
|
|
if len(s.Nodes) == 0 {
|
|
|
|
return errors.New("Require at least one node")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range s.Nodes {
|
2015-09-26 17:09:03 +01:00
|
|
|
_, err := e.client.Delete(context.Background(), nodePath(s.Name, node.Id), &etcd.DeleteOptions{Recursive: false})
|
2015-05-27 22:22:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:09:03 +01:00
|
|
|
e.client.Delete(context.Background(), servicePath(s.Name), &etcd.DeleteOptions{Dir: true})
|
2015-05-27 22:22:02 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) Register(s *registry.Service) error {
|
|
|
|
if len(s.Nodes) == 0 {
|
|
|
|
return errors.New("Require at least one node")
|
|
|
|
}
|
|
|
|
|
|
|
|
service := ®istry.Service{
|
2015-10-11 16:41:55 +01:00
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Metadata: s.Metadata,
|
|
|
|
Endpoints: s.Endpoints,
|
2015-05-27 22:22:02 +01:00
|
|
|
}
|
|
|
|
|
2015-09-26 17:09:03 +01:00
|
|
|
e.client.Set(context.Background(), servicePath(s.Name), "", &etcd.SetOptions{Dir: true})
|
2015-05-27 22:22:02 +01:00
|
|
|
|
|
|
|
for _, node := range s.Nodes {
|
|
|
|
service.Nodes = []*registry.Node{node}
|
2015-09-26 17:09:03 +01:00
|
|
|
_, err := e.client.Set(context.Background(), nodePath(service.Name, node.Id), encode(service), &etcd.SetOptions{})
|
2015-05-27 22:22:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) GetService(name string) (*registry.Service, error) {
|
|
|
|
e.RLock()
|
|
|
|
service, ok := e.services[name]
|
|
|
|
e.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return service, nil
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:09:03 +01:00
|
|
|
rsp, err := e.client.Get(context.Background(), servicePath(name), &etcd.GetOptions{})
|
2015-05-27 22:22:02 +01:00
|
|
|
if err != nil && !strings.HasPrefix(err.Error(), "100: Key not found") {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := ®istry.Service{}
|
|
|
|
|
|
|
|
for _, n := range rsp.Node.Nodes {
|
|
|
|
if n.Dir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sn := decode(n.Value)
|
2015-09-26 17:09:03 +01:00
|
|
|
s.Name = sn.Name
|
2015-10-11 16:41:55 +01:00
|
|
|
s.Version = sn.Version
|
2015-09-26 17:09:03 +01:00
|
|
|
s.Metadata = sn.Metadata
|
2015-10-11 16:41:55 +01:00
|
|
|
s.Endpoints = sn.Endpoints
|
2015-05-27 22:22:02 +01:00
|
|
|
for _, node := range sn.Nodes {
|
|
|
|
s.Nodes = append(s.Nodes, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *etcdRegistry) ListServices() ([]*registry.Service, error) {
|
|
|
|
e.RLock()
|
|
|
|
serviceMap := e.services
|
|
|
|
e.RUnlock()
|
|
|
|
|
|
|
|
var services []*registry.Service
|
|
|
|
|
|
|
|
if len(serviceMap) > 0 {
|
|
|
|
for _, service := range services {
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:09:03 +01:00
|
|
|
rsp, err := e.client.Get(context.Background(), prefix, &etcd.GetOptions{Recursive: true, Sort: true})
|
2015-05-27 22:22:02 +01:00
|
|
|
if err != nil && !strings.HasPrefix(err.Error(), "100: Key not found") {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range rsp.Node.Nodes {
|
|
|
|
service := ®istry.Service{}
|
|
|
|
|
|
|
|
for _, n := range node.Nodes {
|
|
|
|
i := decode(n.Value)
|
|
|
|
service.Name = i.Name
|
|
|
|
for _, in := range i.Nodes {
|
|
|
|
service.Nodes = append(service.Nodes, in)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:55:27 +01:00
|
|
|
func (e *etcdRegistry) Watch() (registry.Watcher, error) {
|
|
|
|
// todo: fix watcher
|
|
|
|
return newEtcdWatcher(e)
|
2015-05-27 22:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRegistry(addrs []string, opt ...registry.Option) registry.Registry {
|
|
|
|
var cAddrs []string
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if len(addr) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cAddrs = append(cAddrs, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cAddrs) == 0 {
|
|
|
|
cAddrs = []string{"http://127.0.0.1:2379"}
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:09:03 +01:00
|
|
|
c, _ := etcd.New(etcd.Config{
|
|
|
|
Endpoints: cAddrs,
|
|
|
|
})
|
|
|
|
|
2015-05-27 22:22:02 +01:00
|
|
|
e := &etcdRegistry{
|
2015-09-26 17:09:03 +01:00
|
|
|
client: etcd.NewKeysAPI(c),
|
2015-05-27 22:22:02 +01:00
|
|
|
services: make(map[string]*registry.Service),
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|