手把手教你使用grpc-gateway提供http服务

使用Grpc可以直接提供通信服务,但是如果要使用http来通信,那必须使用grpc-gateway,按照如下步骤进行操作

1 在原来提供grpc服务的proto文件中添加http服务的定义,然后将要提供服务的proto文件复制到grpc-gateway的文件夹下;

2 使用命令生成某种语言对应的源文件,因为这里gateway使用的是go, 所以生成go语言,总共会生成3个文件,pb.go后缀的文件供服务使用,pb.gw.go后缀的文件是gateway的,_grpc.pb.go后缀的文件是grpc服务使用的,如果在运行命令过程中报错找不到其它的proto文件,则可以在命令里添加-I 路径,或者在 investormargin.proto后面把需要的proto文件带在后面;

命令1:protoc -I .  -I /home/trade_dev/gopath/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/third_party/googleapis/ --go_out . --go_opt paths=source_relative --go-grpc_out . --go-grpc_opt paths=source_relative  investormargin.proto

命令2:protoc -I .  -I /home/trade_dev/gopath/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/third_party/googleapis/ --grpc-gateway_out . --grpc-gateway_opt logtostderr=true --grpc-gateway_opt paths=source_relative --grpc-gateway_opt generate_unbound_methods=true investormargin.proto

 3 编辑gateway使用的代码文件gw.go,将grpcServerEndpoint更改为运行grpc服务的ip和端口,因为grpc的使用优势就在于,只要服务端和客户端有相同的proto文件,即可以调用,我们这里grpc服务也运行在本机,所以ip这里写的是localhost,但是使用不同的端口,然后再把RegisterInvestorMarginServiceHandlerFromEndpoint改为步骤2中生成pb.gw.go后缀的文件中类似的名称的函数,只要修改这两个地方,就可以运行gateway服务了;

package main

import (
	"context"
	"flag"
	"net/http"

	"github.com/golang/glog"
	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
	"google.golang.org/grpc"

	gw "github.com/yourorg/yourprotos/gen/go/your/service/v1" // Update
)

var (
	// command-line options:
	// gRPC server endpoint
	grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:50051", "gRPC server endpoint")
)

func run() error {
	ctx := context.Background()
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	// Register gRPC server endpoint
	// Note: Make sure the gRPC server is running properly and accessible
	mux := runtime.NewServeMux()
	opts := []grpc.DialOption{grpc.WithInsecure()}
	err := gw.RegisterInvestorMarginServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
	if err != nil {
		return err
	}

	// Start HTTP server (and proxy calls to gRPC server endpoint)
	return http.ListenAndServe(":8081", mux)
}

func main() {
	flag.Parse()
	defer glog.Flush()

	if err := run(); err != nil {
		glog.Fatal(err)
	}
}

4 运行grpc的服务,grpc服务使用c++语言,所以对应的proto要生成c++文件,使用命令如下,如果在运行命令过程中报错找不到其它的proto文件,则可以在命令里添加-I 路径,或者在 investormargin.proto后面把需要的proto文件带在后面;:

命令1:protoc -I /home/trade_dev/gopath/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/third_party/googleapis --cpp_out=./src/proto investormargin.proto /home/trade_dev/gopath/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/third_party/googleapis/google/api/annotations.proto /home/trade_dev/gopath/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/third_party/googleapis/google/api/http.proto  --proto_path=./proto

命令2:protoc  -I /home/trade_dev/gopath/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/third_party/googleapis/ --grpc_out=./src/proto --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin investormargin.proto /home/trade_dev/gopath/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/third_party/googleapis/google/api/annotations.proto  /home/trade_dev/gopath/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.16.0/third_party/googleapis/google/api/http.proto  --proto_path=./proto

5 使用postman软件运行命令,调用http服务,运行结果如下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值