srvHttpServer.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. --[[
  2. Descripttion:
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-06 10:42:14
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-06 11:27:01
  8. --]]
  9. require "skynet.manager"
  10. local skynet = require "skynet"
  11. local socket = require "skynet.socket"
  12. local nodes = require("nodes")
  13. local agentList = {}
  14. skynet.start(
  15. function()
  16. local nodeName = skynet.getenv("nodeName")
  17. local ip = nodes[nodeName].ip
  18. local port = nodes[nodeName].port
  19. local agentCount = nodes[nodeName].agentCount
  20. local bodyLimit = nodes[nodeName].bodyLimit or 8192
  21. log.info("node[%s] ip[%s] port[%s]", nodeName, ip, port)
  22. -- 创建代理服务
  23. for i = 1, agentCount do
  24. local handle = skynet.newservice("srvHttpAgent", bodyLimit)
  25. table.insert(agentList, handle)
  26. end
  27. local balance = 1
  28. local listenId = socket.listen(ip, port, 128)
  29. skynet.error(string.format("%s server listen %s:%s", nodeName, ip, port))
  30. socket.start(
  31. listenId,
  32. function(id, addr)
  33. local cip, cport = string.match(addr, "([^:]+):?(%d*)$")
  34. skynet.send(agentList[balance], "lua", "http", id, cip)
  35. balance = balance + 1
  36. if balance > #agentList then
  37. balance = 1
  38. end
  39. end
  40. )
  41. skynet.register(".http_server")
  42. end
  43. )