forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
application.go
235 lines (210 loc) · 6.12 KB
/
application.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
//服务端启动初始化,解析命令行参数,解析配置
package tars
import (
"flag"
"fmt"
"net/http"
"os"
"sync"
"time"
"tars/protocol/res/adminf"
"tars/transport"
"tars/util/conf"
"tars/util/endpoint"
"tars/util/rogger"
"tars/util/tools"
)
var tarsConfig map[string]*transport.TarsServerConf
var goSvrs map[string]*transport.TarsServer
var httpSvrs map[string]*http.Server
var shutdown chan bool
var serList []string
var objRunList []string
//TLOG is the logger for tars framework.
var TLOG = rogger.GetLogger("TLOG")
var initOnce sync.Once
type adminFn func(string) (string, error)
var adminMethods map[string]adminFn
func init() {
tarsConfig = make(map[string]*transport.TarsServerConf)
goSvrs = make(map[string]*transport.TarsServer)
httpSvrs = make(map[string]*http.Server)
shutdown = make(chan bool, 1)
adminMethods = make(map[string]adminFn)
rogger.SetLevel(rogger.ERROR)
// Remove flag.Parse() from init, due to https://github.com/golang/go/issues/31859
// Disable call Init() here
// Init()
}
//Init should run before GetServerConfig & GetClientConfig , or before run
// and Init should be only run once
func Init() {
initOnce.Do(initConfig)
}
func initConfig() {
confPath := flag.String("config", "", "init config path")
flag.Parse()
if len(*confPath) == 0 {
return
}
c, err := conf.NewConf(*confPath)
if err != nil {
TLOG.Error("open app config fail")
}
//Config.go
//Server
svrCfg = new(serverConfig)
if c.GetString("/tars/application<enableset>") == "Y" {
svrCfg.Enableset = true
svrCfg.Setdivision = c.GetString("/tars/application<setdivision>")
}
sMap := c.GetMap("/tars/application/server")
svrCfg.Node = sMap["node"]
svrCfg.App = sMap["app"]
svrCfg.Server = sMap["server"]
svrCfg.LocalIP = sMap["localip"]
//svrCfg.Container = c.GetString("/tars/application<container>")
//init log
svrCfg.LogPath = sMap["logpath"]
svrCfg.LogSize = tools.ParseLogSizeMb(sMap["logsize"])
svrCfg.LogNum = tools.ParseLogNum(sMap["lognum"])
svrCfg.LogLevel = sMap["logLevel"]
svrCfg.config = sMap["config"]
svrCfg.notify = sMap["notify"]
svrCfg.BasePath = sMap["basepath"]
svrCfg.DataPath = sMap["datapath"]
//svrCfg.netThread = sMap["netthread"]
svrCfg.netThread = c.GetInt("/tars/application/server<netthread>")
svrCfg.log = sMap["log"]
//add version info
svrCfg.Version = TarsVersion
//add adapters config
svrCfg.Adapters = make(map[string]adapterConfig)
rogger.SetLevel(rogger.StringToLevel(svrCfg.LogLevel))
TLOG.SetFileRoller(svrCfg.LogPath+"/"+svrCfg.App+"/"+svrCfg.Server, 10, 100)
//client
cltCfg = new(clientConfig)
cMap := c.GetMap("/tars/application/client")
cltCfg.Locator = cMap["locator"]
cltCfg.stat = cMap["stat"]
cltCfg.property = cMap["property"]
cltCfg.AsyncInvokeTimeout = c.GetInt("/tars/application/client<async-invoke-timeout>")
cltCfg.refreshEndpointInterval = c.GetInt("/tars/application/client<refresh-endpoint-interval>")
serList = c.GetDomain("/tars/application/server")
for _, adapter := range serList {
endString := c.GetString("/tars/application/server/" + adapter + "<endpoint>")
end := endpoint.Parse(endString)
svrObj := c.GetString("/tars/application/server/" + adapter + "<servant>")
protocol := c.GetString("/tars/application/server/" + adapter + "<protocol>")
threads := c.GetInt("/tars/application/server/" + adapter + "<threads>")
svrCfg.Adapters[adapter] = adapterConfig{end, protocol, svrObj, threads}
host := end.Host
if end.Bind != "" {
host = end.Bind
}
conf := &transport.TarsServerConf{
Proto: end.Proto,
Address: fmt.Sprintf("%s:%d", host, end.Port),
MaxInvoke: int32(MaxInvoke),
AcceptTimeout: AcceptTimeout,
ReadTimeout: ReadTimeout,
WriteTimeout: WriteTimeout,
HandleTimeout: HandleTimeout,
IdleTimeout: IdleTimeout,
TCPNoDelay: TCPNoDelay,
TCPReadBuffer: TCPReadBuffer,
TCPWriteBuffer: TCPWriteBuffer,
}
tarsConfig[svrObj] = conf
}
TLOG.Debug("config add ", tarsConfig)
localString := c.GetString("/tars/application/server<local>")
localpoint := endpoint.Parse(localString)
adminCfg := &transport.TarsServerConf{
Proto: "tcp",
Address: fmt.Sprintf("%s:%d", localpoint.Host, localpoint.Port),
MaxInvoke: int32(MaxInvoke),
AcceptTimeout: AcceptTimeout,
ReadTimeout: ReadTimeout,
WriteTimeout: WriteTimeout,
HandleTimeout: HandleTimeout,
IdleTimeout: IdleTimeout,
QueueCap: QueueCap,
TCPNoDelay: TCPNoDelay,
TCPReadBuffer: TCPReadBuffer,
TCPWriteBuffer: TCPWriteBuffer,
}
tarsConfig["AdminObj"] = adminCfg
svrCfg.Adapters["AdminAdapter"] = adapterConfig{localpoint, "tcp", "AdminObj", 1}
go initReport()
}
//Run the application
func Run() {
Init()
<-statInited
// add adminF
adf := new(adminf.AdminF)
ad := new(Admin)
AddServant(adf, ad, "AdminObj")
for _, obj := range objRunList {
if s, ok := httpSvrs[obj]; ok {
go func(obj string) {
fmt.Println(obj, "http server start")
err := s.ListenAndServe()
if err != nil {
fmt.Println(obj, "server start failed", err)
os.Exit(1)
}
}(obj)
continue
}
s := goSvrs[obj]
if s == nil {
TLOG.Debug("Obj not found", obj)
break
}
TLOG.Debug("Run", obj, s.GetConfig())
go func(obj string) {
err := s.Serve()
if err != nil {
fmt.Println(obj, "server start failed", err)
os.Exit(1)
}
}(obj)
}
go reportNotifyInfo("restart")
mainloop()
}
func mainloop() {
ha := new(NodeFHelper)
comm := NewCommunicator()
//comm.SetProperty("netthread", 1)
node := GetServerConfig().Node
app := GetServerConfig().App
server := GetServerConfig().Server
//container := GetServerConfig().Container
ha.SetNodeInfo(comm, node, app, server)
go ha.ReportVersion(GetServerConfig().Version)
go ha.KeepAlive("") //first start
loop := time.NewTicker(MainLoopTicker)
for {
select {
case <-shutdown:
reportNotifyInfo("stop")
return
case <-loop.C:
for name, adapter := range svrCfg.Adapters {
if adapter.Protocol == "not_tars" {
//TODO not_tars support
ha.KeepAlive(name)
continue
}
if s, ok := goSvrs[adapter.Obj]; ok {
if !s.IsZombie(ZombileTimeout) {
ha.KeepAlive(name)
}
}
}
}
}
}