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 | 可选 | 请求命令 |