-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul.go
80 lines (66 loc) · 1.93 KB
/
consul.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
package registrycenter
import (
"context"
"errors"
"fmt"
"github.com/duanshanghanqing/rocket/registry"
consulApi "github.com/hashicorp/consul/api"
)
type ConsulRegisterCenter struct {
client *consulApi.Client
clientConfig *consulApi.Config
}
type ConsulRegisterCenterOptions func(o *ConsulRegisterCenter)
func WithConsulRegisterCenterClientConfig(clientConfig *consulApi.Config) ConsulRegisterCenterOptions {
return func(o *ConsulRegisterCenter) {
o.clientConfig = clientConfig
}
}
func NewConsulRegisterCenter(opts ...ConsulRegisterCenterOptions) (registry.IRegistrar, error) {
r := &ConsulRegisterCenter{
clientConfig: consulApi.DefaultConfig(),
}
for _, o := range opts {
o(r)
}
client, err := consulApi.NewClient(r.clientConfig)
if err != nil {
return nil, err
}
r.client = client
return r, err
}
func (r *ConsulRegisterCenter) Register(ctx context.Context, service *registry.ServiceRegisterInfo) error {
if service == nil {
return errors.New("ServiceRegisterInfo is nil")
}
// 1.Registration Service Information
reg := consulApi.AgentServiceRegistration{}
reg.ID = service.ID
reg.Name = service.Name
reg.Address = service.Host
reg.Port = service.Port
reg.Tags = service.Tags
// 2.Set up health check ups
check := &consulApi.AgentServiceCheck{
HTTP: fmt.Sprintf("http://%s:%d/health", reg.Address, reg.Port),
Timeout: "2s",
Interval: "5s",
DeregisterCriticalServiceAfter: "30s",
}
// 3.Add check
reg.Check = check
fmt.Println(reg.Check.HTTP)
// 4.Registration Services
err := r.client.Agent().ServiceRegister(®)
if err != nil {
return err
}
return nil
}
func (r *ConsulRegisterCenter) Deregister(ctx context.Context, service *registry.ServiceRegisterInfo) error {
if service == nil {
return errors.New("ServiceRegisterInfo is nil")
}
return r.client.Agent().ServiceDeregister(service.ID) // Unregister service
}