Skynet默认的配置是把Master与Harbor集成到一个结点了,为了更清楚的了解Skynet框架,需要分离Master与Harbor,这就需要了解Skynet是如何配置的及Skynet的启动流程及各个参数的作用.
root = "./" thread = 8 logger = nil harbor = 1 address = "127.0.0.1:2525" master = "127.0.0.1:2012" start = "main" standalone = "0.0.0.0:2012" luaservice = root.."service/?.lua;"..root.."service/?/init.lua" cpath = root.."service/?.so" protopath = root.."proto" redis = root .. "redisconf"
这是Skynet的默认配置,skynet_main.c的main函数从配置文件中读取这些值,并赋给config对象,skynet_start函数就使用这些配置参数,启动skynet。
if (config->standalone) { if (_start_master(config->standalone)) { return; } } // harbor must be init first if (skynet_harbor_start(config->master , config->local)) { fprintf(stderr, "Init fail : no master"); return; }
可以看到master与harbor核心区别在于standalone参数,如果配置了这个参数skynet就会把该结点视作一个master,同时master的本质上也是一个harbor,所以必须配置address参数,这个参数就是harbor结点的地址,如果一个harbor不是master,则必须配置master参数,告诉该harbor结点在哪一个master集群里。
start参数是对应的lua文件,它们于service目录。
local skynet = require "skynet" skynet.start(function() print("Server start") local service = skynet.launch("snlua","service_mgr") local connection = skynet.launch("connection","256") local lualog = skynet.launch("snlua","lualog") local console = skynet.launch("snlua","console") local remoteroot = skynet.launch("snlua","remote_root") local watchdog = skynet.launch("snlua","watchdog","8889 4 0") local db = skynet.launch("snlua","simpledb") -- skynet.launch("snlua","testgroup") skynet.exit() end)
这是默认的main.lua,分离位于同一结点的master与harbor,最重要的是master不需要启动一个watchdog这个网关服务,所以把main.lua拷贝为main_master.lua,并注释掉watchdog,最终配置文件为
config_master
root = "./" thread = 8 logger = nil harbor = 1 address = "127.0.0.1:2525" start = "main_master" standalone = "0.0.0.0:2012" cpath = root.."service/?.so" protopath = root.."proto"
config_harbor
root = "./" thread = 8 logger = nil harbor = 1 address = "127.0.0.1:2526" master = "127.0.0.1:2012" start = "main" luaservice = root.."service/?.lua;"..root.."service/?/init.lua" cpath = root.."service/?.so" protopath = root.."proto" redis = root .. "redisconf"
这个时候再启动skynet,clinet连接,就发现master与harbor已经分离了。
但此时的harbor结点,还起着网关的作用,所以可以另外起一个harbor,充当网关,config_gateway
root = "./" thread = 8 logger = nil harbor = 1 address = "127.0.0.1:2528" master = "127.0.0.1:2012" start = "main_gateway" luaservice = root.."service/?.lua;"..root.."service/?/init.lua" cpath = root.."service/?.so" protopath = root.."proto" redis = root .. "redisconf"
其实也就是让另一个harbor不起网关.
现在只是增加了几个配置文件,就已经有了客户端,网关服务,log服务,可以看到一个网游服务器的雏形已经出来了。