main.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "io/ioutil"
  4. _ "net/http/pprof"
  5. "os"
  6. "rocommon"
  7. "rocommon/service"
  8. _ "rocommon/socket"
  9. _ "rocommon/socket/tcp"
  10. "roserver/baseserver"
  11. "roserver/baseserver/model"
  12. self "roserver/battleboss/model"
  13. _ "roserver/battleboss/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 := "battleboss_server.pid" + strconv.Itoa(pid)
  26. ioutil.WriteFile(fileName, []byte(strconv.Itoa(pid)), 0777)
  27. defer os.Remove(fileName)
  28. }
  29. }
  30. //go func(){
  31. // log.Println(http.ListenAndServe("localhost:8005", nil))
  32. //}()
  33. //CPU
  34. //prof := profile.Start(profile.CPUProfile, profile.ProfilePath("./pprof/serverboss.pprof"), profile.NoShutdownHook)
  35. baseserver.Init(model.SERVICE_NODE_TYPE_BOSS_STR, self.ConfigInit, &self.BossUpdate{})
  36. //prof := profile.Start(profile.MemProfile, profile.ProfilePath("./pprof/serverbossmem.pprof"), profile.NoShutdownHook)
  37. sConfig := service.GetServiceConfig()
  38. //先建立服务器对应的连接,在监听客户端
  39. //创建监听器
  40. var acceNode rocommon.ServerNode = nil
  41. if sConfig.Node.Addr != "" {
  42. acceNode = baseserver.CreateAcceptor(baseserver.ServiceParam{
  43. ServiceType: "tcpAcceptor",
  44. ServiceName: model.SERVICE_NODE_TYPE_BOSS_STR,
  45. ProcName: "common.backend",
  46. LisAddr: sConfig.Node.Addr,
  47. }, sConfig)
  48. }
  49. for _, concern := range sConfig.Node.Concern {
  50. //建立需要链接的服务器,可以通过服务器发现etcd来处理(包含在了CreateConnector中)
  51. baseserver.CreateConnector(baseserver.ServiceParam{
  52. DiscoveryServiceName: concern,
  53. DiscoveryServiceZone: sConfig.Node.Zone,
  54. ServiceType: "tcpConnector",
  55. ServiceName: model.SERVICE_NODE_TYPE_BOSS_STR,
  56. ProcName: "common.backend",
  57. })
  58. }
  59. baseserver.Wait()
  60. //CPU
  61. //prof.Stop()
  62. baseserver.Exit(acceNode)
  63. }