cache.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package http
  2. import (
  3. "encoding/json"
  4. "net"
  5. "strconv"
  6. "strings"
  7. "go-common/app/admin/main/cache/model"
  8. "go-common/library/ecode"
  9. bm "go-common/library/net/http/blademaster"
  10. yaml "gopkg.in/yaml.v2"
  11. )
  12. // @params ClustersReq
  13. // @router get /x/admin/cache/clusters
  14. // @response ClustersResp
  15. func clusters(ctx *bm.Context) {
  16. req := new(model.ClusterReq)
  17. if err := ctx.Bind(req); err != nil {
  18. return
  19. }
  20. req.Cookie = ctx.Request.Header.Get("Cookie")
  21. ctx.JSON(srv.Clusters(ctx, req))
  22. }
  23. // @params AddClusterReq
  24. // @router post /x/admin/cache/cluster/add
  25. // @response EmpResp
  26. func addCluster(ctx *bm.Context) {
  27. req := new(model.AddClusterReq)
  28. if err := ctx.Bind(req); err != nil {
  29. return
  30. }
  31. ctx.JSON(srv.AddCluster(ctx, req))
  32. }
  33. // @params delClusterReq
  34. // @router post /x/admin/cache/cluster/del
  35. // @response EmpResp
  36. func delCluster(ctx *bm.Context) {
  37. req := new(model.DelClusterReq)
  38. if err := ctx.Bind(req); err != nil {
  39. return
  40. }
  41. ctx.JSON(srv.DelCluster(ctx, req))
  42. }
  43. // @params ClusterReq
  44. // @router get /x/admin/cache/cluster
  45. // @response []Cluster
  46. func cluster(ctx *bm.Context) {
  47. req := new(model.ClusterReq)
  48. if err := ctx.Bind(req); err != nil {
  49. return
  50. }
  51. if req.AppID != "" {
  52. resp, err := srv.Cluster(ctx, req)
  53. if err != nil {
  54. ctx.JSON(nil, err)
  55. return
  56. }
  57. ctx.JSON(&model.ClusterResp{Clusters: resp}, nil)
  58. } else {
  59. req.Cookie = ctx.Request.Header.Get("Cookie")
  60. ctx.JSON(srv.Clusters(ctx, req))
  61. }
  62. }
  63. // @params ClusterDtlReq
  64. // @router get /x/admin/cache/cluster/detail
  65. // @response ClusterDtlResp
  66. func clusterDtl(ctx *bm.Context) {
  67. req := new(model.ClusterDtlReq)
  68. if err := ctx.Bind(req); err != nil {
  69. return
  70. }
  71. ctx.JSON(srv.ClusterDtl(ctx, req))
  72. }
  73. // @params ModifyClusterReq
  74. // @router post /x/admin/cache/cluster/modify
  75. // @response EmpResp
  76. func modifyCluster(ctx *bm.Context) {
  77. req := new(model.ModifyClusterReq)
  78. if err := ctx.Bind(req); err != nil {
  79. return
  80. }
  81. ctx.JSON(srv.ModifyCluster(ctx, req))
  82. }
  83. func toml(ctx *bm.Context) {
  84. req := new(model.ClusterReq)
  85. if err := ctx.Bind(req); err != nil {
  86. return
  87. }
  88. resp, err := srv.Toml(ctx, req)
  89. if err != nil {
  90. ctx.Status(500)
  91. return
  92. }
  93. ctx.Writer.Write(resp)
  94. }
  95. // @params addFromYml
  96. // @router post /x/admin/cache/cluster/from/yml
  97. // @response EmpResp
  98. func addFromYml(ctx *bm.Context) {
  99. req := new(model.ClusterFromYml)
  100. if err := ctx.Bind(req); err != nil {
  101. ctx.JSONMap(map[string]interface{}{
  102. "message": "参数有问题:app_id,zone,tw_yml",
  103. }, ecode.RequestErr)
  104. return
  105. }
  106. type server struct {
  107. AutoEjectHosts bool `yaml:"auto_eject_hosts"`
  108. Backlog int `yaml:"backlog"`
  109. Distribution string `yaml:"distribution"`
  110. Hash string `yaml:"hash"`
  111. Listen string `yaml:"listen"`
  112. Preconnect bool `yaml:"preconnect"`
  113. Timeout int `yaml:"timeout"`
  114. Redis bool `yaml:"redis"`
  115. ServerConnections int `yaml:"server_connections"`
  116. ServerFailureLimit int `yaml:"server_failure_limit"`
  117. ServerRetryTimeout int `yaml:"server_retry_timeout"`
  118. Servers []string `yaml:"servers"`
  119. }
  120. type node struct {
  121. Addr string `json:"addr"`
  122. Weigth int64 `json:"weight"`
  123. Alias string `json:"alias"`
  124. }
  125. confs := make(map[string]server)
  126. err := yaml.Unmarshal([]byte(req.TwYml), &confs)
  127. if err != nil {
  128. ctx.JSONMap(map[string]interface{}{
  129. "message": "解析twemproxy.yml文件失败",
  130. }, ecode.RequestErr)
  131. return
  132. }
  133. mcPort := 11211
  134. rdPort := 26379
  135. for name, conf := range confs {
  136. ctp := "memcache"
  137. if conf.Redis {
  138. ctp = "redis"
  139. }
  140. if conf.Hash != "fnv1a_64" {
  141. ctx.JSONMap(map[string]interface{}{
  142. "message": "不支持除了fnv1a_64之外的hash方法",
  143. }, ecode.RequestErr)
  144. return
  145. }
  146. addr := "0.0.0.0:"
  147. _, port, err := net.SplitHostPort(conf.Listen)
  148. if err == nil {
  149. addr = addr + port
  150. } else {
  151. if conf.Redis {
  152. addr = addr + strconv.Itoa(rdPort)
  153. rdPort++
  154. } else {
  155. addr = addr + strconv.Itoa(mcPort)
  156. mcPort++
  157. }
  158. }
  159. clst := &model.AddClusterReq{
  160. Type: ctp,
  161. AppID: req.AppID,
  162. Zone: req.Zone,
  163. HashMethod: "fnv1a_64",
  164. HashDistribution: "ketama",
  165. HashTag: "",
  166. Name: name,
  167. DailTimeout: 1000,
  168. ReadTimeout: 1000,
  169. WriteTimeout: 1000,
  170. NodeConn: 2,
  171. PingFailLimit: 3,
  172. PingAutoEject: true,
  173. ListenProto: "tcp",
  174. ListenAddr: addr,
  175. }
  176. if _, err := srv.AddCluster(ctx, clst); err != nil {
  177. ctx.JSONMap(map[string]interface{}{
  178. "message": "添加cluster失败:" + name + " " + err.Error(),
  179. }, ecode.RequestErr)
  180. return
  181. }
  182. var nodes []*node
  183. for _, n := range conf.Servers {
  184. ss := strings.Split(n, " ")
  185. idx := strings.LastIndex(ss[0], ":")
  186. weight, _ := strconv.ParseInt(ss[0][idx+1:], 10, 64)
  187. nodes = append(nodes, &node{Addr: ss[0][:idx], Weigth: weight, Alias: ss[1]})
  188. }
  189. ns, _ := json.Marshal(nodes)
  190. cn := &model.ModifyClusterReq{
  191. Name: name,
  192. Action: 1,
  193. Nodes: string(ns),
  194. }
  195. if _, err := srv.ModifyCluster(ctx, cn); err != nil {
  196. ctx.JSONMap(map[string]interface{}{
  197. "message": "添加cluster node失败:" + name + " " + err.Error(),
  198. }, ecode.RequestErr)
  199. return
  200. }
  201. }
  202. }