当前位置:网站首页>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-f6pu4PtL-1658989710154)(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)))
边栏推荐
- 用LEX(FLEX)生成PL语言的词法分析器
- Force buckle 1331. Array serial number conversion
- Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
- Accumulation and development -- the way of commercialization of open source companies
- Using CPLEX to solve small-scale energy-efficient FJSP
- 博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)
- Application of time series database in Hydropower Station
- Srs4.0 installation steps
- SaltStack配置管理
- Cvpr19 - adjust reference dry goods bag of tricks for image classification with revolutionary neural network
猜你喜欢

Rust Getting Started Guide (modules and engineering structures)

Rust 入门指南(rustup, cargo)

Powerbi time series analysis, prediction and visualization tutorial

Streamlit machine learning application development tutorial

英文翻译阿拉伯语-批量英文翻译阿拉伯语工具免费

Application value of MES production management system to equipment

Cvpr19 - adjust reference dry goods bag of tricks for image classification with revolutionary neural network

微信公众号授权登录后报redirect_uri参数错误的问题

Learn from Li Mu, deep learning - linear regression and basic optimization function

Tikz draw Gantt chart in FJSP -trans necessary
随机推荐
idea properties文件显示\u不显示中文的解决
Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
Solve the critical path in FJSP - with Matlab source code
SaltStack之salt-ssh
机器学习 --- 模型评估、选择与验证
RFs self study notes (III): clutter model - first determine the number with Poisson distribution, and then use uniform distribution as probability distribution
微信公众号授权登录后报redirect_uri参数错误的问题
Rust 入门指南(rustup, cargo)
Update of objects in ES6
SaltStack进阶
博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)
Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
测试开发备忘
Validate hardware DDR design with Xilinx MIG
Kotlin Android development novice tutorial
Pandownload revival tutorial
剑指 Offer II 109. 开密码锁
Adobe XD web design tutorial
RTC clock: a calendar
VAE:变分自编码器的理解与实现