simple_rpc
简介
简单易于使用的跨进程调用(RPC)
调用时,需要指定调用的目标主机和目标方法
支持阻塞和非阻塞两种调用方式
支持对一个目标或者对一组目标发起跨进程调用
组件接口
SimpleConfig.gs
RPC属性配置
| 函数原型 | 函数作用 |
|---|---|
| void dump() | 调试接口:输出所有配置 |
| void init(RpcConfig value) | 初始化RPC配置 |
| mixed get(string path) | 获取指定路径的配置值 |
| void set(string key, mixed value) | 设置指定属性的配置值 |
| array get_server_ports() | 获取RPC服务的端口列表 |
| map get_server_para() | 获取RPC服务的服务参数 |
| map get_client_para() | 获取RPC服务的客户端参数 |
| int get_connection_life_seconds() | 获取连接的存活时长 |
| int get_rpc_get_timeout_seconds() | 获取每次远程调用本地的超时时长 |
| int get_rpc_get_failure_retry_times() | 获取RPC GET请求调用失败重试次数 |
SimpleConnection.gs
RPC服务的客户端连接对象
| 函数原型 | 函数作用 |
|---|---|
| string desc() | 获取描述信息 |
| void send(string cmd, ...) | 发送消息 |
| void dispatch(array args) | 处理消息 |
| void bind_connection(object client) | 绑定网络连接对象 |
| object unbind_connection() | 解绑网络连接对象 |
| bool is_alive() | 判断连接是否存活 |
SimpleConnectionPool.gs
RPC服务客户端连接对象池(管理一组SimpleConnection实例)
| 函数原型 | 函数作用 |
|---|---|
| object get_connection_by_name(string name) | 根据连接名称获取连接对象 |
| object get_or_create_connection(string host, int port) | 获取或创建到目标主机的连接对象 |
| void dump() | 调试接口: 输出连接对象信息 |
SimpleNetD.gs
提供网络服务的模块
| 函数原型 | 函数作用 |
|---|---|
| void start_server(int port, map para = ) | 启动服务 |
| void stop_server(int port) | 停止服务 |
| object connect(string host, int port, map para = ) | 连接RPC服务 |
SimpleRpcD.gs
| 函数原型 | 函数作用 |
|---|---|
| void start() | 启动功能 |
| void stop() | 停止功能 |
| array remote_get(RpcAddress addr_dict, string method, array args) | 向一个资源RPC地址发起GET(阻塞)调用 |
| array remote_get_multi(array address_list, string method, array args) | 向一组资源RPC地址发起GET(阻塞)调用 |
| void remote_post(RpcAddress addr_dict, string method, array args) | 向一个资源RPC地址发起POST(非阻塞)调用 |
| void remote_post_multi(array address_list, string method, array args) | 向一组资源RPC地址发起POST(非阻塞)调用 |
SimpleRpcEntry.gs
主要功能是用来自定义资源类别及定位函数
| 函数原型 | 函数作用 |
|---|---|
| void set_entries(map entries) | 增加自定义的资源类别及其定位函数 |
| RpcEntry get_entry(RpcAddress addr_dict, string method, array method_args, bool is_post) | 根据资源RPC地址以及传入的方法名等参数获取资源在本节点上的定位信息 |
simple_rpc.gs
| 函数原型 | 函数作用 |
|---|---|
| void setup(map args) | 设置一些必要的或者可选的配置 |
| void start() | 开启服务 |
| void stop() | 停止服务 |
| array get(string host, int port, string target, string method, ...) | 阻塞式调用目标主机上目标对象的指定方法 |
| void post(string host, int port, string target, string method, ...) | 非阻塞式调用目标主机上目标对象的指定方法 |
| array all_get(array host_port_list, string target, string method, ...) | 阻塞式调用一组目标主机上目标对象的指定方法 |
| void all_post(array host_port_list, string target, string method, ...) | 非阻塞式调用一组目标主机上目标对象的指定方法 |
类
RpcAddress
RPC地址
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| rpc_address | string | nil | 可选 | 字符串形式的RPC地址 |
| id | string | "" | 可选 | 调用目标所属的服务器的ID 通常格式为IP:PORT,例如127.0.0.1:8888 也可以使用一些特殊标识符,如用"/"表示一个特定的服务器 |
| catalog | string | "" | 可选 | 调用目标所属的分类 |
| target | string | "" | 可选 | 调用目标 |
| relay | string | nil | 可选 | 调用目标无法直接访问时指定的中继目标 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| void update() | 更新自身数据 |
| RpcAddress new_by_dict(map dict) | 创建RPC地址 |
| RpcAddress new_by_args(string host, int port, string catalog, string target, string relay = nil) | 创建RPC地址 |
| string make_rpc_address(string id, string catalog, string target, string relay) | 生成字符串形式的RPC地址 |
| string get_rpc_address() | 获取字符串形式的RPC地址 |
| string get_ip() | 获取服务器IP地址 |
| int get_port() | 获取服务器端口 |
RpcRequest
RPC请求基础类
成员变量
| 变量名 | 类型 | 初 始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| cookie | int | -1 | 可选 | 请求结果投递队列标识 |
| cmd | string | nil | 可选 | 请求命令 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| bool send(array address_list, string method, array args) | 发送请求 |
RpcRequestGet
继承自 RpcRequest
RPC GET请求
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| address_list | array | nil | 可选 | 请求目标RPC地址列表 |
| q | queue | nil | 可选 | 请求结果的投递队列 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| array wait_result(mixed wait_time) | 等待请求结果 |
| void notify_result(int cookie, array address_list, mixed result) | 投递请求的处理结果 |
| bool send(array address_list, string method, array args) | 发送请求 |
| bool send_local(array address_list, string method, array args) | 发送请求(模拟发送,直接处理请求) |
| int new_timeout_result() | 获取请求处理超时的结果 |
| int new_failure_result() | 获取请求处理失败的结果 |
| array new_success_result(mixed value) | 将原始的处理结果转换为可传输的处理结果 |
| array restore_result(mixed result) | 将可传输类型的处理结果还为原始的处理结果 |
RpcRequestPost
继承自 RpcRequest
RPC POST请求
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| dispatcher | object | nil | 可选 | 发送请求 的调度对象 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| bool send(array address_list, string method, array args) | 发送请求 |
| bool send_local(object worker, array address_list, string method, array args) | 发送请求(伪发送,本地直接处理请求) |
RpcResponse
处理RPC请求的基础类
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| cookie | int | -1 | 可选 | 请求结果的投递队列标识 |
| callback | function | nil | 可选 | 请求结果的回调函数 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| void execute(RpcAddress addr_dict, string method, array args) | 执行请求 |
RpcResponseGet
继承自 RpcResponse
处理RPC GET请求
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| cmd | string | nil | 可选 | GET请求的处理结果的命令名称 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| void execute(RpcAddress addr_dict, string method, array args) | 执行请求 |
RpcResponseLocalGet
继承自 RpcResponse
处理RPC GET请求(本地模式)
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|
成员方法
| 函数原型 | 函数作用 |
|---|---|
| void execute(RpcAddress addr_dict, string method, array args) | 执行请求 |
RpcResponsePost
继承自 RpcResponse
处理RPC POST请求
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| worker | object | nil | 可选 | 处理请求的对象 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| void execute(RpcAddress addr_dict, string method, array args) | 执行请求 |
RpcEntry
RPC请求执行器
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| ob | object | nil | 可选 | 目标对象 |
| method | string | nil | 可选 | 方法名称 |
| args | array | nil | 可选 | 参数列表 |
成员方法
| 函数原型 | 函数作用 |
|---|---|
| bool is_ok() | 执行器是否正常 |
| mixed execute() | 执行请求 |
RpcEntryCall
继承自 RpcEntry
RPC请求执行器 - call
参数列表: [ ob_file, ob方法名称, 参数1, 参数2, ...]
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|
成员方法
| 函数原型 | 函数作用 |
|---|---|
| mixed execute() | 执行请求 |
RpcEntryPing
继承自 RpcEntry
RPC请求执行器 - ping
参数列表: [ 参数1 ]
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|
成员方法
| 函数原型 | 函数作用 |
|---|---|
| mixed execute() | 执行请求 |
RpcEntryEfun
继承自 RpcEntry
RPC请求执行器 - efun
参数列表: [ 参数1, 参数2, ...]
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|
成员方法
| 函数原型 | 函数作用 |
|---|---|
| mixed execute() | 执行请求 |
RpcEntryRelayGet
继承自 RpcEntry
RPC请求执行器 - 通过中继目标执行调用GET
参数列表: [ 参数1, 参数2, ...]
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|
成员方法
| 函数原型 | 函数作用 |
|---|---|
| mixed execute() | 执行请求 |
RpcEntryRelayPost
继承自 RpcEntry
RPC请求执行器 - 通过中继目标执行调用POST
成员变量
| 变量名 | 类型 | 初始值 | 须初 始化 | 描述 |
|---|
成员方法
| 函数原型 | 函数作用 |
|---|---|
| mixed execute() | 执行请求 |
RpcEntryObject
继承自 RpcEntry
RPC请求执行器 - 通过对象执行调用
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|
成员方法
| 函数原型 | 函数作用 |
|---|---|
| mixed execute() | 执行请求 |
RpcConfig
RPC配置类
成员变量
| 变量名 | 类型 | 初始值 | 须初始化 | 描述 |
|---|---|---|---|---|
| server_port | mixed | nil | 可选 | RPC服务的侦听端口(server_port优先起效, int或者array类型) |
| server_ports | mixed | nil | 可选 | rpc服务的侦听端口(server_port优先起效,int或者array类型) |
| server_para | map | nil | 可选 | 服务端一侧的连接参数 |
| client_para | map | nil | 可选 | 客户端一侧的连接参数 |
| post_coroutine_num | int | 0 | 可选 | RPC POST请求的处理协程数量 |
| connection_life_seconds | int | 0 | 可选 | 连接的存活时长 |
| rpc_get_timeout_seconds | int | 0 | 可选 | 每次远程调用本地的超时时长 |
| rpc_get_failure_retry_times | int | 0 | 可选 | 远程阻塞调用失败重试次数 |
| not_local_optimize | bool | false | 可选 | 本服务器之间的RPC是否不优化(默认优化) |
| check_local_rpc_address | function | nil | 可选 | 用于检测是否本服务器地址的方法 在not_local_optimize为false时生效 |
成员方法
| 函数原型 | 函数作用 |
|---|
样例
#pragma parallel
import gs.lang.*;
import gs.util.*;
import pkg.simple_rpc;
public int calc(...)
{
int total = 0;
for (int v : $<)
total += v;
return total;
}
public void say(string word)
{
printf("%s\n", word);
}
public void test1()
{
// 启动配置
map cfg = {
"server_port" : [
12345,
12346,
],
"server_para" : {
"port_recv_size" : 1024 * 256,
},
"client_para" : {
"port_recv_size" : 1024 * 256,
},
};
simple_rpc.setup(cfg);
simple_rpc.start();
mixed success, result;
string target = __FILE__;
// 演示get/post
// 调用localhost:12345上target对象的calc函数
let success, result = simple_rpc.get("localhost", 12345, target, "calc", 1, 2, 3, 4, 5);
if (success)
printf("result: %d\n", result);
let success, result = simple_rpc.get("localhost", 12345, target, "say", "hello");
if (success)
printf("result: %M\n", result);
simple_rpc.post("localhost", 12345, target, "say", "post hello");
// 演示all_get/all_post
array host_port_list = [
[ "localhost", 12345 ],
[ "localhost", 12346 ]
];
array results = simple_rpc.all_get(host_port_list, target, "calc", 1, 2, 3, 4, 5);
gtest.test_equal(results.length(), host_port_list.length());
for (array one_result : results)
{
let success, result = one_result;
if (success)
printf("result: %d\n", result);
}
simple_rpc.all_post(host_port_list, target, "say", "all post hello");
}