gRPC 快速体验(5):gRPC-Gateway

简介

gRPC-Gateway是protoc的一个插件。它读取gRPC服务定义并生成反向代理服务器,该服务器将RESTful JSON API转换为gRPC。此服务器根据gRPC定义中的自定义选项生成。

grpc-gateway文档

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-261LV8LZ-1662048112939)(https://cdn.nlark.com/yuque/0/2022/svg/12487795/1662001502595-799f0c0c-c9d4-40f5-ac1d-5616856a30d5.svg#clientId=uef1d0fb0-3fe7-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u46f7a2cf&margin=%5Bobject%20Object%5D&originHeight=760&originWidth=1120&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=ufb7b917c-de12-448e-b5fb-6a5e8924871&title=)]

安装依赖

go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway

proto 文件

gateway/pb/gateway.proto 文件;其中 需要 import google/api/annotations.proto;可以将 github.com\grpc-ecosystem\grpc-gateway 下的 third_party\googleapis\google\api 文件拷贝到项目目录下;执行protoc命令

protoc -I=./gateway/pb/ -I=./third_party/ –go_out=./gateway/ –go-grpc_out=./gateway/ -grpc-gateway_out=./gateway/ ./gateway/pb/gateway.proto

syntax = "proto3";

package stream;

option go_package = "pb/;gateway";
import "google/api/annotations.proto";

message Request {
  string name = 1;
}

message Reply {
  string content = 1;
}

service GatewayDemo {
  rpc Gate(Request) returns (Reply) {
    option (google.api.http) = {
      get: "/v1/gate/{name}"
    };
  }
}

Server端

server端与正常gRPC服务没什么差别;

package main

import (
	"context"
	"fmt"
	"net"

	gateway "github.com/grpc-demo/gateway/pb"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

type Gate struct {
	gateway.UnimplementedGatewayDemoServer
}

func (g Gate) Gate(ctx context.Context, req *gateway.Request) (*gateway.Reply, error) {
	return &gateway.Reply{
		Content: fmt.Sprintf("name:%s;", req.Name),
	}, nil
}

func main() {
	server := grpc.NewServer()
	gateway.RegisterGatewayDemoServer(server, &Gate{})
	reflection.Register(server)
	lis, _ := net.Listen("tcp", ":9000")
	_ = server.Serve(lis)
}

Http服务端

通过 gateway.RegisterGatewayDemoHandlerFromEndpoint 将http请求代理到给RPC服务;

package main

import (
	"context"
	"net/http"

	gateway "github.com/grpc-demo/gateway/pb"
	"github.com/grpc-ecosystem/grpc-gateway/runtime"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

const (
	// gRPC服务地址
	ServerAddr = "127.0.0.1:9000"

	ClientAddr = "127.0.0.1:8000"
)

func main() {
	ctx, cancelFunc := context.WithCancel(context.Background())
	defer cancelFunc()
	mux := runtime.NewServeMux()
	opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
	err := gateway.RegisterGatewayDemoHandlerFromEndpoint(ctx, mux, ServerAddr, opts)
	if err != nil {
		panic(err)
	}
	err = http.ListenAndServe(ClientAddr, mux)
	if err != nil {
		panic(err)
	}
}

启动测试

go run server.go

访问 127.0.0.1:8000/v1/gate/grpc
同时也可以通过grpcurl 测试服务端

grpcurl -plaintext -d ‘{“name”:“grpc”}’ localhost:9000 stream.GatewayDemo/Gate

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Devin_S

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值