main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "rocommon"
  6. "rocommon/service"
  7. _ "rocommon/socket"
  8. _ "rocommon/socket/tcp"
  9. "roserver/baseserver"
  10. "roserver/baseserver/model"
  11. _ "roserver/battleboss/msg"
  12. rankModel "roserver/rank/model"
  13. _ "roserver/rank/msg"
  14. "runtime"
  15. "strconv"
  16. "syscall"
  17. )
  18. //todo...
  19. // 单点有状态服务器
  20. func main() {
  21. //记录battleboss pid用来做关闭操作
  22. sysType := runtime.GOOS
  23. if sysType != "windows" {
  24. if pid := syscall.Getpid(); pid != 1 {
  25. fileName := "rank_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/rank.pprof"), profile.NoShutdownHook)
  32. //prof := profile.Start(profile.MemProfile, profile.ProfilePath("./pprof/rankmem.pprof"), profile.NoShutdownHook)
  33. baseserver.Init(model.SERVICE_NODE_TYPE_RANK_STR, rankModel.ConfigInit, &rankModel.RankUpdate{})
  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_RANK_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_RANK_STR,
  54. ProcName: "common.backend",
  55. })
  56. }
  57. baseserver.Wait()
  58. //CPU
  59. //prof.Stop()
  60. baseserver.Exit(acceNode)
  61. }