main.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package main
  2. import (
  3. "github.com/pkg/profile"
  4. "io/ioutil"
  5. "os"
  6. "rocommon"
  7. "rocommon/service"
  8. _ "rocommon/socket"
  9. _ "rocommon/socket/tcp"
  10. "roserver/baseserver"
  11. "roserver/baseserver/model"
  12. guildModel "roserver/guild/model"
  13. _ "roserver/guild/msg"
  14. "runtime"
  15. "strconv"
  16. "syscall"
  17. )
  18. //todo...
  19. // 单点有状态服务器
  20. func main() {
  21. //记录guild pid用来做关闭操作
  22. sysType := runtime.GOOS
  23. if sysType != "windows" {
  24. if pid := syscall.Getpid(); pid != 1 {
  25. fileName := "guild_server.pid" + strconv.Itoa(pid)
  26. ioutil.WriteFile(fileName, []byte(strconv.Itoa(pid)), 0777)
  27. defer os.Remove(fileName)
  28. }
  29. }
  30. //CPU
  31. //prof := profile.Start(profile.CPUProfile, profile.ProfilePath("./pprof/guild.pprof"), profile.NoShutdownHook)
  32. prof := profile.Start(profile.MemProfile, profile.ProfilePath("./pprof/guildmem.pprof"), profile.NoShutdownHook)
  33. baseserver.Init(model.SERVICE_NODE_TYPE_GUILD_STR, guildModel.ConfigInit, &guildModel.GuildUpdate{})
  34. //先建立服务器对应的连接,在监听客户端
  35. sConfig := service.GetServiceConfig()
  36. //配置文件初始化,如果后续做热更新的话,需要加锁,或者用sync.map
  37. //创建监听器
  38. var acceNode rocommon.ServerNode = nil
  39. if sConfig.Node.Addr != "" {
  40. acceNode = baseserver.CreateAcceptor(baseserver.ServiceParam{
  41. ServiceType: "tcpAcceptor",
  42. ServiceName: model.SERVICE_NODE_TYPE_GUILD_STR,
  43. ProcName: "common.backend",
  44. LisAddr: sConfig.Node.Addr,
  45. }, sConfig)
  46. }
  47. for _, concern := range sConfig.Node.Concern {
  48. //建立需要链接的服务器,可以通过服务器发现etcd来处理(包含在了CreateConnector中)
  49. baseserver.CreateConnector(baseserver.ServiceParam{
  50. DiscoveryServiceName: concern,
  51. DiscoveryServiceZone: sConfig.Node.Zone,
  52. ServiceType: "tcpConnector",
  53. ServiceName: model.SERVICE_NODE_TYPE_GUILD_STR,
  54. ProcName: "common.backend",
  55. })
  56. }
  57. baseserver.Wait()
  58. //CPU
  59. prof.Stop()
  60. baseserver.Exit(acceNode)
  61. }