-
Notifications
You must be signed in to change notification settings - Fork 13
/
consul_registry.go
185 lines (152 loc) · 4.34 KB
/
consul_registry.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package consul
import (
"errors"
"fmt"
"strings"
"github.com/cloudwego/kitex/pkg/registry"
"github.com/hashicorp/consul/api"
)
type options struct {
check *api.AgentServiceCheck
}
type consulRegistry struct {
consulClient *api.Client
opts options
}
const kvJoinChar = ":"
var _ registry.Registry = (*consulRegistry)(nil)
var errIllegalTagChar = errors.New("illegal tag character")
// Option is consul option.
type Option func(o *options)
// WithCheck is consul registry option to set AgentServiceCheck.
// If disable consul check, set the check option to nil.
func WithCheck(check *api.AgentServiceCheck) Option {
return func(o *options) { o.check = check }
}
// NewConsulRegister create a new registry using consul.
func NewConsulRegister(address string, opts ...Option) (registry.Registry, error) {
config := api.DefaultConfig()
config.Address = address
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
op := options{
check: defaultCheck(),
}
for _, option := range opts {
option(&op)
}
return &consulRegistry{consulClient: client, opts: op}, nil
}
// NewConsulRegisterWithConfig create a new registry using consul, with a custom config.
func NewConsulRegisterWithConfig(config *api.Config, opts ...Option) (*consulRegistry, error) {
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
op := options{
check: defaultCheck(),
}
for _, option := range opts {
option(&op)
}
return &consulRegistry{consulClient: client, opts: op}, nil
}
// NewConsulRegisterWithClient create a new registry using consul, with client.
func NewConsulRegisterWithClient(client *api.Client, opts ...Option) (*consulRegistry, error) {
op := options{
check: defaultCheck(),
}
for _, option := range opts {
option(&op)
}
return &consulRegistry{consulClient: client, opts: op}, nil
}
// Register register a service to consul.
// Note: the tag map of the service can not contain the `:` character.
func (c *consulRegistry) Register(info *registry.Info) error {
if err := validateRegistryInfo(info); err != nil {
return err
}
host, port, err := parseAddr(info.Addr)
if err != nil {
return err
}
svcID, err := getServiceId(info)
if err != nil {
return err
}
tagSlice, err := convTagMapToSlice(info.Tags)
if err != nil {
return err
}
svcInfo := &api.AgentServiceRegistration{
ID: svcID,
Address: host,
Port: port,
Name: info.ServiceName,
Tags: tagSlice,
Weights: &api.AgentWeights{
Passing: info.Weight,
Warning: info.Weight,
},
Check: c.opts.check,
}
if c.opts.check != nil {
c.opts.check.TCP = fmt.Sprintf("%s:%d", host, port)
svcInfo.Check = c.opts.check
}
return c.consulClient.Agent().ServiceRegister(svcInfo)
}
// Deregister deregister a service from consul.
func (c *consulRegistry) Deregister(info *registry.Info) error {
svcID, err := getServiceId(info)
if err != nil {
return err
}
return c.consulClient.Agent().ServiceDeregister(svcID)
}
func validateRegistryInfo(info *registry.Info) error {
if info.ServiceName == "" {
return errors.New("missing service name in consul register")
}
if info.Addr == nil {
return errors.New("missing addr in consul register")
}
return nil
}
func defaultCheck() *api.AgentServiceCheck {
check := new(api.AgentServiceCheck)
check.Timeout = "5s"
check.Interval = "5s"
check.DeregisterCriticalServiceAfter = "1m"
return check
}
// convTagMapToSlice Tags map be convert to slice.
// Keys must not contain `:`.
func convTagMapToSlice(tagMap map[string]string) ([]string, error) {
svcTags := make([]string, 0, len(tagMap))
for k, v := range tagMap {
if strings.Contains(k, kvJoinChar) {
return svcTags, errIllegalTagChar
}
svcTags = append(svcTags, fmt.Sprintf("%s%s%s", k, kvJoinChar, v))
}
return svcTags, nil
}