当前位置:网站首页>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)))
边栏推荐
- IMU 加热
- Accumulation and development -- the way of commercialization of open source companies
- JS preventDefault() 键盘输入限制 onmousewheel stopPropagation停止事件传播
- VAE:变分自编码器的理解与实现
- Solve the critical path in FJSP - with Matlab source code
- Jestson nano Object detection
- 智能合约安全——溢出漏洞
- Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
- 【笔记】《结网:互联网产品经理改变世界》
- Using Baidu easydl to realize chef hat recognition of bright kitchen and stove
猜你喜欢

Self-adaptive multi-objective evolutionary algorithm for flexible job shop scheduling with fuzzy pro

力扣 1331. 数组序号转换

SQL审核工具自荐Owls

Adobe XD web design tutorial

Using CPLEX to solve small-scale energy-efficient FJSP
![[data analysis] realize SVDD decision boundary visualization based on MATLAB](/img/3b/72bb459a357f836b52aa79171d75fc.png)
[data analysis] realize SVDD decision boundary visualization based on MATLAB

企业级分布式爬虫框架入门

Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix

英文翻译意大利语-批量英文翻译意大利语工具免费

Transformer for anomaly detection - instra "painting transformer for anomaly detection"
随机推荐
【笔记】《结网:互联网产品经理改变世界》
Understanding of PID
SaltStack入门
NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
[radar] radar signal online sorting based on kernel clustering with matlab code
Adobe Flash player 34.0.0.92 and available version modification methods (2021-01-23
文章翻译软件-批量免费翻译软件支持各大翻译接口
Scrapy Spider源码分析
Cvpr21 unsupervised anomaly detection cutpaste:self supervised learning for anomaly detection and localization
我的第二次博客——C语言
Mid 2022 summary
Application of TSDB in civil aircraft industry
Nips18(AD) - 利用几何增广的无监督异常检测《Deep Anomaly Detection Using Geometric Transformations》
Application of time series database in monitoring operation and maintenance platform
After several twists and turns, how long can the TSDB C-bit of influxdb last?
ES6's new data container map
Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
为什么在telnet登入界面下没有日志输出?
Pytorch:交叉熵损失(CrossEntropyLoss)以及标签平滑(LabelSmoothing)的实现
英文翻译意大利语-批量英文翻译意大利语工具免费