|
| 1 | +package etcd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/muxi-Infra/muxi-micro/pkg/logger" |
| 11 | + "github.com/muxi-Infra/muxi-micro/pkg/logger/logx" |
| 12 | + "github.com/muxi-Infra/muxi-micro/pkg/transport/grpc/discovery" |
| 13 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 14 | +) |
| 15 | + |
| 16 | +type EtcdDiscovery struct { |
| 17 | + client *clientv3.Client |
| 18 | + logger logger.Logger |
| 19 | + endpoints []string |
| 20 | + dialTimeout time.Duration |
| 21 | + namespace string |
| 22 | + username string |
| 23 | + password string |
| 24 | + watchEventChanSize int |
| 25 | + |
| 26 | + sync.Mutex |
| 27 | +} |
| 28 | + |
| 29 | +type Option func(*EtcdDiscovery) |
| 30 | + |
| 31 | +func WithUsername(username string) Option { |
| 32 | + return func(r *EtcdDiscovery) { |
| 33 | + r.username = username |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func WithPassword(password string) Option { |
| 38 | + return func(r *EtcdDiscovery) { |
| 39 | + r.password = password |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func WithEndpoints(endpoints []string) Option { |
| 44 | + return func(r *EtcdDiscovery) { |
| 45 | + r.endpoints = endpoints |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// WithLogger 用于记录新增或删减节点 |
| 50 | +func WithLogger(l logger.Logger) Option { |
| 51 | + return func(r *EtcdDiscovery) { |
| 52 | + r.logger = l |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func WithDialTimeout(timeout time.Duration) Option { |
| 57 | + return func(r *EtcdDiscovery) { |
| 58 | + r.dialTimeout = timeout |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func WithNamespace(ns string) Option { |
| 63 | + return func(r *EtcdDiscovery) { |
| 64 | + r.namespace = ns |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// WithWatchEventChanSize 设置监听etcd变更的channel大小, 默认为10 |
| 69 | +func WithWatchEventChanSize(size int) Option { |
| 70 | + return func(r *EtcdDiscovery) { |
| 71 | + r.watchEventChanSize = size |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func NewEtcdDiscovery(opts ...Option) (*EtcdDiscovery, error) { |
| 76 | + r := &EtcdDiscovery{ |
| 77 | + endpoints: []string{"127.0.0.1:2379"}, |
| 78 | + dialTimeout: 5 * time.Second, |
| 79 | + namespace: "/services", |
| 80 | + logger: logx.NewStdLogger(), |
| 81 | + username: "root", |
| 82 | + password: "12345678", |
| 83 | + watchEventChanSize: 10, |
| 84 | + } |
| 85 | + |
| 86 | + for _, opt := range opts { |
| 87 | + opt(r) |
| 88 | + } |
| 89 | + |
| 90 | + cli, err := clientv3.New(clientv3.Config{ |
| 91 | + Endpoints: r.endpoints, |
| 92 | + DialTimeout: r.dialTimeout, |
| 93 | + Username: r.username, |
| 94 | + Password: r.password, |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + r.client = cli |
| 100 | + return r, nil |
| 101 | +} |
| 102 | + |
| 103 | +func (r *EtcdDiscovery) Discover(ctx context.Context, serviceName string) ([]string, error) { |
| 104 | + key := fmt.Sprintf("%s/%s/", r.namespace, serviceName) |
| 105 | + |
| 106 | + val, err := r.client.Get(ctx, key, clientv3.WithPrefix()) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + var nodes []string |
| 112 | + for _, kv := range val.Kvs { |
| 113 | + nodes = append(nodes, string(kv.Value)) |
| 114 | + } |
| 115 | + |
| 116 | + return nodes, nil |
| 117 | +} |
| 118 | + |
| 119 | +// Watch 动态监听etcd变更 |
| 120 | +func (r *EtcdDiscovery) Watch(ctx context.Context, serviceName string) <-chan *discovery.Event { |
| 121 | + eventCh := make(chan *discovery.Event, r.watchEventChanSize) |
| 122 | + |
| 123 | + go func() { |
| 124 | + defer func() { |
| 125 | + close(eventCh) |
| 126 | + }() |
| 127 | + |
| 128 | + key := fmt.Sprintf("%s/%s/", r.namespace, serviceName) |
| 129 | + ch := r.client.Watch(ctx, key, clientv3.WithPrefix()) |
| 130 | + for resp := range ch { |
| 131 | + for _, ev := range resp.Events { |
| 132 | + switch ev.Type { |
| 133 | + case clientv3.EventTypePut: |
| 134 | + eventCh <- &discovery.Event{ |
| 135 | + Type: "PUT", |
| 136 | + Address: string(ev.Kv.Value), |
| 137 | + } |
| 138 | + r.logger.Warn(fmt.Sprintf("service[%s] has a new address: %s add into etcd", serviceName, string(ev.Kv.Value))) |
| 139 | + case clientv3.EventTypeDelete: |
| 140 | + addr := strings.TrimPrefix(string(ev.Kv.Key), key) |
| 141 | + eventCh <- &discovery.Event{ |
| 142 | + Type: "DELETE", |
| 143 | + Address: addr, |
| 144 | + } |
| 145 | + r.logger.Warn(fmt.Sprintf("(service[%s], addr[%s]) has been removed from etcd", serviceName, addr)) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + }() |
| 150 | + |
| 151 | + return eventCh |
| 152 | +} |
0 commit comments