reload.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. local skynet = require "skynet"
  2. local codecache = require "skynet.codecache"
  3. require "skynet.manager"
  4. local logger = require "logger"
  5. local stringify = require "stringify"
  6. local cjson = require "cjson"
  7. local md5 = require "md5"
  8. local assetcenter
  9. local watchdog
  10. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  11. -- Generate request data
  12. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  13. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  14. local whitelist = {
  15. ["47.99.157.83"] = true,
  16. }
  17. local updateserver
  18. local function newserver()
  19. local newnum = {}
  20. table.insert(newnum,{server=skynet.localname(".manage"),configname={"cdkTime_proto",}})-- 集群管理服务
  21. return newnum
  22. end
  23. local function hotfix_true(hotfixlist, configname)
  24. if hotfixlist then
  25. if hotfixlist == "*" then return 1 end
  26. for k, v in pairs(configname) do
  27. if hotfixlist[v] then
  28. logger.trace("_______________________热更配置:%s,更新服务", v)
  29. return 1
  30. end
  31. end
  32. end
  33. return
  34. end
  35. -- 对需要更新配置的服务进行更新
  36. local function updateconfig(hotfixlist)
  37. updateserver = updateserver or newserver()
  38. if next(updateserver) then
  39. for k, v in pairs(updateserver) do
  40. if v.configname and hotfix_true(hotfixlist, v.configname) then
  41. skynet.send(v.server, "lua", "hotfix", hotfixlist)
  42. end
  43. end
  44. else
  45. logger.trace("_______________________热更配置无服务需要")
  46. end
  47. end
  48. --示例1: http://192.168.1.102:9002/reload?code=xxx&ntype=1
  49. --示例2: http://192.168.1.102:9002/reload?code=xxx&ntype=2&vjson=["award_proto","item_proto"]
  50. local function reload(args, ipaddr, header)
  51. logger.warn("处理来自主机 %s 的hotfix热更请求,:%s", ipaddr, args.vjson)
  52. if not whitelist[ipaddr] then
  53. -- return { "403 - Forbidden" }
  54. -- return cjson.encode({errno = 403, msg = "不信任ip"})
  55. end
  56. -- 验证gm账号
  57. local code = string.sub(args.code, 1, 16)
  58. if code ~= content.sign then
  59. return cjson.encode({state = 403, msg = "账号或密码错误"})
  60. end
  61. local ntype = tonumber(args.ntype)
  62. -- 热更配置文件
  63. if ntype == 2 then
  64. logger.warn("更新配置文件:%s", args.vjson)
  65. local hotfixlist
  66. assetcenter = assetcenter or skynet.localname(".assetcenter")
  67. local error
  68. if args.vjson == "*" then
  69. error = skynet.call(assetcenter, "lua", "hotfix")
  70. hotfixlist = "*"
  71. elseif args.vjson then
  72. hotfixlist = {}
  73. local entity=cjson.decode(args.vjson)
  74. for k, v in pairs(entity) do
  75. logger.warn("更新配置文件:%s", v)
  76. error = skynet.call(assetcenter, "lua", "hotfix", v)
  77. hotfixlist[v] = 1
  78. end
  79. else
  80. return cjson.encode({state = 400, msg = "缺少配置表参数"})
  81. end
  82. if error then
  83. return cjson.encode({state = 400,msg = "读取文件失败:" .. error})
  84. end
  85. updateconfig(hotfixlist)
  86. -- 热更源代码
  87. elseif ntype == 1 then
  88. codecache.clear()
  89. else
  90. return cjson.encode({state =400, msg = "错误的ntype"})
  91. end
  92. -- 通知 watchdog, 放弃 agent 池中的失效对象
  93. -- watchdog = watchdog or skynet.localname(".watchdog")
  94. -- skynet.call(watchdog, "lua", "hotfix")
  95. logger.warn("hotfix (%s, %s)", args.code, ipaddr)
  96. return cjson.encode({state = 0, msg = "热更新 成功"})
  97. end
  98. return reload