golang GRPC mock

golang GRPC mock

1.)前置条件

go install github.com/golang/protobuf/protoc-gen-go
go install github.com/carvalhorr/protoc-gen-mock

2.) 生成mock文件
proto文件greeter.proto如:

syntax = "proto3";

package carvalhorr.greeter;

option go_package = ".;greeter_service";

service Greeter {
	rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
	string name = 1;
}

message HelloReply {
	string message = 1;
}

运行命令生成mock

protoc --plugin protoc-gen-mock --go_out=plugins=grpc:greeter-service --mock_out=greeter-service greeter.proto

3.)用docker启动mock服务端

docker build -t greeter:1.0 .
docker run -p 10010:10010 -p 1068:1068 greeter:1.0   // 默认端口启动
docker run -e REST_PORT=1069 -e GRPC_PORT=50051 -p 10010:50051 -p 1010:1069 tag-version  //

相关链接

https://github.com/carvalhorr/protoc-gen-mock
https://rogchap.com/2019/06/25/mocking-grpc-in-go/
https://chromium.googlesource.com/external/github.com/grpc/grpc-go/+/refs/tags/v1.23.0/Documentation/gomock-example.md

上一篇:Mock server post使用


下一篇:postman mock测试