当前位置:网站首页>App自动化测试是怎么实现H5测试的
App自动化测试是怎么实现H5测试的
2022-07-28 17:39:00 【泽泽说测试】
微服务离不开gRPC
现在大多数企业都会采用微服务作为软件的架构,在这种架构的大背景下,微服务框架和协议广泛流行,而RPC也开始流行。 grpc 是基于RPC的框架,性能高,使用非常广泛。
grpc 由谷歌公司开发和维护,支持几乎所有的主流编程语言。 不管你用的是 Java, 还是 Python, 还是 Go, 还是 Ruby 等等,都可以使用他来实现远程的服务。

Protocol Buffers
grpc 默认使用 protocol buffers 作为序列化传输格式,通常会把传输的数据类型用一个带有.proto扩展名的普通文本文件来存储,不管是请求还是响应的数据都需要符合这里面定义的数据要求。
比如在进行用户验证时往往需要传入登录的用户信息,服务端返回 token 值,对应的 proto 文件描述, 如果请求或者响应数据太大,不能一次获取完,可以通过 stream 流信息持续传输,此时在类前面加 stream 关键字。
// login.proto
service User{
// login
rpc Login (LoginReqeust ) returns (LoginReply ) {}
// stream
rpc GetImage(LoginRequest) return (stream LoginReply ){}
}
// 登录请求数据
message LoginReqeust {
string username = 1;
string passwd = 2;
}
//登录响应数据
message LoginReply {
string token = 1;
string msg = 2;
}
proto 文件生成 gPRC 代码
以Python为例,首先需要安装 Python 相关的包。
pip install grpcio
pip install grpcio-tools
对应生成Python代码的命令, 注意 -I 表示 proto 文件所在的子目录:
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I protos login.proto
输入完命令以后,在目录下会多两个文件,一个叫 login_pb2.py, 里面报告了请求和响应相关的数据格式,还有一个叫 login_pb2_grpc.py, 包含了客户端和响应端的类。
生成的 login_pb2.grpc.py 中的代码…
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WzAvEuw6-1658921915132)(C:\Users\muji\Desktop\logseq_local_D__logseq笔记_markdown_1658920736\logseq笔记_markdown_1658920736\assets\image_1658915681955_0.png)]
创建gRPC服务
创建接口需要用到上面生成的 UserServicer 类。
import login_pb2_grpc
import login_pb2
class User(login_pb2_grpc.UserServicer):
def Login(self, request, context):
return login_pb2.LoginReply(msg=f"hello, {
request.username}",
token="It is my token")
运行服务:
import grpc
from concurrent import futures
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
login_pb2_grpc.add_UserServicer_to_server(
User(), server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
serve()
创建gRPC客户端
一个例子:
import grpc
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = login_pb2_grpc.UserStub(channel)
response = stub.Login(login_pb2.LoginRequest(username='you', passwd="123"))
print("Client received: " + response.msg)
protocol buffer 转成 json
客户端拿到响应结果之后,可以通过类属性获取字段:
response.msg
response.token
有时候我们会把它转换成 json 格式,方便获取整个响应。此时可以使用 protobuf 这个库,先安装:
pip install protobuf
接下来使用转换成 json 格式的方法就可以了。
from google.protobuf.json_format import MessageToJson
import json
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = login_pb2_grpc.UserStub(channel)
response = stub.Login(login_pb2.LoginRequest(username='you', passwd="123"))
print(json.loads(MessageToJson(response)))
边栏推荐
- SQL审核工具自荐Owls
- Rust Getting Started Guide (crite Management)
- SaltStack系统初始化
- Pyg builds heterogeneous graph attention network han to realize DBLP node prediction
- TSDB and blockchain
- SaltStack进阶
- 博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)
- Iclr21 (classification) - future classic "vit" an image is worth 16x16 words (including code analysis)
- BLDC 6-step commutation simulink
- Pandownload revival tutorial
猜你喜欢

Share several coding code receiving verification code platforms, which will be updated in February 2022

Application of time series database in monitoring operation and maintenance platform

文章翻译软件-批量免费翻译软件支持各大翻译接口

Validate hardware DDR design with Xilinx MIG

source insight项目导入和使用教程

JS modify table font and table border style

Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation

Transformer for anomaly detection - instra "painting transformer for anomaly detection"

Powerbi time series analysis, prediction and visualization tutorial

英文翻译意大利语-批量英文翻译意大利语工具免费
随机推荐
BLDC 6步换相 simulink
Pyg builds heterogeneous graph attention network han to realize DBLP node prediction
【笔记】《启示录》:产品经理的实践经验与反省清单
SQL审核工具自荐Owls
企业级分布式爬虫框架入门
Application of time series database in Hydropower Station
SaltStack入门
英语文章翻译-英语文章翻译软件-免费批量翻译
Rust 入门指南(modules 和工程结构)
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
SaltStack之return与job管理
Application of time series database in cigarette factory
顺序线性表——课上练
Streamlit machine learning application development tutorial
New this prototype precompiled exercise
SaltStack常用的模块
Force buckle 1331. Array serial number conversion
Avoidance Adjusted Climbrate
SaltStack之数据系统
Application of time series database in museum environment detection