gRPC-Gateway helps you to provide your APIs in both gRPC and RESTful style at the same time.
官方网站:https://github.com/grpc-ecosystem/grpc-gateway
官方文档:https://grpc-ecosystem.github.io/grpc-gateway/
配置插件
下载
$ go get github.com/grpc-ecosystem/grpc-gateway
go: downloading github.com/grpc-ecosystem/grpc-gateway v1.16.0
go: github.com/grpc-ecosystem/grpc-gateway upgrade => v1.16.0
进入
cd $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/protoc-gen-grpc-gateway
$ go install
proto
syntax = "proto3";
package user;
//引入google api实现http转rpc
import "google/api/annotations.proto";
message UserRequest{
// [修饰符] 类型 字段名 = 标识符
string name = 1;
}
message UserResponse{
int32 id = 1;
string name = 2;
int32 age = 3;
repeated string title = 4; // 可变数组,即 slice 类型
}
service userInfoService {
rpc GetUserInfo(UserRequest) returns (UserResponse){
option (google.api.http) = {
post: "/v1/userInfo"
body: "*"
};
}
}
执行
protoc -I/usr/local/include -I. \
-I$GOPATH/src \
-I$GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway\@v1.16.0/third_party/googleapis/ \
--go_out=plugins=grpc:. \
proto/user.proto
生成pb代理文件
protoc -I/usr/local/include -I. \
-I$GOPATH/src \
-I$GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway\@v1.16.0/third_party/googleapis/ \
--grpc-gateway_out=logtostderr=true:. \
proto/user.proto
user 服务代码
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
proto "github.com/baxiang/go-note/grpc_gateway_service/proto"
"log"
"net"
)
//定义服务端 实现 约定的接口
type UserInfoService struct{}
//实现 interface
func (s *UserInfoService) GetUserInfo(ctx context.Context, req *proto.UserRequest) (resp *proto.UserResponse, err error) {
resp = &proto.UserResponse{
Id: 1,
Name: req.Name,
Age: 25,
Title: []string{"Java", "Go"},
}
return
}
func main() {
port := ":6666"
l, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("listen error: %v\n", err)
}
fmt.Printf("listen %s\n", port)
s := grpc.NewServer()
// 将 UserInfoService 注册到 gRPC
// 注意第二个参数 UserInfoServiceServer 是接口类型的变量
// 需要取地址传参
proto.RegisterUserInfoServiceServer(s, &UserInfoService{})
s.Serve(l)
}
执行服务
go run cmd/server/server.go
网关代码
package main
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
"log"
//多个服务引入多个包
proto "github.com/baxiang/go-note/grpc_gateway_service/proto"
"net/http"
)
var userPoint = "localhost:6666"
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Register gRPC server endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
//多个服务注册多次即可
err := proto.RegisterUserInfoServiceHandlerFromEndpoint(ctx, mux, userPoint, []grpc.DialOption{grpc.WithInsecure()})
if err != nil {
log.Fatal(err)
}
// Start HTTP server (and proxy calls to gRPC server endpoint)
log.Fatal(http.ListenAndServe(":8081", mux))
}
执行网关
go run cmd/gateway/gateway.go
测试
curl -H "Content-Type:application/json" -X POST --data '{"name":"test"}' http://127.0.0.1:8081/v1/userInfo
参考
https://blog.csdn.net/NikoKVCS/article/details/94568057
https://blog.csdn.net/zoeou/article/details/107593875