HiveNetGRpc.server module¶
grpc服务模块
- class HiveNetGRpc.server.AIOGRpcServer(app_name: str, server_config: dict = {}, support_auths: dict = {}, before_server_start=None, after_server_start=None, before_server_stop=None, after_server_stop=None, logger=None, log_level: int = 20, load_i18n_para=None, **kwargs)[源代码]¶
基类:
ServerBaseFWgrpc服务(异步IO模式)
- __init__(app_name: str, server_config: dict = {}, support_auths: dict = {}, before_server_start=None, after_server_start=None, before_server_stop=None, after_server_stop=None, logger=None, log_level: int = 20, load_i18n_para=None, **kwargs)[源代码]¶
构造函数
- 参数
app_name (str) – 服务器名称
server_config (dict) –
default={}, 服务配置字典
run_config {dict} - 运行参数字典, 参数包括:
host {str} - 绑定的主机地址, 默认为’’
port {int} - 服务监听的端口, 默认为50051
workers {int} - 最大工作线程数, 默认为1
max_connect {int} - 允许最大连接数, 默认为20
enable_health_check {bool} - 是否启用健康检查服务, 默认为False
auto_enable_service {bool} - 是否自动启用加载的服务, 默认为True
use_ssl {bool} - 是否使用SSL/TLS - 默认为False
ssl {list[dict]} - 使用ssl时的证书配置, 允许送入多套, dict信息支持两种送入的方式
1、使用证书文件二进制数据 {‘cert’: crt证书bytes, ‘key’: 私钥bytes}
例如:
with open(‘server.pem’, ‘rb’) as f:
private_key = f.read() # 服务器端的私钥文件
with open(‘server.crt’, ‘rb’) as f:
certificate_chain = f.read() # 服务器端的公钥证书文件
ssl = {‘cert’: certificate_chain, ‘key’: private_key}
2、使用证书文件路径 {‘cert’: ‘/path/to/server.crt’, ‘key’: ‘/path/to/server.pem’}
root_certificates {str|bytes} - 客户端反向认证时(验证客户端证书)的客户端根证书, 即客户端的公钥证书文件
多客户端反向认证时, 客户端证书应基于同一个根证书签发, 这里使用根证书的公钥证书文件
1、字符形式传文件路径, 例如’/path/to/ca.crt’
2、字节数组形式,例如:
with open(‘ca.crt’, ‘rb’) as f:
root_certificates = f.read()
grpc_config {dict} - grpc其他的原生参数
options {list} - grpc选项数组, 可参考grpc的原生函数说明, 例如:
[(‘grpc.max_send_message_length’, 最大发送消息长度), (‘grpc.max_receive_message_length’, 最大接收消息长度)]
compression {} - grpc的压缩选项, 例如: grpc.compression.Gzip
handlers {} - An optional list of GenericRpcHandlers used for executing RPCs. More handlers may be added by calling add_generic_rpc_handlers any time before the server is started
interceptors {} - An optional list of ServerInterceptor objects that observe and optionally manipulate the incoming RPCs before handing them over to handlers. The interceptors are given control in the order they are specified
support_auths (dict) –
default={}, 服务器支持的验证对象字典, key为验证对象类型名(可以为类名), value为验证对象实例对象
注意: 支持的auth对象必须有auth_required这个修饰符函数
before_server_start (function) – default=None, 服务器启动前执行的函数对象, 传入服务自身(self)
after_server_start (function) – default=None, 服务器启动后执行的函数对象, 传入服务自身(self)
before_server_stop (function) – default=None, 服务器关闭前执行的函数对象, 传入服务自身(self)
after_server_stop (function) – default=None, 服务器关闭后执行的函数对象, 传入服务自身(self)
logger (Logger) – default=None, 自定义应用逻辑使用的日志对象
log_level (int) – default=logging.INFO, 一般信息的记录使用的日志级别
load_i18n_para (dict) –
default=None, 要装载的i18n语言文件配置
path {str} - 要加载的i18n字典文件路径, 如果填空代表程序运行的当前路径
prefix {str} - 要加载的i18n字典文件前缀
encoding {str} - 要加载的i18n字典文件的字符编码, 默认为’utf-8’
实现类自定义扩展参数 (-) –
- async add_service(service_uri: str, handler: Callable, call_mode: EnumCallMode = EnumCallMode.Simple, servicer_name: str = 'JsonService', **kwargs) CResult[源代码]¶
添加请求处理服务
- 参数
service_uri (str) – 服务唯一标识, 例如服务名或url路由
handler (Callable) –
请求处理函数, 应可同时支持同步或异步函数
函数格式统一为 func(request) -> xxx_pb2.RpcResponse
其中, request为请求字典:
{
‘request’: request/request_iterator, # 请求报文对象, 如果是流模式则为请求报文对象的迭代器
’context’: context, # 请求服务端上下文, grpc.ServicerContext
’call_mode’: call_mode # 调用模式
}
call_mode (EnumCallMode) – default=EnumCallMode.Simple, 调用模式
servicer_name (str) – default=’JsonService’, 服务对象名
实现类的自定义扩展参数 (-) –
- 返回
添加服务结果, result.code: ‘00000’-成功, ‘21405’-服务名已存在, 其他-异常
- 返回类型
- get_service_status(servicer_name='') EnumGRpcStatus[源代码]¶
获取服务可用状态
- 参数
servicer_name (string) – 要获取的服务名, 如果为’’则代表获取所有服务状态汇总
- async remove_service(service_uri: str, **kwargs) CResult[源代码]¶
移除请求处理服务
- 参数
service_uri (str) – 服务唯一标识, 例如服务名或url路由
实现类的自定义扩展参数 (-) –
- 返回
添加服务结果, result.code: ‘00000’-成功, ‘21403’-服务不存在, 其他-异常
- 返回类型
- set_service_status(servicer_name, status_code: EnumGRpcStatus)[源代码]¶
设置服务可用状态
- 参数
servicer_name (string) – 要设置的服务名, 如果为’’则代表设置所有服务
status_code (EnumGRpcStatus) – 要设置的服务状态
- class HiveNetGRpc.server.AIOGRpcServicer(service_name: str, pb2_module, pb2_grpc_module, error_response_func=None, logger: Logger = None)[源代码]¶
基类:
objectgrpc通用服务管理(异步IO模式, 注意不支持同步模式的GRpcServer)
注: 管理proto相关的通讯服务类
- async GRpcCallBidirectionalStream(request_iterator, context)[源代码]¶
双向流模式(BidirectionalStream)gRPC的标准接入服务接口
- 参数
request_iterator (iterator) –
请求对象迭代器(msg_pb2.RpcRequest), 单个对象与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- async GRpcCallClientSideStream(request_iterator, context)[源代码]¶
客户端流模式(ClientSideStream)gRPC的标准接入服务接口
- 参数
request_iterator (iterator) –
请求对象迭代器(msg_pb2.RpcRequest), 单个对象与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- GRpcCallHealthCheck(request, context)[源代码]¶
自定义的健康检查服务
- 参数
request (xxx_pb2.HealthRequest) –
请求对象, 与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- async GRpcCallServerSideStream(request, context)[源代码]¶
服务器流模式(ServerSideStream)gRPC的标准接入服务接口
- 参数
request (xxx_pb2.RpcRequest) –
请求对象, 与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- async GRpcCallSimple(request, context)[源代码]¶
简单模式(Simple)gRPC的标准接入服务接口
- 参数
request (xxx_pb2.RpcRequest) –
请求对象, 与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- __init__(service_name: str, pb2_module, pb2_grpc_module, error_response_func=None, logger: Logger = None)[源代码]¶
grpc通用服务管理
- 参数
service_name (str) – 服务名, 也就是proto文件中定义的服务名, 例如JsonService
pb2_module (module) – pb2模块对象, proto文件所生成的xx_pb2.py模块对象, 例如msg_json_pb2
pb2_grpc_module (module) – pb2_grpc模块对象, proto文件所生成的xx_pb2_grpc.py模块对象, 例如msg_json_pb2_grpc
error_response_func (function) –
default=None, 当遇到失败时生成响应报文的函数
函数格式 func(request, cresult) -> xxx_pb2.RpcResponse
注: 如果不设置, 出现异常不返回
logger (logging.Logger) – default=None, 日志对象
- add_service(service_uri: str, handler: Callable, call_mode: EnumCallMode = EnumCallMode.Simple, **kwargs) CResult[源代码]¶
添加请求处理服务
- 参数
service_uri (str) – 服务唯一标识, 例如服务名或url路由
handler (Callable) –
请求处理函数, 可同时支持同步或异步函数
函数格式统一为 func(request) -> xxx_pb2.RpcResponse
其中, request为请求字典:
{
‘request’: request/request_iterator, # 请求报文对象, 如果是流模式则为请求报文对象的迭代器
’context’: context, # 请求服务端上下文, grpc.ServicerContext
’call_mode’: call_mode # 调用模式
}
call_mode (EnumCallMode) – default=EnumCallMode.Simple, 服务调用模式
实现类的自定义扩展参数 (-) –
- 返回
添加服务结果, result.code: ‘00000’-成功, ‘21405’-服务名已存在, 其他-异常
- 返回类型
- clear_service(**kwargs) CResult[源代码]¶
清空请求处理服务
- 返回
清空结果, result.code: ‘00000’-成功, ‘21403’-服务不存在, 其他-异常
- 返回类型
- class HiveNetGRpc.server.GRpcServer(app_name: str, server_config: dict = {}, support_auths: dict = {}, before_server_start=None, after_server_start=None, before_server_stop=None, after_server_stop=None, logger=None, log_level: int = 20, load_i18n_para=None, **kwargs)[源代码]¶
-
grpc服务(同步模式)
- add_service(service_uri: str, handler: Callable, call_mode: EnumCallMode = EnumCallMode.Simple, servicer_name: str = 'JsonService', **kwargs) CResult[源代码]¶
添加请求处理服务
- 参数
service_uri (str) – 服务唯一标识, 例如服务名或url路由
handler (Callable) –
请求处理函数, 应可同时支持同步或异步函数
函数格式统一为 func(request) -> xxx_pb2.RpcResponse
其中, request为请求字典:
{
‘request’: request/request_iterator, # 请求报文对象, 如果是流模式则为请求报文对象的迭代器
’context’: context, # 请求服务端上下文, grpc.ServicerContext
’call_mode’: call_mode # 调用模式
}
call_mode (EnumCallMode) – default=EnumCallMode.Simple, 调用模式
servicer_name (str) – default=’JsonService’, 服务对象名
实现类的自定义扩展参数 (-) –
- 返回
添加服务结果, result.code: ‘00000’-成功, ‘21405’-服务名已存在, 其他-异常
- 返回类型
- class HiveNetGRpc.server.GRpcServicer(service_name: str, pb2_module, pb2_grpc_module, error_response_func=None, logger: Logger = None)[源代码]¶
-
grpc通用服务管理(同步模式)
- GRpcCallBidirectionalStream(request_iterator, context)[源代码]¶
双向流模式(BidirectionalStream)gRPC的标准接入服务接口
- 参数
request_iterator (iterator) –
请求对象迭代器(msg_pb2.RpcRequest), 单个对象与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- GRpcCallClientSideStream(request_iterator, context)[源代码]¶
客户端流模式(ClientSideStream)gRPC的标准接入服务接口
- 参数
request_iterator (iterator) –
请求对象迭代器(msg_pb2.RpcRequest), 单个对象与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- GRpcCallServerSideStream(request, context)[源代码]¶
服务器流模式(ServerSideStream)gRPC的标准接入服务接口
- 参数
request (xxx_pb2.RpcRequest) –
请求对象, 与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- GRpcCallSimple(request, context)[源代码]¶
简单模式(Simple)gRPC的标准接入服务接口
- 参数
request (xxx_pb2.RpcRequest) –
请求对象, 与msg.proto定义一致,
例如可通过request.para_json获取要执行的函数的入参信息
context (grpc.ServicerContext) –
服务端的上下文,
具体定义@see https://grpc.github.io/grpc/python/grpc.html#service-side-context
- HiveNetGRpc.server.isgenerator(obj)¶