前言 集群远程RPC调用,是借鉴skynet cluster mode 自己实现的(https://github.com/cloudwu/skynet/wiki/Cluster)封装了一套简单好用、适用用框架的模式。
配置启动服务端
配置load_mods.lua
load_mods.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 share_config_m = { launch_seq = 1 , launch_num = 1 , default_arg = { frpc_server = { host = "127.0.0.1:9688" , gateconf = { address = '127.0.0.1' , port = 9688 , maxclient = 2048 , }, }, }, },
启动集群服务端1 skynet.uniqueservice("frpc_server" )
远程RPC调用 以 frpc_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' ,'frpc_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
命令呢
frpc_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 25 26 27 28 29 30 31 32 33 34 return { share_config_m = { launch_seq = 1 , launch_num = 1 , }, frpc_client_m = { launch_seq = 2 , launch_num = 1 , default_arg = { node_map = { ['frpc_server' ] = { [1 ] = { svr_id = 1 , host = "127.0.0.1:9688" , secret_key = 'sdfdsoifhkjguihre234wedfoih24' , is_encrypt = true , }, [2 ] = { svr_id = 2 , host = "127.0.0.1:9689" , secret_key = 'safdsifuhiu34yjfindskj43hqfo32yosd' , is_encrypt = true , } } } } }, test_m = { launch_seq = 4 , launch_num = 1 , } }
之后使用frpc_client
就可以使用远程RPC 调用了
1 2 3 local cli = frpc_client:new("frpc_server" ,"test_m" ) cli:one_balance_send("hello" ,"one_balance_send" )
功能增强
服务发现 目前支持把host注册到redis, rpc-client端通过watch发现服务连接信息。
连接身份验证 可选配置项 secret_key
用于验证rpc-client的登录密钥。
消息加密支持 可选配置项 is_encrypt
用于把rpc调用中的req
和rsp
消息加密传输,端口对外开放时,可提高安全性,不过因为多了消息加解密的过程会降低tps
。
2024/9/10 增加skynet别名调用方式 one_send_by_name
one_call_by_name
byid_send_by_name
byid_call_by_name
all_send_by_name
all_call_by_name