-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
141 lines (124 loc) · 3.81 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"github.com/Runner-Go-Team/RunnerGo-engine-open/middlewares"
"github.com/Runner-Go-Team/RunnerGo-engine-open/model"
"github.com/Runner-Go-Team/RunnerGo-engine-open/tools"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/Runner-Go-Team/RunnerGo-engine-open/config"
"github.com/Runner-Go-Team/RunnerGo-engine-open/initialize"
"github.com/Runner-Go-Team/RunnerGo-engine-open/log"
"github.com/Runner-Go-Team/RunnerGo-engine-open/server/heartbeat"
_ "net/http/pprof"
)
var (
GinRouter *gin.Engine
mode int
)
func initService() {
// 初始化配置文件
if mode == 0 {
// 读取配置文件方式
config.InitConfig()
} else {
// 读取环境变量方式
config.EnvInitConfig()
}
// 初始化logger
zap.S().Debug("初始化logger")
log.InitLogger()
// 获取本机地址
heartbeat.InitLocalIp()
// 初始化redis客户端
log.Logger.Info(fmt.Sprintf("机器ip:%s,初始化redis客户端", middlewares.LocalIp))
if err := model.InitRedisClient(
config.Conf.ReportRedis.Address,
config.Conf.ReportRedis.Password,
config.Conf.ReportRedis.DB,
config.Conf.Redis.Address,
config.Conf.Redis.Password,
config.Conf.Redis.DB,
); err != nil {
log.Logger.Error(fmt.Sprintf("机器ip:%s, redis连接失败:", middlewares.LocalIp), err)
panic("redis 连接失败")
return
}
//3. 初始化routers
log.Logger.Debug(fmt.Sprintf("机器ip:%s, 初始化routers", middlewares.LocalIp))
GinRouter = initialize.Routers()
// 语言转换
if err := initialize.InitTrans("zh"); err != nil {
log.Logger.Error(fmt.Sprintf("机器ip:%s", middlewares.LocalIp), err)
}
// 注册服务
log.Logger.Debug(fmt.Sprintf("机器ip:%s, 注册服务", middlewares.LocalIp))
kpRunnerService := &http.Server{
Addr: config.Conf.Http.Address,
Handler: GinRouter,
MaxHeaderBytes: 1 << 20,
}
// 初始化全局函数
tools.InitPublicFunc()
go func() {
if err := kpRunnerService.ListenAndServe(); err != nil {
log.Logger.Error(fmt.Sprintf("机器ip:%s, kpRunnerService:", middlewares.LocalIp), err)
return
}
}()
//runtime.SetBlockProfileRate(1) // 开启对阻塞操作的跟踪,block
//runtime.SetMutexProfileFraction(1) // 开启对锁调用的跟踪,mutex
//// pprof 监控
//go func() {
// pprof.Register(GinRouter)
// GinRouter.Run(":8003")
//}()
// 注册并发送心跳数据
field := middlewares.LocalIp + "_" + fmt.Sprintf("%d", config.Conf.Heartbeat.Port) + "_" + config.Conf.Heartbeat.Region
go func() {
//heartbeat.SendHeartBeat(config.Conf.Heartbeat.GrpcHost, config.Conf.Heartbeat.Duration)
heartbeat.SendHeartBeatRedis(field, config.Conf.Heartbeat.Duration)
}()
// 资源监控数据
go func() {
heartbeat.SendMachineResources(config.Conf.Heartbeat.Resources)
}()
/// 接收终止信号
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Logger.Info(fmt.Sprintf("机器ip:%s, 注销成功", middlewares.LocalIp))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := kpRunnerService.Shutdown(ctx); err != nil {
log.Logger.Info(fmt.Sprintf("机器ip:%s, 注销成功", middlewares.LocalIp))
}
}
func main() {
flag.IntVar(&mode, "m", 0, "读取环境变量还是读取配置文件")
flag.Parse()
// 性能分析
//_, err := profiler.Start(
// profiler.Config{
// ApplicationName: "RunnerGo-engine-open",
// ServerAddress: "http://192.168.1.205:4040/",
// ProfileTypes: []profiler.ProfileType{
// profiler.ProfileCPU,
// profiler.ProfileAllocObjects,
// profiler.ProfileAllocSpace,
// profiler.ProfileInuseObjects,
// profiler.ProfileInuseSpace,
// },
// })
//if err != nil {
// log.Logger.Error("监控信息出错: ", err.Error())
//}
initService()
}