/
registry.go
275 lines (244 loc) · 7.12 KB
/
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2022 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 servicecomb
import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/cloudwego/hertz/pkg/app/server/registry"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/go-chassis/cari/discovery"
"github.com/go-chassis/sc-client"
"github.com/thoas/go-funk"
)
var _ registry.Registry = (*serviceCombRegistry)(nil)
type scHeartbeat struct {
cancel context.CancelFunc
instanceKey string
}
type registryOptions struct {
appId string
versionRule string
hostName string
heartbeatInterval int32
}
// RegistryOption is ServiceComb option.
type RegistryOption func(o *registryOptions)
// WithAppId with app id option
func WithAppId(appId string) RegistryOption {
return func(o *registryOptions) {
o.appId = appId
}
}
// WithRegistryVersionRule with version rule option
func WithRegistryVersionRule(versionRule string) RegistryOption {
return func(o *registryOptions) {
o.versionRule = versionRule
}
}
// WithRegistryHostName with host name option
func WithRegistryHostName(hostName string) RegistryOption {
return func(o *registryOptions) {
o.hostName = hostName
}
}
// WithRegistryHeartbeatInterval with heart beat second
func WithRegistryHeartbeatInterval(second int32) RegistryOption {
return func(o *registryOptions) {
o.heartbeatInterval = second
}
}
type serviceCombRegistry struct {
cli *sc.Client
opts registryOptions
lock *sync.RWMutex
registryIns map[string]*scHeartbeat
}
// NewDefaultSCRegistry create a new default ServiceComb registry
func NewDefaultSCRegistry(endPoints []string, opts ...RegistryOption) (registry.Registry, error) {
client, err := sc.NewClient(sc.Options{
Endpoints: endPoints,
})
if err != nil {
return nil, err
}
return NewSCRegistry(client, opts...), nil
}
// NewSCRegistry create a new ServiceComb registry
func NewSCRegistry(client *sc.Client, opts ...RegistryOption) registry.Registry {
op := registryOptions{
appId: "DEFAULT",
versionRule: "1.0.0",
hostName: "DEFAULT",
heartbeatInterval: 5,
}
for _, opt := range opts {
opt(&op)
}
return &serviceCombRegistry{
cli: client,
opts: op,
lock: &sync.RWMutex{},
registryIns: make(map[string]*scHeartbeat),
}
}
// Register a service info to ServiceComb
func (scr *serviceCombRegistry) Register(info *registry.Info) error {
err := scr.validRegistryInfo(info)
if err != nil {
return err
}
addr, err := scr.parseAddr(info.Addr.String())
if err != nil {
return err
}
instanceKey := fmt.Sprintf("%s:%s", info.ServiceName, addr)
scr.lock.RLock()
_, ok := scr.registryIns[instanceKey]
scr.lock.RUnlock()
if ok {
return fmt.Errorf("instance{%s} already registered", instanceKey)
}
serviceID, err := scr.cli.RegisterService(&discovery.MicroService{
ServiceName: info.ServiceName,
AppId: scr.opts.appId,
Version: scr.opts.versionRule,
Status: sc.MSInstanceUP,
})
if err != nil {
return fmt.Errorf("register service error: %w", err)
}
healthCheck := &discovery.HealthCheck{
Mode: "push",
Interval: 30,
Times: 3,
}
if scr.opts.heartbeatInterval > 0 {
healthCheck.Interval = scr.opts.heartbeatInterval
}
instanceId, err := scr.cli.RegisterMicroServiceInstance(&discovery.MicroServiceInstance{
ServiceId: serviceID,
Endpoints: []string{addr},
HostName: scr.opts.hostName,
HealthCheck: healthCheck,
Status: sc.MSInstanceUP,
Properties: info.Tags,
})
if err != nil {
return fmt.Errorf("register service instance error: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
go scr.heartBeat(ctx, serviceID, instanceId)
scr.lock.Lock()
defer scr.lock.Unlock()
scr.registryIns[instanceKey] = &scHeartbeat{
instanceKey: instanceKey,
cancel: cancel,
}
return nil
}
// Deregister a service or an instance
func (scr *serviceCombRegistry) Deregister(info *registry.Info) error {
err := scr.validRegistryInfo(info)
if err != nil {
return err
}
addr, err := scr.parseAddr(info.Addr.String())
if err != nil {
return err
}
serviceId, err := scr.cli.GetMicroServiceID(scr.opts.appId, info.ServiceName, scr.opts.versionRule, "")
if err != nil {
return fmt.Errorf("get service-id error: %w", err)
}
instanceKey := fmt.Sprintf("%s:%s", info.ServiceName, addr)
scr.lock.RLock()
insHeartbeat, ok := scr.registryIns[instanceKey]
scr.lock.RUnlock()
if !ok {
return fmt.Errorf("instance{%s} has not registered", instanceKey)
}
instanceId := ""
instances, err := scr.cli.FindMicroServiceInstances("", scr.opts.appId, info.ServiceName, scr.opts.versionRule, sc.WithoutRevision())
if err != nil {
return fmt.Errorf("get instances error: %w", err)
}
for _, instance := range instances {
if funk.ContainsString(instance.Endpoints, addr) {
instanceId = instance.InstanceId
}
}
if instanceId != "" {
// unregister is too slow to take effect, update status to down first.
_, err = scr.cli.UpdateMicroServiceInstanceStatus(serviceId, instanceId, sc.MSIinstanceDown)
if err != nil {
return fmt.Errorf("down service error: %w", err)
}
_, err = scr.cli.UnregisterMicroServiceInstance(serviceId, instanceId)
if err != nil {
return fmt.Errorf("deregister service error: %w", err)
}
}
scr.lock.Lock()
insHeartbeat.cancel()
delete(scr.registryIns, instanceKey)
scr.lock.Unlock()
return nil
}
func (scr *serviceCombRegistry) heartBeat(ctx context.Context, serviceId, instanceId string) {
ticker := time.NewTicker(time.Second * time.Duration(scr.opts.heartbeatInterval))
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
success, err := scr.cli.Heartbeat(serviceId, instanceId)
if err != nil || !success {
hlog.CtxErrorf(ctx, "HERTZ: beat to ServerComb return error:%+v instance:%v", err, instanceId)
ticker.Stop()
return
}
}
}
}
func (scr *serviceCombRegistry) validRegistryInfo(info *registry.Info) error {
if info == nil {
return errors.New("registry.Info can not be empty")
}
if info.ServiceName == "" {
return errors.New("registry.Info ServiceName can not be empty")
}
if info.Addr == nil {
return errors.New("registry.Info Addr can not be empty")
}
return nil
}
func (scr *serviceCombRegistry) parseAddr(s string) (string, error) {
host, port, err := net.SplitHostPort(s)
if err != nil {
return "", fmt.Errorf("parse addr error: %w", err)
}
if host == "" || host == "::" {
host = utils.LocalIP()
if host == utils.UNKNOWN_IP_ADDR {
return "", errors.New("get local ip error")
}
}
return net.JoinHostPort(host, port), nil
}