本系列【Skynet 入门实战练习】所有源码同步:https://gitee.com/Cauchy_AQ/skynet_practice
开发环境搭建 skynet skynet 框架地址:https://github.com/cloudwu/skynet git clone https://github.com/cloudwu/skynet.git 编译skynet make 'linux'成功编译会在 skynet 文件夹下多出一个 skynet 可执行文件
执行 skynet 示例 ./skynet examples/config如上成功跑起了 skynet 的官方示例。
配置文件
skynet 启动服务需要指定配置文件,这里以官方示例 Demo 来简单介绍。
examples/config:
include "config.path"-- preload = "./examples/preload.lua"-- run preload.lua before every lua service runthread = 8logger = nillogpath = "."harbor = 1address = "127.0.0.1:2526"master = "127.0.0.1:2013"start = "main"-- main scriptbootstrap = "snlua bootstrap"-- The service for bootstrapstandalone = "0.0.0.0:2013"-- snax_interface_g = "snax_g"cpath = root.."cservice/?.so"-- daemon = "./skynet.pid"第一行 include "config.path",表示当前配置文件包含了另一个配置文件 config.path,且这个文件相对于 examples/config 在一个目录下。
config.path:
root = "./"luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua;"..root.."test/?/init.lua"lualoader = root .. "lualib/loader.lua"lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua"lua_cpath = root .. "luaclib/?.so"snax = root.."examples/?.lua;"..root.."test/?.lua"配置文件实际上就是一段 lua 代码,通常,我们以 key = value 的形式对配置项赋值。
skynet 在启动时,会读取里面必要的配置项,并将暂时用不到的配置项以字符串形式保存在 skynet 内部的 env 表中。这些配置项可以通过 skynet.getenv 获取。
需要了解的几个重要参数:
参数描述lualoaderlua 脚本加载器,通常配置为 skynet/lualib/loader.lualuaservice服务脚本路径,包括 skynet 框架自带服务和自己写的服务lua_pathlua 脚本路径,即 lua 实现的库文件路径lua_cpath用 C 编写的程序库路径,.so 文件路径cpath用 C 编写的服务模块的位置,通常指 cservice 下那些 .so 文件thread启用的工作线程数量,一般配置为 CPU 核心数harbor一般配置为 0 ,采用 cluster 集群模式,skynet 工作在单节点模式下。此时 master 和 address 以及 standalone 都不必设置,否则 master/slave集群模式,指定 1-255 间的任意整数,表示节点唯一编号start主服务的入口bootstrap skynet 启动的第一个服务以及其启动参数。默认配置为 snlua bootstrap ,即启动一个名为 bootstrap 的 lua 服务snax 用 snax 框架编写的服务的查找路径preload 在设置完 package 中的路径后,加载 lua 服务代码前,loader 会尝试先运行一个 preload 制定的脚本,默认为空daemon 配置 daemon = "./skynet.pid" 可以以后台模式启动 skynet,同时请配置 loggerlogger 决定了 skynet 内建的 skynet_error 这个 C API 将信息输出到什么文件中。logger 配置为 nil,将输出到标准输出详细配置说明参考:https://github.com/cloudwu/skynet/wiki/Config
项目,启动!
在了解了上述的基础配置后,我们可以开始运行自己的 skynet 项目了。
首先准备好项目需要的几个文件及文件夹 lualib 跟游戏逻辑无关的通用的 Lua 库代码service 各种游戏服务入口代码,这里的一个文件就是一个服务etc 配置文件,比如配置服务器端口,数据库端口module 游戏逻辑test 测试代码暂且对上面几个文件夹的作用有个印象,现在我们只需要编写配置文件、一个主服务,就可以先运行一个游戏项目的空壳了。
etc/config.path
root = "./"lualoader = root .. "skynet/lualib/loader.lua"luaservice = root .. "service/?.lua;" .. root .. "skynet/service/?.lua" lua_path = root .. "lualib/?.lua;" .. root .. "skynet/lualib/?.lua;" .. root .. "module/?.lua"lua_cpath = root .. "luaclib/?.so;" .. root .. "skynet/luaclib/?.so"cpath = root .. "skynet/cservice/?.so"snax = root .. "service/?.lua"etc/config
include "config.path"-- 启动配置thread = 4bootstrap = "snlua bootstrap"start = "main"harbor = 0-- preload = "preload.lua"-- daemon = "skynet.pid"-- debug consoledebug_console_port = 4040service/main.lua
local skynet = require "skynet"local debug_port = tonumber(skynet.getenv("debug_console_port")) or 4040skynet.start(function()skynet.error("[main.lua] start")if not skynet.getenv "daemon" then-- 不是 daemon 模式启动则开启 console 服务local console = skynet.newservice("console")end-- 开启 debug console 服务skynet.newservice("debug_console", debug_port)-- main 服务只作为入口,启动完所需的服务后就可以退出skynet.exit()end)编写好后,就可以通过 ./skynet/skynet etc/config 来启动项目:
debug console
我们在这份代码中,有自行启动一个 debug console 服务,运行在 4040 端口,端口可通过配置文件进行配置。
这是一个 skynet 自带的调试控制台服务,可以通过 telnet 工具来连接,执行 telnet 127.0.0.1 4040。
执行 help,查看调试工具提供的指令 list,列出所有服务,以及启动服务的命令参数 mem,让所有 lua 服务汇报自己占用的内存更多指令介绍可参考:https://github.com/cloudwu/skynet/wiki/DebugConsole