-
Notifications
You must be signed in to change notification settings - Fork 2
/
start.go
397 lines (329 loc) · 9.77 KB
/
start.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package cmd
import (
"beluga/src/beluga/configuration_constant"
"beluga/src/beluga/helpers"
"beluga/src/beluga/library"
"beluga/src/beluga/task_constant"
web_server_helpers "beluga/src/web_server/helpers"
"beluga/src/web_server/models"
_ "beluga/src/web_server/routers"
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
_ "github.com/go-sql-driver/mysql"
"github.com/urfave/cli"
"golang.org/x/net/context"
"os"
"runtime"
"strconv"
"strings"
"time"
)
var Start = cli.Command{
Name: "start",
Usage: "启动服务",
Description: "服务启动",
Action: start,
Flags: []cli.Flag{
helpers.StringFlag("configDir, c", "conf/", "重定向配置文件路径"),
helpers.StringFlag("host", "0.0.0.0", "监听地址"),
helpers.StringFlag("port, p", "9410", "监听端口"),
},
}
var config_name = "app.conf" // 配置文件名
func start(c *cli.Context) {
if c.IsSet("configDir") {
config_path := c.String("configDir") + config_name
if err := beego.LoadAppConfig("ini", config_path); err != nil {
beego.Error("配置文件不存在。", config_path)
os.Exit(0)
}
} else {
if err := beego.LoadAppConfig("ini", "conf/"+config_name); err != nil {
beego.Error("当前路径下配置文件不存在")
os.Exit(0)
}
}
if c.IsSet("host") {
beego.BConfig.Listen.HTTPAddr = c.String("host")
}
if c.IsSet("port") {
beego.BConfig.Listen.HTTPPort, _ = strconv.Atoi(c.String("port"))
}
Run()
}
func Run() {
// 初始化线程
helpers.InitThreadNum(runtime.NumCPU())
// 初始化etcd
initEtcd()
// 初始化数据库
initDatabase()
// 判断中心是否存在,如果存在则直接退出,不存在则继续走
isMaster()
// TODO 如果断线的情况的话,定时检测
initConfNodeRegister()
initTaskNodeRegister()
// 监听配置节点
watchConfNode()
// 监听任务节点
watchTaskNode()
// 启动服务
beego.Run()
}
// 初始化数据库
func initDatabase() bool {
mysql_url := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s",
beego.AppConfig.DefaultString("db_username", "root"),
beego.AppConfig.DefaultString("db_password", "root"),
beego.AppConfig.DefaultString("db_host", "127.0.0.1"),
beego.AppConfig.DefaultInt("db_port", 3306),
beego.AppConfig.DefaultString("db_database", "beluga"),
beego.AppConfig.DefaultString("db_charset", "utf8"),
)
mysql_url += "&loc=Local"
if err := orm.RegisterDataBase("default", "mysql", mysql_url, 30); err != nil {
beego.Error("初始化mysql错误", err)
os.Exit(0)
return false
}
debug := beego.AppConfig.DefaultBool("debug", false)
if debug {
orm.Debug = true
}
orm.DefaultTimeLoc = time.Local
registerModel()
return true
}
// 注册模型
func registerModel() {
orm.RegisterModelWithPrefix(
web_server_helpers.GetTablePrefix(),
new(models.Account),
new(models.ConfigurationProject),
new(models.ConfigurationNamespace),
new(models.ConfigurationVersion),
new(models.ConfigurationLog),
new(models.Configuration),
new(models.ConfigurationOperation),
new(models.ConfigurationOperationLog),
new(models.ConfigurationNodeConf),
new(models.ConfigurationNode),
new(models.OperationLog),
new(models.Task),
new(models.TaskNode),
new(models.TaskLog),
)
}
// 判断中心服务是否存在。如果存在则禁止启动,保证中心接口服务的唯一性。
func isMaster() {
debug := beego.AppConfig.DefaultBool("debug", false)
if debug {
return
}
key := configuration_constant.BELUGA_MASTER_DIR
get_resp, err := library.G_conf_etcd_client.Kv.Get(context.TODO(), key)
if err != nil {
beego.Error("启动失败!")
os.Exit(0)
}
if len(get_resp.Kvs) != 0 {
beego.Error("中心接口服务存在。禁止启动。")
os.Exit(0)
}
go func() {
for {
// 创建租约
lease_grant_resp, err := library.G_conf_etcd_client.Lease.Grant(context.TODO(), 3)
if err != nil {
continue
}
// 自动续租
keep_alive_chan, err := library.G_conf_etcd_client.Lease.KeepAlive(context.TODO(), lease_grant_resp.ID)
if err != nil {
continue
}
// 数据写入
if _, err = library.G_conf_etcd_client.Kv.Put(context.TODO(), key, "", clientv3.WithLease(lease_grant_resp.ID)); err != nil {
continue
}
// 续租回应
for {
select {
case keep_alive_resq := <-keep_alive_chan:
if keep_alive_resq == nil {
break
}
}
}
time.Sleep(1 * time.Second)
}
}()
}
// 获取etcd节点配置
func initEtcd() {
etcd_conf_ip := beego.AppConfig.DefaultStrings("etcd_host", []string{})
etcd_conf_timeoute := beego.AppConfig.DefaultInt("etcd_timeoute", 5000)
if err := library.InitRegister(etcd_conf_ip, etcd_conf_timeoute); err != nil {
beego.Error("etcd链接失败", err)
os.Exit(0)
return
}
}
// 监控配置节点
func watchConfNode() {
key := configuration_constant.CONFIGURATION_REGISTER_DIR
watch_resp_chan := clientv3.NewWatcher(library.G_conf_etcd_client.Client).Watch(context.TODO(), key, clientv3.WithPrefix())
go func() {
for v := range watch_resp_chan {
if v.Err() != nil {
beego.Error(v.Err())
}
for _, resp := range v.Events {
key_spl := strings.Split(string(resp.Kv.Key), "/")
if len(key_spl) == 3 {
continue
}
node_conf_key := configuration_constant.NODE_CONF_DIR + key_spl[3]
switch resp.Type {
case mvccpb.PUT:
configuration_node_model := models.NewConfigurationNode()
configuration_node_model.Ip = key_spl[3]
configuration_node_model.CreateTime = time.Now()
configuration_node_model.IsDelete = 1
id, err := configuration_node_model.Save(orm.NewOrm())
if err != nil {
beego.Error(err, "节点写入失败")
}
// 更新节点中的配置数据
node_data := configuration_node_model.IdFind(orm.NewOrm(), id)
var conf_data []map[string]interface{}
var node_conf_etcd []byte
if node_data.NodeConfId != "" {
configuration_node_conf_model := models.NewConfigurationNodeConf()
node_conf_data := configuration_node_conf_model.IdsToData(orm.NewOrm(), node_data.NodeConfId)
for _, val := range node_conf_data {
var conf_map []map[string]interface{}
json.Unmarshal([]byte(val["conf"].(string)), &conf_map)
conf_data = append(conf_data, conf_map...)
}
node_conf_etcd, _ = json.Marshal(conf_data)
}
_, err = library.G_conf_etcd_client.Kv.Put(context.TODO(), node_conf_key, string(node_conf_etcd))
if err != nil {
beego.Error(err, "etcd节点配置写入失败")
}
break
case mvccpb.DELETE:
configuration_node_model := models.NewConfigurationNode()
configuration_node_model.Ip = key_spl[3]
configuration_node_model.IsDelete = 0
configuration_node_model.Edit(orm.NewOrm(), map[string]interface{}{
"is_delete": 0,
})
// 删除节点中的数据
_, err := library.G_conf_etcd_client.Kv.Delete(context.TODO(), node_conf_key)
if err != nil {
beego.Error(err, "etcd节点中数据删除失败")
}
break
}
}
}
}()
}
// 监控任务节点
func watchTaskNode() {
key := task_constant.TASK_REGISTER_DIR
watch_resp_chan := clientv3.NewWatcher(library.G_conf_etcd_client.Client).Watch(context.TODO(), key, clientv3.WithPrefix())
go func() {
for v := range watch_resp_chan {
if v.Err() != nil {
beego.Error(v.Err())
}
for _, resp := range v.Events {
key_spl := strings.Split(string(resp.Kv.Key), "/")
if len(key_spl) == 3 {
continue
}
switch resp.Type {
case mvccpb.PUT:
task_node_model := models.NewTaskNode()
task_node_model.Ip = key_spl[3]
task_node_model.CreateTime = time.Now()
task_node_model.IsDelete = 1
_, err := task_node_model.Save(orm.NewOrm())
if err != nil {
beego.Error("节点写入失败")
}
break
case mvccpb.DELETE:
task_node_model := models.NewTaskNode()
task_node_model.Ip = key_spl[3]
task_node_model.IsDelete = 0
task_node_model.Edit(orm.NewOrm(), map[string]interface{}{
"is_delete": 0,
})
break
}
}
}
}()
}
// 配置节点注册初始化
func initConfNodeRegister() {
var page int = 1
configuration_node_model := models.NewConfigurationNode()
for {
config_node_list := configuration_node_model.List(orm.NewOrm(), page, 10, "")
if len(config_node_list.List.([]orm.Params)) == 0 {
break
}
for _, val := range config_node_list.List.([]orm.Params) {
configuration_node_model.Ip = val["ip"].(string)
key := configuration_constant.CONFIGURATION_REGISTER_DIR + val["ip"].(string)
get_res, err := library.G_conf_etcd_client.Kv.Get(context.TODO(), key)
if err != nil {
beego.Error("节点数据初始化失败", err)
}
if get_res.Kvs == nil {
configuration_node_model.Edit(orm.NewOrm(), map[string]interface{}{"is_delete": 0})
} else {
for _ = range get_res.Kvs {
configuration_node_model.Edit(orm.NewOrm(), map[string]interface{}{"is_delete": 1})
}
}
}
page++
}
}
// 任务节点注册初始化
func initTaskNodeRegister() {
var page int = 1
task_node_model := models.NewTaskNode()
for {
config_node_list := task_node_model.List(orm.NewOrm(), page, 10, "")
if len(config_node_list.List.([]orm.Params)) == 0 {
break
}
for _, val := range config_node_list.List.([]orm.Params) {
task_node_model.Ip = val["ip"].(string)
key := task_constant.TASK_REGISTER_DIR + val["ip"].(string)
get_res, err := library.G_conf_etcd_client.Kv.Get(context.TODO(), key)
if err != nil {
beego.Error("节点数据初始化失败", err)
}
if get_res.Kvs == nil {
task_node_model.Edit(orm.NewOrm(), map[string]interface{}{"is_delete": 0})
} else {
for _ = range get_res.Kvs {
task_node_model.Edit(orm.NewOrm(), map[string]interface{}{"is_delete": 1})
}
}
}
page++
}
}