前言 集群远程RPC调用,是基于skynet的集群 cluster mode模式 封装了一套简单好用、适用用框架的模式。
配置启动服务端
配置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 = { host = "127.0.0.1:9688" , }, }, },
启动集群服务端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" ) cli:one_balance_send("hello" ,"one_balance_send" )