Commit 3f604f2e3e304ecede4626a554528ab3473d437f

Authored by zhouhaihai
1 parent 1b6307bc

扩容 redis 和 log服务

src/agent.lua
... ... @@ -356,11 +356,6 @@ skynet.start(function()
356 356 return info
357 357 end)
358 358  
359   - redisd = skynet.localname(".redis")
360   - if tonumber(skynet.getenv "logd") == 1 then
361   - logd = skynet.localname(".log")
362   - end
363   -
364 359 cs = queue()
365 360  
366 361 pvpd = skynet.localname(".pvpcross")
... ...
src/main.lua
... ... @@ -2,23 +2,41 @@ local skynet = require "skynet"
2 2  
3 3 local max_client = tonumber(skynet.getenv("max_client"))
4 4 local max_queue = tonumber(skynet.getenv("max_queue"))
  5 +local work_count = tonumber(skynet.getenv("thread"))
  6 +local use_logd = tonumber(skynet.getenv("logd"))
5 7  
6 8 skynet.start(function()
7 9 print("Server start")
8 10 skynet.newservice("debug_console", tonumber(skynet.getenv("debug_port")))
9 11  
  12 +
  13 + -- 启动redis
  14 + for i = 1, work_count do
  15 + local redisd = skynet.newservice("services/redisd", i)
  16 + skynet.call(redisd, "lua", "open", {
  17 + redishost = skynet.getenv("redis_host"),
  18 + redisport = tonumber(skynet.getenv("redis_port")),
  19 + redisdb = tonumber(skynet.getenv("redis_db")),
  20 + auth = skynet.getenv("redis_auth")
  21 + })
  22 + end
  23 +
  24 + --启动log
  25 + if use_logd == 1 then
  26 + for i = 1, work_count do
  27 + local logd = skynet.newservice("services/logd", i)
  28 + skynet.call(logd, "lua", "open")
  29 + end
  30 + end
  31 +
10 32 local httpd = skynet.newservice("services/httpweb", tonumber(skynet.getenv("httpweb_port")))
11 33 local watchdog = skynet.newservice("services/watchdog", max_client)
12 34  
  35 +
13 36 skynet.call(watchdog, "lua", "start", {
14 37 port = tonumber(skynet.getenv("server_port")),
15 38 maxclient = max_client + max_queue + 10,
16 39 httpd = httpd,
17   -
18   - redishost = skynet.getenv("redis_host"),
19   - redisport = tonumber(skynet.getenv("redis_port")),
20   - redisdb = tonumber(skynet.getenv("redis_db")),
21   - auth = skynet.getenv("redis_auth"),
22 40 })
23 41  
24 42 skynet.exit()
... ...
src/models/RoleLog.lua
1 1 local serverId = skynet.getenv("servId")
2 2 local server_id = (skynet.getenv("serverType") or "localtest") .. "_" .. serverId
  3 +local logproxy = require "shared.logproxy"
3 4  
4 5 --[[
5 6 100 购买/兑换行为
... ... @@ -765,8 +766,7 @@ function RoleLog.bind(Role)
765 766 end
766 767 end
767 768 end
768   - if not logd then return end
769   - pcall(skynet.send, logd, "lua", "log", doc, "bi")
  769 + logproxy:log(doc, "bi")
770 770 end
771 771  
772 772 function Role:logItems(itemId, before, after, log)
... ... @@ -825,8 +825,7 @@ function RoleLog.bind(Role)
825 825 end
826 826 end
827 827 doc["@type"] = logType
828   - if not logd then return end
829   - pcall(skynet.send, logd, "lua", "log", doc, "log")
  828 + logproxy:log(doc, "log")
830 829 end
831 830  
832 831  
... ...
src/services/agent_ctrl.lua
... ... @@ -6,6 +6,7 @@ local xxtea = require "xxtea"
6 6 local deque = require "deque"
7 7 local datacenter = require "skynet.datacenter"
8 8 local agent_queued = require "services.agent_queued"
  9 +local logproxy = require "shared.logproxy"
9 10  
10 11 local pcall = pcall
11 12 local string_format = string.format
... ... @@ -139,11 +140,11 @@ function _M:check_agent_status()
139 140 end
140 141 end
141 142  
142   - if now >= next_log_time and now % 60 == 0 and logd then
  143 + if now >= next_log_time and now % 60 == 0 then
143 144 next_log_time = now + 60
144 145 local count = table_nums(self.u2f)
145 146 datacenter.set("onlineCount", count)
146   - pcall(skynet.send, logd, "lua", "log", {["@type"] = "online", count = count}, "log")
  147 + logproxy:log({["@type"] = "online", count = count}, "log")
147 148 end
148 149 end
149 150  
... ...
src/services/dbseed.lua
... ... @@ -34,8 +34,6 @@ local steps = {
34 34 }
35 35  
36 36 skynet.start(function ()
37   - redisd = skynet.localname(".redis")
38   -
39 37 redisproxy = require("shared.redisproxy")
40 38  
41 39 local new = redisproxy:hsetnx("autoincrement_set", "server_start", os.date("%Y%m%d", skynet.timex())) == 1
... ...
src/services/globald.lua
... ... @@ -190,7 +190,6 @@ local function __init__()
190 190 end
191 191 end
192 192 end)
193   - redisd = skynet.localname(".redis")
194 193 skynet.register(".globald")
195 194  
196 195 end
... ...
src/services/httpweb.lua
... ... @@ -53,12 +53,8 @@ end
53 53 local CMD = require "actions.HttpAction"
54 54  
55 55 local function start()
56   - redisd = skynet.localname(".redis")
57 56 globalCsv = csvdb["GlobalDefineCsv"]
58 57  
59   - if tonumber(skynet.getenv "logd") == 1 then
60   - logd = skynet.localname(".log")
61   - end
62 58  
63 59 local listen_socket = socket.listen("0.0.0.0", port)
64 60 print("Listen web port " .. port)
... ...
src/services/logd.lua
... ... @@ -2,12 +2,12 @@ local skynet = require "skynet"
2 2 local queue = require "skynet.queue"
3 3 local bson = require "bson"
4 4 local socketdriver = require "skynet.socketdriver"
5   -
6 5 local serverId = tonumber(skynet.getenv("servId"))
7 6  
8 7 require "shared.init"
9 8 require "skynet.manager"
10 9  
  10 +local logdIdx = ...
11 11 local table_insert = table.insert
12 12 local pairs = pairs
13 13 local ipairs = ipairs
... ... @@ -15,6 +15,7 @@ local string_format = string.format
15 15  
16 16 local logId = 0
17 17 local CMD, cs = {}
  18 +local prefix = "wasteland" .. logdIdx .. "S" .. serverId .. "C"
18 19  
19 20 local logHandle = {
20 21 bi = {
... ... @@ -33,7 +34,7 @@ local logHandle = {
33 34 doc["game_name"] = "wasteland"
34 35 doc["env"] = "cb"
35 36 doc["game_name_type"] = "guaji"
36   - doc["log_id"] = "wastelandC" .. logId .. "S" .. serverId .. "T" .. now
  37 + doc["log_id"] = prefix .. logId .. "T" .. now
37 38 logId = (logId + 1) % 10000000
38 39 end
39 40 },
... ... @@ -135,7 +136,7 @@ local function __init__()
135 136 end)
136 137 cs = queue()
137 138  
138   - skynet.register(".log")
  139 + skynet.register(".logd" .. logdIdx)
139 140 end
140 141  
141 142 skynet.start(__init__)
... ...
src/services/pvpd.lua
... ... @@ -289,7 +289,6 @@ end
289 289  
290 290 ------------------------------------------------------
291 291 function CMD.start()
292   - redisd = skynet.localname(".redis")
293 292 globalCsv = csvdb["GlobalDefineCsv"]
294 293  
295 294 pvpInfo = require("models.Pvpd").new({key = "cross:pvpInfo"})
... ...
src/services/redisd.lua
... ... @@ -3,7 +3,7 @@ require "skynet.manager"
3 3 local redis = require "skynet.db.redis"
4 4  
5 5 local db
6   -
  6 +local idx = ...
7 7 local command = {}
8 8  
9 9 function command.open(conf)
... ... @@ -27,5 +27,5 @@ skynet.start(function()
27 27 skynet.info_func(function()
28 28 return skynet.stat("mqlen")
29 29 end)
30   - skynet.register ".redis"
  30 + skynet.register(".redis" .. idx)
31 31 end)
32 32 \ No newline at end of file
... ...
src/services/watchdog.lua
1 1 local skynet = require "skynet"
2 2 require "skynet.manager"
3   -local redisproxy = require "shared.redisproxy"
4 3 local socket = require "skynet.socket"
5 4 local netpack = require "skynet.netpack"
6 5 local datacenter = require "skynet.datacenter"
... ... @@ -47,7 +46,7 @@ function SOCKET.data(fd, msg)
47 46 end
48 47 end
49 48  
50   -local use_logd = tonumber(skynet.getenv "logd")
  49 +
51 50  
52 51 -- @desc: agent状态定时检测
53 52 function check_agent_status()
... ... @@ -65,11 +64,7 @@ end
65 64  
66 65 function CMD.start(conf)
67 66 skynet.call(gate_serv, "lua", "open" , conf)
68   - skynet.call(redisd, "lua", "open", conf)
69   -
70   - if use_logd == 1 then
71   - skynet.call(logd, "lua", "open")
72   - end
  67 +
73 68 skynet.call(pvpd, "lua", "start")
74 69 -- 开启agent状态检测定时器
75 70 check_agent_status()
... ... @@ -110,17 +105,11 @@ skynet.start(function()
110 105 end
111 106 end)
112 107 skynet.register ".watchdog"
113   - -- 数据库服务
114   - redisd = skynet.newservice("services/redisd")
115 108  
116 109 -- 提前加载好
117 110 csvdata.init()
118 111 print("launch csvdatad ...")
119 112  
120   - -- 日志服务
121   - if use_logd == 1 then
122   - logd = skynet.newservice("services/logd")
123   - end
124 113 -- pvp 服务
125 114 pvpd = skynet.newservice("services/pvpd")
126 115 cluster.register("pvpd", pvpd)
... ...
src/shared/logproxy.lua 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +
  2 +local skynet = require "skynet"
  3 +local logd_count = tonumber(skynet.getenv("thread"))
  4 +local use_logd = tonumber(skynet.getenv("logd"))
  5 +
  6 +
  7 +local logd
  8 +skynet.init(function()
  9 + if use_logd then
  10 + local idx = math.randomInt(1, logd_count)
  11 + logd = skynet.localname(".logd" .. idx)
  12 + end
  13 +end)
  14 +
  15 +local logproxy = {}
  16 +
  17 +function logproxy:log(doc, logTo)
  18 + if use_logd and logd then
  19 + pcall(skynet.send, logd, "lua", "log", doc, logTo)
  20 + end
  21 +end
  22 +
  23 +return logproxy
0 24 \ No newline at end of file
... ...
src/shared/redisproxy.lua
1 1 local skynet = require "skynet"
2   -local harbor = require "skynet.harbor"
  2 +require "utils.init"
  3 +
  4 +local redisd_count = tonumber(skynet.getenv("thread"))
  5 +local redisd
  6 +skynet.init(function()
  7 + local idx = math.randomInt(1, redisd_count)
  8 + redisd = skynet.localname(".redis" .. idx)
  9 +end)
3 10  
4 11 local table_insert = table.insert
5 12  
6 13 local redisproxy = {}
7 14  
8   -
9 15 local isUsePika = false
10 16  
11 17  
... ...