参考:
GitHub - grpc/grpc-go: The Go language implementation of gRPC. HTTP/2 based RPC
安装protobuf
1.brew install protobuf
2.protoc --version
安装grpc
1.go get google.golang.org/grpc
2.go get github.com/golang/protobuf/protoc-gen-go
生成 pb.go
1.protoc --go_out=plugins=grpc:. ServeRoute.proto
安装gin
1.go get github.com/gin-gonic/gin
测试运行
1.serve+client
2.serve+main
package main
import (
"github.com/gin-gonic/gin"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "grpc-test/proto"
"log"
"net/http"
"time"
)
const dateFormat = "2006-01-02 15:04:05"
const dateTimeFormat = "20060102150405"
func main() {
router := gin.Default()
router.GET("/test", Test)
router.GET("/grpc", Grpc)
router.Run(":9090")
}
func Test(c *gin.Context) {
c.String(http.StatusOK, "HelloWorld!")
}
func Grpc(c *gin.Context) {
conn, err := grpc.Dial("127.0.0.1:10086", grpc.WithInsecure())
if err != nil {
log.Fatalln(err)
}
rpc := pb.NewServeRouteClient(conn)
reqBody1 := &pb.Id{Id: 1}
res1, err := rpc.GetUser(context.Background(), reqBody1) //就像调用本地函数一样,通过serve1得到返回值
if err != nil {
log.Fatalln(err)
}
log.Println("message from serve: ", res1.Name)
reqBody2 := &pb.Name{Name: "HaHa"}
res2, err := rpc.GetActivity(context.Background(), reqBody2) //就像调用本地函数一样,通过serve2得到返回值
if err != nil {
log.Fatalln(err)
}
log.Println("message from serve: ", res2.Name)
c.String(http.StatusOK, "get grpc success,serve1:"+timeFormat(res1.Time, dateFormat))
}
func timeFormat(timeStamp int64, format ...string) string {
t := time.Unix(timeStamp/1e3, 0)
defaultFormat := dateTimeFormat
if len(format) > 0 {
defaultFormat = format[0]
}
return t.Format(defaultFormat)
}
访问localhost:9090/grpc
设置proxy
export GO111MODULE=on
export GOPROXY=goproxy.io
代码: