68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package main
|
||
|
||
import (
|
||
"github.com/pkg/profile"
|
||
"io/ioutil"
|
||
"os"
|
||
"rocommon"
|
||
"rocommon/service"
|
||
_ "rocommon/socket"
|
||
_ "rocommon/socket/tcp"
|
||
"roserver/baseserver"
|
||
"roserver/baseserver/model"
|
||
guildModel "roserver/guild/model"
|
||
_ "roserver/guild/msg"
|
||
"runtime"
|
||
"strconv"
|
||
"syscall"
|
||
)
|
||
|
||
//todo...
|
||
// 单点有状态服务器
|
||
func main() {
|
||
//记录guild pid用来做关闭操作
|
||
sysType := runtime.GOOS
|
||
if sysType != "windows" {
|
||
if pid := syscall.Getpid(); pid != 1 {
|
||
fileName := "guild_server.pid" + strconv.Itoa(pid)
|
||
ioutil.WriteFile(fileName, []byte(strconv.Itoa(pid)), 0777)
|
||
defer os.Remove(fileName)
|
||
}
|
||
}
|
||
//CPU
|
||
//prof := profile.Start(profile.CPUProfile, profile.ProfilePath("./pprof/guild.pprof"), profile.NoShutdownHook)
|
||
prof := profile.Start(profile.MemProfile, profile.ProfilePath("./pprof/guildmem.pprof"), profile.NoShutdownHook)
|
||
|
||
baseserver.Init(model.SERVICE_NODE_TYPE_GUILD_STR, guildModel.ConfigInit, &guildModel.GuildUpdate{})
|
||
|
||
//先建立服务器对应的连接,在监听客户端
|
||
sConfig := service.GetServiceConfig()
|
||
//配置文件初始化,如果后续做热更新的话,需要加锁,或者用sync.map
|
||
//创建监听器
|
||
var acceNode rocommon.ServerNode = nil
|
||
if sConfig.Node.Addr != "" {
|
||
acceNode = baseserver.CreateAcceptor(baseserver.ServiceParam{
|
||
ServiceType: "tcpAcceptor",
|
||
ServiceName: model.SERVICE_NODE_TYPE_GUILD_STR,
|
||
ProcName: "common.backend",
|
||
LisAddr: sConfig.Node.Addr,
|
||
}, sConfig)
|
||
}
|
||
|
||
for _, concern := range sConfig.Node.Concern {
|
||
//建立需要链接的服务器,可以通过服务器发现etcd来处理(包含在了CreateConnector中)
|
||
baseserver.CreateConnector(baseserver.ServiceParam{
|
||
DiscoveryServiceName: concern,
|
||
DiscoveryServiceZone: sConfig.Node.Zone,
|
||
ServiceType: "tcpConnector",
|
||
ServiceName: model.SERVICE_NODE_TYPE_GUILD_STR,
|
||
ProcName: "common.backend",
|
||
})
|
||
}
|
||
|
||
baseserver.Wait()
|
||
//CPU
|
||
prof.Stop()
|
||
baseserver.Exit(acceNode)
|
||
}
|