grpc服务接口测试方案与实现

3 篇文章 0 订阅

优势

grpc服务集成RESTful Json 和grpc-gateway反向代理来实现对grpc服务的接口测试,省去了自己编写客户端代码的工作。
一句话描述:使用grpc-gateway反向代理插件将基于HTTP协议的RESTful JSON API转换为gRPC(即翻译),同时也免去自己去写grpc的客户端。

流程图

在这里插入图片描述

步骤

一、编写grpc服务测试工程

该工程使用maven进行管理

1.安装环境

在这里插入图片描述

2、创建测试工程

1)创建maven工程

修改pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.xueqiu</groupId>
<artifactId>grpctest</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<protobuf.version>3.7.1</protobuf.version>
<grpc.version>1.20.0</grpc.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>
./src/test/java/XmlPortfolio/DemoTest.xml
<!-- ${xmlFileName} -->
</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>

<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<!-- proto文件的所在路径 -->
<!-- <protoSourceRoot>${project.basedir}/src/main/resources/proto</protoSourceRoot> -->
<!-- 编译后java文件的输出路径,默认为${project.build.directory}/generated-sources/protobuf/java -->
<outputDirectory>src/main/java</outputDirectory>
<!-- 制定protoc编译器路径 -->
<!-- <protocExecutable></protocExecutable> -->
<!-- 定义生成的java文件输出路径 -->
<!--<outputDirectory>${project.build.sourceDirectory}</outputDirectory> -->
<!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件 -->
<clearOutputDirectory>false</clearOutputDirectory>
<protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.9.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal> <!--按照指定的插件进行编译,即按照GRPC协议编译protob文件-->
</goals>
</execution>
</executions>
</plugin>

</plugins>

<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions>

</build>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.7</version>
</dependency>

<dependency>
<groupId>com.xueqiu.infra</groupId>
<artifactId>xueqiu-grpc-lib</artifactId>
<version>2.1.6</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

</project>

最终的目录结构如下:
在这里插入图片描述

2)把proto文件放在src/main/proto目录下

修改proto文件的输出包,例如:
在这里插入图片描述

3)根据proto文件生成java文件,有两种方式:

1、使用命令生成,在项目根目录执行也可以

mvn protobuf:compile
mvn protobuf:compile-custom

2、使用maven插件生成,如图
在这里插入图片描述
3、会在target下的generated-sources/protobuf目录生成两个目录
在这里插入图片描述
4、把生成的这几个java文件统一移到src/main/java下的com.xueqiu.grpcgenerate包中,如图
在这里插入图片描述

5、编写客户端代码,如图
在这里插入图片描述

二、集成RESTful JSON gRPC-gateway

1.安装gprc-gateway

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go

注:安装失败见最后的注释

2.修改.proto文件

添加gateway的选项
针对proto文件中定义的接口,在里面自定义我们要对外开放的HTTP协议接口。
例如修改crm.proto文件,如下:

import "google/api/annotations.proto";
service CRMService {
    rpc  queryLiquidationWarningAccounts(LiqWarningAccountRequest) returns (LiqWarningAccountsResponse) {
        option (google.api.http) = {
        post: "/v1/queryAccount" //这个自定义接口是我们对外开放的HTTP请求接口
 body: "*"
 };
    }
}

3.生成grpc golang stub类文件,即生成的是客户端

protoc -I/Users/xueqiu/Downloads/grpctest/src/main/proto/ -I.   -I$GOPATH/src   -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis   --go_out=plugins=grpc:.   /Users/xueqiu/Downloads/grpctest/src/main/proto/crm.proto

4.生成反向代理代码

protoc -I/Users/xueqiu/Downloads/grpctest/src/main/proto/ -I.   -I$GOPATH/src   -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis   --plugin=protoc-gen-grpc=grpc_ruby_plugin   --grpc-gateway_out=logtostderr=true:.   /Users/xueqiu/Downloads/grpctest/src/main/proto/crm.proto

对应生成crm.pb.gw.go

5.把以上生成的go文件移到cmd的文件夹下面,最好根据接口或者服务划分到对应的目录下,这样方便管理,如图
在这里插入图片描述
备注:因为model是公共的,所以放在cmd下面,package为cmd即可。

6.编写proxy.go代码,即翻译代理
在这里插入图片描述

7.编译,生成可执行文件proxy
go build proxy.go
在这里插入图片描述

8.启动服务
1)启动RD的grpc server端服务,或者RD的server端服务已经启动。
注意,我发现RD的服务是占用8080端口,所以RESTful JSON API gateway的端口,我们对外开放8081端口,避免端口冲突,如下图的proxy.go
在这里插入图片描述

2)启动RESTful JSON API gateway

./proxy

三、接口测试

1.使用curl访问

curl -X POST "http://localhost:8081/v1/queryAccount" -d "{\"page\":{\"number\":1,\"size\":2}}"
返回:{"assets":[{"accountId":"3","currency":"USD","leverage":1,"accountType":"MARGIN"},{"accountId":"4","currency":"USD","leverage":1,"accountType":"MARGIN"}],"totalCount":8,"pageCount":4}

2.使用postman访问
在这里插入图片描述

注释

mac上安装go环境
https://blog.csdn.net/xiaoquantouer/article/details/79985650
golang安装gRpc
https://www.jianshu.com/p/dba4c7a6d608
安装官方安装命令:
go get google.golang.org/grpc
是安装不起的,会报:
package google.golang.org/grpc: unrecognized import path “google.golang.org/grpc”(https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
原因是这个代码已经转移到github上面了,但是代码里面的包依赖还是没有修改,还是google.golang.org这种,
所以不能使用go get的方式安装,正确的安装方式:
git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
cd $GOPATH/src/
go install google.golang.org/grpc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值