01、前置准备
开发所在目录
shell
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy$ pwd
/home/mhzzj/work/skynetStudy
前置准备
shell
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy$ sudo apt install lua5.3
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy$ apt install git
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy$ sudo apt-get install git build-essential readline-dev autoconf
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy$ git clone https://github.com/cloudwu/skynet.git
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy$ git clone https://github.com/jemalloc/jemalloc.git
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy$ cp -r jemalloc/* skynet/3rd/jemalloc/
02、编译Skynet
shell
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy$ cd skynet/
# 编译前的skynet文件夹
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ ls
3rd examples HISTORY.md LICENSE lualib lualib-src Makefile platform.mk README.md service service-src skynet-src test
# 编译
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ make linux
# 编译后的文件夹
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ ls
3rd cservice examples HISTORY.md LICENSE luaclib lualib lualib-src Makefile platform.mk README.md service service-src skynet skynet-src test
03、Skynet运行
服务端
shell
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ ./skynet examples/config
客户端
shell
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ ./3rd/lua/lua examples/client.lua
04、skynet目录介绍与配置说明
目录结构
shell
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ ls
3rd examples HISTORY.md LICENSE lualib lualib-src Makefile platform.mk README.md service service-src skynet-src test
- 3rd #第三方支持库,包括LUA虚拟机,jmalloc等
- lualib #lua语言封装的常用库,包括http、md5
- lualib-src #将c语言实现的插件捆绑成lua库,例如数据库驱动、bson、加密算法等
- service #使用lua写的Skynet的服务模块
- service-src #使用C写的Skynet的服务模块
- skynet-src #skynet核心代码目录
- test #使用lua写的一些测试代码
配置说明
/home/mhzzj/work/skynetStudy/skynet/examples/config
lua
include "config.path"
thread = 8 -- 启用的工作线程数量,一般配置为CPU核心数
logger = nil
logpath = "."
harbor = 1
address = "127.0.0.1:2526"
master = "127.0.0.1:2013"
start = "main" -- 主服务入口
bootstrap = "snlua bootstrap" -- The service for bootstrap
standalone = "0.0.0.0:2013"
-- 用C语言编写的服务模块的路径
cpath = root.."cservice/?.so"
/home/mhzzj/work/skynetStudy/skynet/examples/config.path
lua
root = "./"
--服务脚本路径,包括skynet框架自带的一些服务和自己写的服务
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua;"..root.."test/?/init.lua"
-- lua脚本加载器,指定skynet的loader.lua
lualoader = root .. "lualib/loader.lua"
-- 程序加载lua脚本时,会搜索这个lua_path配置的路径
lua_path = root.."lualib/?.lua;"..root.."lualib/?/init.lua"
-- 用C语言编写的程序库(.so文件)的路径
lua_cpath = root .. "luaclib/?.so"
snax = root.."examples/?.lua;"..root.."test/?.lua"
05、hello world
项目结构
myMain.lua
lua
local skynet = require "skynet"
skynet.start(function()
skynet.error("[start main] hello world")
-- TODO 启动其他服务
skynet.exit()
end)
配置文件
shell
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ cp examples/config examples/myconfig
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ cp examples/config.path examples/myconfig.path
myconfig配置如下:
lua
include "myconfig.path" --更改路径文件
thread = 8
harbor = 1
address = "127.0.0.1:2526"
master = "127.0.0.1:2013"
start = "myMain" -- 更改启动文件
bootstrap = "snlua bootstrap" -- The service for bootstrap
standalone = "0.0.0.0:2013"
myconfig.pat配置如下:
启动hellworld
06、源码
https://gitee.com/mhz-zzj/skynet-study
PS:后续代码会随着笔记更新而更新