newservice(name, ...):
阻塞的形势启动一个名为 name 的新服务,待start函数执行完后会返回这个服务的地址。uniqueservice(name, ...):
针对于当前节点,启动一个唯一服务(相当于单例),如果服务该服务已经启动,则返回已启动的服务地址。queryservice(true, name, ...):
针对于所有节点,启动一个唯一服务。queryservice(name) :
查询一个由uniqueservice 启动的唯一服务的地址,若该服务尚未启动则等待。queryservice(true,name):
查询一个由uniqueservice 启动的唯一服务的地址,若该服务尚未启动则等待。project = "../project/02service/"
thread = 8
cpath = "./cservice/?.so"
bootstrap = "snlua bootstrap"
start = "serviceTest"
harbor = 1
address = "127.0.0.1:2526"
master = "127.0.0.1:2013"
standalone = "0.0.0.0:2013" -- 主节点
lualoader = "./lualib/loader.lua"
luaservice = "./service/?.lua;"..project.."?.lua;"..project.."worker/?.lua;"
lua_path = "./lualib/?.lua;" .. "./lualib/?/init.lua;"
lua_cpath = "./luaclib/?.so"
work1.lua
local skynet = require "skynet"
local param1 = ""
local param2 = ""
skynet.init(function ()
skynet.error("[work1] init work param1",param1,"param2",param2)
end)
local function initParams(p1, p2)
skynet.error("[work1] initParams p1",p1,"p2",p2)
param1 = p1
param2 = p2
end
-- 调用初始化函数,...是不定参数,会从skynet.newservice的第二个参数开始透传过来
initParams(...)
local function doWork()
while true do
skynet.error("---------work----------------")
skynet.sleep(200)
end
end
skynet.start(function ()
skynet.error("worker start param1 = ",param1,"param2 = ",param2)
-- doWork()
end)
serviceTest.lua
local skynet = require "skynet"
local function test1()
local h1 = skynet.newservice("work1","work001","10086")
local h2 = skynet.newservice("work1","work002","1008611")
local h3 = skynet.uniqueservice("work1","work003","1008612")
local h33 = skynet.uniqueservice("work1","work004","1008613") -- 这里并没有调用initParams与init函数
skynet.error("h1",h1,"h2",h2,"h3",h3,"h33",h33)
end
skynet.start(function()
skynet.error("[service main] start")
skynet.error("-----------test service begin--------------")
test1();
skynet.error("-----------test service end--------------")
skynet.error("[service main] end")
skynet.exit()
end)
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ ./skynet ./examples/02serviceconfig
project = "../project/02service/"
thread = 8
cpath = "./cservice/?.so"
bootstrap = "snlua bootstrap"
start = "serviceTest"
harbor = 2 -- 修改节点
address = "127.0.0.1:2527" -- 修改从节点端口
master = "127.0.0.1:2013"
lualoader = "./lualib/loader.lua"
luaservice = "./service/?.lua;"..project.."?.lua;"..project.."worker/?.lua;"
lua_path = "./lualib/?.lua;" .. "./lualib/?/init.lua;"
lua_cpath = "./luaclib/?.so"
local function test2()
local h1 = skynet.uniqueservice("work1","work001","10086")
local h2 = skynet.uniqueservice("work1","work002","1008611")
local h3 = skynet.uniqueservice("work1","work003","1008612")
skynet.error("h1",h1,"h2",h2,"h3",h3)
local h33 = skynet.queryservice("work1")
skynet.queryservice("work1")
end
skynet.start(function()
skynet.error("[service main] start")
skynet.error("-----------test service begin--------------")
test2();
skynet.error("-----------test service end--------------")
skynet.error("[service main] end")
skynet.exit()
end)
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ ./skynet ./examples/02serviceconfig
[:01000009] -----------test service begin--------------
[:0100000a] LAUNCH snlua work1 work001 10086
[:0100000a] [work1] initParams p1 work001 p2 10086
[:0100000a] [work1] init work param1 work001 param2 10086
[:0100000a] worker start param1 = work001 param2 = 10086
[:0100000b] LAUNCH snlua work1 work003 1008612
[:0100000b] [work1] initParams p1 work003 p2 1008612
[:0100000b] [work1] init work param1 work003 param2 1008612
[:0100000b] worker start param1 = work003 param2 = 1008612
[:01000009] h1 16777226 h2 16777226 h3 16777227
[:01000009] h33 16777226 h333 16777227
[:01000009] -----------test service end--------------
mhzzj@mhzzj-virtual-machine:~/work/skynetStudy/skynet$ ./skynet ./examples/02serviceconfig2
[:02000007] -----------test service begin--------------
[:02000008] LAUNCH snlua work1 work001 10086
[:02000008] [work1] initParams p1 work001 p2 10086
[:02000008] [work1] init work param1 work001 param2 10086
[:02000008] worker start param1 = work001 param2 = 10086
[:02000007] h1 33554440 h2 33554440 h3 16777227
[:02000007] h33 33554440 h333 16777227
[:02000007] -----------test service end--------------
https://gitee.com/mhz-zzj/skynet-study