grpc-gateway - Go语言中文社区

grpc-gateway


1.环境

win10,eclipse

安装好go(gopath,gobin 设置好)

a. 安装好 grpc-go

go get -u google.golang.org/grpc

如果被墙,git clone 到go path 相关目录 

git clone https://github.com/grpc/grpc-go

b.安装 go get -u github.com/golang/protobuf/protoc-gen-go

c. 安装 Grpc-gateway

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

d.生成proxy 基本依赖

protoc -I. -I%GOPATH%/src -I%GOPATH%/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis --go_out=plugins=grpc:. helloWorld.proto

e:main类

package main

import (

"flag"

"net/http"

"github.com/golang/glog"

"github.com/grpc-ecosystem/grpc-gateway/runtime"

"golang.org/x/net/context"

"google.golang.org/grpc"

)

var (

echoEndpoint = flag.String("echo_endpoint", "localhost:9090", "endpoint of YourService")

)

func run() error {

ctx := context.Background()

ctx, cancel := context.WithCancel(ctx)

defer cancel()

mux := runtime.NewServeMux()

opts := []grpc.DialOption{grpc.WithInsecure()}

err := RegisterHellowordServiceHandlerFromEndpoint(ctx, mux, *echoEndpoint, opts)

if err != nil {

return err

}

return http.ListenAndServe(":8080", mux)

}

func main() {

flag.Parse()

defer glog.Flush()

if err := run(); err != nil {

glog.Fatal(err)

}

}

f.启动9090端口server java类,启动poxy main

g: post man 测试

版权声明:本文来源简书,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.jianshu.com/p/2fe0516df5fb
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-02 14:51:04
  • 阅读 ( 948 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢