etcddesc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "rocommon"
  7. "strconv"
  8. "strings"
  9. )
  10. type ETCDServiceDesc struct {
  11. Name string
  12. ID string
  13. Host string
  14. Port int
  15. Tags []string
  16. Type int
  17. Zone int
  18. Index int
  19. RegTime int64 //注册时的时间单位为m
  20. LocalAddr string
  21. }
  22. func (this *ETCDServiceDesc) String() string {
  23. data, err := json.Marshal(this)
  24. if err != nil {
  25. return ""
  26. }
  27. return string(data)
  28. }
  29. //字符串形式返回服务器ID 例如:server/zone/gate#type@zone@index -> gate#1@1@1
  30. func GenServiceID(prop rocommon.ServerNodeProperty) string {
  31. return fmt.Sprintf("%s#%d@%d@%d",
  32. prop.GetName(),
  33. prop.GetZone(),
  34. prop.ServerType(),
  35. prop.GetIndex(),
  36. )
  37. }
  38. func GenSelfServiceID(prop configServerNode) string {
  39. return fmt.Sprintf("%s#%d@%d@%d",
  40. serviceName,
  41. prop.Zone,
  42. prop.Type,
  43. prop.Id,
  44. )
  45. }
  46. func GenDiscoveryServicePrefix(sName string, zone int) string {
  47. if zone > 0 {
  48. return "server/" + sName + "#" + strconv.Itoa(zone)
  49. } else {
  50. return "server/" + sName + "#"
  51. }
  52. }
  53. func GenServicePrefix(sName string, zone int) string {
  54. return "server/" + sName
  55. //return "server/" + strconv.Itoa(zone) + "/" + sName
  56. }
  57. func GenServiceZonePrefix(zone int) string {
  58. return "server/" + strconv.Itoa(zone)
  59. }
  60. func GenService(sKey string) string {
  61. keyList := strings.Split(sKey, "/")
  62. if len(keyList) >= 2 {
  63. return keyList[1]
  64. }
  65. return ""
  66. }
  67. func GenServiceStatePrefix(prop configServerNode) string {
  68. id := fmt.Sprintf("%s#%d@%d@%d",
  69. serviceName,
  70. prop.Zone,
  71. prop.Type,
  72. prop.Id,
  73. )
  74. return "serverstate/" + id
  75. }
  76. func str2Num(str string) (int, error) {
  77. num, e := strconv.ParseInt(str, 10, 32)
  78. if e != nil {
  79. return 0, errors.New("serviceId invalid num convert error:" + str)
  80. } else {
  81. return int(num), nil
  82. }
  83. }
  84. func ParseServiceID(sid string) (serverType, zone, index int, err error) {
  85. str := strings.Split(sid, "#")
  86. if len(str) < 2 {
  87. err = errors.New("serviceId invalid:" + sid)
  88. return
  89. } else {
  90. //serviceName := str[0]
  91. strProp := strings.Split(str[1], "@")
  92. if len(strProp) < 3 {
  93. err = errors.New("serviceId invalid:" + sid)
  94. return
  95. } else {
  96. zone, err = str2Num(strProp[0])
  97. if err != nil {
  98. return
  99. }
  100. serverType, err = str2Num(strProp[1])
  101. if err != nil {
  102. return
  103. }
  104. index, err = str2Num(strProp[2])
  105. if err != nil {
  106. return
  107. }
  108. }
  109. }
  110. return
  111. }
  112. type CrossMapServerStateInfoDesc struct {
  113. LineNum int32 //线路编号
  114. Num int32 //线路人数
  115. }
  116. type ETCDServiceState struct {
  117. ID int
  118. SID string
  119. MaxLineNum int32 //每个服务器线路最大数量
  120. StateList []CrossMapServerStateInfoDesc
  121. SpaceMaxNum int32 //每个线路最大在线人数
  122. }
  123. func (this ETCDServiceState) String() string {
  124. data, err := json.Marshal(this)
  125. if err != nil {
  126. return ""
  127. }
  128. return string(data)
  129. }