-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrpcLb.go
73 lines (66 loc) · 1.62 KB
/
GrpcLb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package GrpcConnectionPool
import (
"errors"
"github.com/hashicorp/consul/api"
"google.golang.org/grpc/naming"
"net"
"strconv"
)
//grpc 负载均衡库
func NewConsulResolver(address string, service string) naming.Resolver {
return &consulResolver{
address: address,
service: service,
}
}
type consulResolver struct {
address string
service string
}
func (r *consulResolver) Resolve(target string) (naming.Watcher, error) {
config := api.DefaultNonPooledConfig()
config.Address = r.address
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
return &consulWatcher{
client: client,
service: r.service,
//addrs: map[string]struct{}{},
}, nil
}
type consulWatcher struct {
client *api.Client
service string
//addrs map[string]struct{}
}
func (w *consulWatcher) Next() ([]*naming.Update, error) {
services, _, err := w.client.Health().Service(w.service, "", true, &api.QueryOptions{})
if err != nil {
return nil, err
}
addrs := map[string]struct{}{}
for _, service := range services {
addrs[net.JoinHostPort(service.Service.Address, strconv.Itoa(service.Service.Port))] = struct{}{}
}
var updates []*naming.Update
//for addr := range w.addrs {
// if _, ok := addrs[addr]; !ok {
// updates = append(updates, &naming.Update{Op: naming.Delete, Addr: addr})
// }
//}
for addr := range addrs {
//if _, ok := w.addrs[addr]; !ok {
updates = append(updates, &naming.Update{Op: naming.Add, Addr: addr})
//}
}
if len(updates) != 0 {
//w.addrs = addrs
return updates, nil
} else {
return nil, errors.New("No Have" + w.service + "In Consul")
}
}
func (w *consulWatcher) Close() {
}