【skynet】skynet 服务间通信

写在前面

skynet 服务之间有自己的一套高效通信 API 。本文给出简单的示例。

文章目录


准备工作

首先要有一个编译好,而且工作正常的 skynet 。

编写代码

在 skynet/example 目录编写一个配置文件,两个代码文件。

calc.lua 提供数值计算服务。

lua 复制代码
local skynet = require "skynet"

local CALC = {}

-- 处理加法
function CALC.add(...)
    local res = 0
    for i, v in ipairs{...} do
        res = res + v
    end
    return res
end

-- 处理减法
function CALC.sub(lhs, rhs)
    local res = lhs - rhs
    skynet.error(lhs .. " - " .. rhs .. " = " .. res)
end

-- 处理 lua 消息
function lua_dispatch(session, source, cmd, ...)
    local f = assert(CALC[cmd])
    skynet.ret(skynet.pack(f(...)))
end

skynet.start(function()
    -- 注册 lua 消息的处理函数
    skynet.dispatch("lua", lua_dispatch)
end)

主服务 main_test 负责启动 calc ,之后周期发出数值计算请求。

lua 复制代码
local skynet = require "skynet"
require "skynet.manager"

-- 初始化函数
function init()
    math.randomseed(math.floor(skynet.time()))
    -- 启动一个服务,并命名
    local calc_serv = skynet.newservice("calc")
    skynet.name(".calc", calc_serv)
end

-- 服务函数
function task_add()
    while true do
        -- 加法
        local a = math.random(1, 100)
        local b = math.random(1, 100)
        local c = math.random(1, 100)
        local ret = skynet.call(".calc", "lua", "add", a, b, c)
        skynet.error(a .. " + " .. b .. " + " .. c .. " = " .. ret)

        -- 睡眠一秒
        skynet.sleep(300)
    end
end

function task_sub()
    while true do
        -- 减法
        local lhs = math.random(1, 100)
        local rhs = math.random(1, 100)
        local ret = skynet.send(".calc", "lua", "sub", lhs, rhs)

        -- 睡眠 1500ms
        skynet.sleep(150)
    end
end

-- 注册初始化函数
skynet.init(init)

-- 启动服务
skynet.start(function()
    skynet.fork(task_add)
    skynet.fork(task_sub)
end)

配置文件 config_test

lua 复制代码
-- 启动多少个工作线程
thread = 8

-- skynet 工作在单节点模式下
harbor = 0

-- skynet 节点的主程序
start = "main_test"

-- lua 服务代码所在的位置
luaservice = "./service/?.lua;./examples/?.lua"

运行结果

root@macbook:~/skynet# ./skynet examples/config_test 
[:00000001] LAUNCH logger 
[:00000002] LAUNCH snlua bootstrap
[:00000003] LAUNCH snlua launcher
[:00000004] LAUNCH snlua cdummy
[:00000005] LAUNCH harbor 0 4
[:00000006] LAUNCH snlua datacenterd
[:00000007] LAUNCH snlua service_mgr
[:00000008] LAUNCH snlua main_test
[:00000009] LAUNCH snlua calc
[:00000008] 52 + 77 + 75 = 204
[:00000008] 25 - 56 = -31
...
相关推荐
一眼万里*e2 个月前
skynet 实操篇
c++·skynet
一眼万里*e2 个月前
skynet 入门篇
服务器·c++·skynet
tissar5 个月前
【skynet】 网络编程之回显服务器
服务器·lua·skynet
笨死de猪5 个月前
skynet 使用protobuf
skynet·protobuf
csdn_HZW5 个月前
skynet中newservice和uniqueservice的区别
skynet
ღCauchyོꦿ࿐9 个月前
【Skynet 入门实战练习】事件模块 | 批处理模块 | GM 指令 | 模糊搜索
mongodb·lua·skynet·游戏服务器·gm 指令
ღCauchyོꦿ࿐10 个月前
【Skynet 入门实战练习】分布式 ID | 雪花算法 | 缓存设计 | LRU算法 | 数据库
数据库·分布式·算法·缓存·skynet·lru
ღCauchyོꦿ࿐10 个月前
【Skynet 入门实战练习】实现网关服务 | 用户代理 | RPC 协议 | 客户端
网络协议·gateway·客户端·skynet·服务端
ღCauchyོꦿ࿐10 个月前
【Skynet 入门实战练习】游戏模块划分 | 基础功能模块 | timer 定时器模块 | logger 日志服务模块
服务器·lua·定时器·日志·skynet
ღCauchyོꦿ࿐10 个月前
【Skynet 入门实战练习】开发环境搭建 | 运行第一个项目 | debug console 简单使用
lua·游戏开发·skynet·服务器开发