集群远程rpc调用

前言

集群远程RPC调用,是基于skynet的集群 cluster mode模式封装了一套简单好用、适用用框架的模式。

配置启动服务端

  1. 配置load_mods.lua

load_mods.lua

1
2
3
4
5
6
7
8
9
10
11
--共享配置
share_config_m = {
launch_seq = 1,
launch_num = 1,
default_arg = {
--cluster_server用的配置
cluster_server = {
host = "127.0.0.1:9688",
},
},
},
  1. 启动集群服务端
    1
    skynet.uniqueservice("cluster_server")

实现细节

远程RPC调用

cluster_server为例
服务有一个可热更服务模块 test_m.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
local log = require "log"
local skynet = require "skynet"
local contriner_client = require "contriner_client"

contriner_client:register("share_config_m")
local string = string

local g_config = nil

local CMD = {}

function CMD.hello(who)
log.info(string.format("%s send hello msg for me",who))
end

function CMD.ping()
local confclient = contriner_client:new("share_config_m")
local conf = confclient:mod_call('query','cluster_server')
return string.format("pong %s %s %s",g_config.instance_name,conf.host,skynet.self())
end

function CMD.start(config)
g_config = config
return true
end

function CMD.exit()
return true
end

return CMD

那边其他服务改如何调用到这个服务的hello命令呢

cluster_client首先需要配置连接服务端的地址。

load_mods.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
return {
share_config_m = {
launch_seq = 1,
launch_num = 1,
},

cluster_client_m = {
launch_seq = 2,
launch_num = 1,
default_arg = {
node_map = {
['cluster_server'] = {
[1] = "127.0.0.1:9688",
[2] = "127.0.0.1:9689",
}
}
}
},

test_m = {
launch_seq = 4,
launch_num = 1,
}
}

之后使用cluster_client就可以使用远程RPC调用了

1
2
3
local cli = cluster_client:new("cluster_server","test_m") --访问cluster_server的test_m模板

cli:one_balance_send("hello","one_balance_send") --调用hello命令

详细API介绍


集群远程rpc调用
https://huahua132.github.io/2023/02/25/skynet_fly_word/word_2/C_RPC/
作者
huahua132
发布于
2023年2月25日
许可协议