-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
173 lines (141 loc) · 3.8 KB
/
client.go
File metadata and controls
173 lines (141 loc) · 3.8 KB
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
package eureka
import (
"fmt"
"github.com/phpdragon/go-eureka-client/config"
"github.com/phpdragon/go-eureka-client/core"
"github.com/phpdragon/go-eureka-client/logger"
"go.uber.org/atomic"
"go.uber.org/zap"
"os"
"os/signal"
"sync"
"syscall"
)
const (
defaultSleepIntervals = 3
//
httpPrefix = "http://"
httpsPrefix = "https://"
//
httpKey = 0
httpsKey = 1
)
type Client struct {
Running bool
//
apiClient *core.EurekaServerApi
//自增器
autoIncr *atomic.Int64
// for monitor system signal
signalChan chan os.Signal
//日志对象
logger *logger.Logger
mutex sync.RWMutex
config *config.Config
// current client (instance) config
instance *core.Instance
// applications registry
// key: appId
// value: Application
registryAppMap map[string]*core.Application
// instances registry
// key: appId
// value:
// key: int(0...n)
// value: InstanceConfig
activeInstanceMap map[string]map[int]*core.Instance
// instance real url map
// key: appId
// value:
// key: int(http:0, https:1)
// value:
// key: int(0...n)
// value: real url
activeServiceIpPortMap map[string]map[int]map[int]string
}
func NewClient(configPath string) *Client {
return NewClientWithLog(configPath, nil)
}
func NewClientWithLog(configPath string, zapLog *zap.SugaredLogger) *Client {
log := logger.NewLogAgent(zapLog)
eurekaConfig, err := config.LoadConfig(configPath, false)
if err != nil {
log.Error(fmt.Sprintf("LoadConfig %s failed, err=%s", configPath, err.Error()))
os.Exit(1)
}
//实例化
instanceInfo, err := config.NewInstance(eurekaConfig)
if err != nil {
log.Error(fmt.Sprintf("NewInstance %s failed, err=%s", eurekaConfig.InstanceConfig.AppName, err.Error()))
os.Exit(1)
}
client := &Client{
//自增器
autoIncr: atomic.NewInt64(0),
logger: log,
signalChan: make(chan os.Signal),
//
config: eurekaConfig,
instance: instanceInfo,
}
api, err := client.Api()
if err != nil {
client.logger.Error(fmt.Sprintf("Failed to get EurekaServerApi instance, err=%s", err.Error()))
os.Exit(1)
}
client.apiClient = api
return client
}
func (client *Client) Run() {
client.mutex.Lock()
client.Running = true
client.mutex.Unlock()
// handle exit signal to de-register instance
go client.handleSignal()
// (if FetchRegistry is true), fetch registry apps periodically
// and update to t.registryAppMap
go client.refreshRegistry()
client.registerWithEureka()
}
func (client *Client) Shutdown() {
//client在shutdown情况下,是否显示从注册中心注销
if !client.Running || !client.config.ClientConfig.ShouldUnregisterOnShutdown {
return
}
client.logger.Info(fmt.Sprintf("Receive exit signal, client instance going to de-register, instanceId=%s.", client.instance.InstanceId))
// de-register instance
err := client.apiClient.DeRegisterInstance(client.instance.App, client.instance.InstanceId)
if err != nil {
client.logger.Error(fmt.Sprintf("Failed to de-register %s, err=%s", client.instance.InstanceId, err.Error()))
return
}
client.mutex.Lock()
client.Running = false
client.mutex.Unlock()
client.logger.Info(fmt.Sprintf("de-register %s success.", client.instance.InstanceId))
}
// for graceful kill. Here handle SIGTERM signal to do sth
// e.g: kill -TERM $pid
//
// or "ctrl + c" to exit
func (client *Client) handleSignal() {
if client.signalChan == nil {
client.signalChan = make(chan os.Signal)
}
signal.Notify(client.signalChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
for {
switch <-client.signalChan {
case syscall.SIGINT:
fallthrough
case syscall.SIGKILL:
fallthrough
case syscall.SIGHUP:
fallthrough
case syscall.SIGQUIT:
fallthrough
case syscall.SIGTERM:
client.logger.Info(fmt.Sprintf("syscall kill, instanceId=%s.", client.instance.InstanceId))
client.Shutdown()
}
}
}