安裝工具鏈
go install google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
編寫proto
// hello.proto
syntax = "proto3";
package pb;
option go_package = "/pb";
service Hello {
rpc SayHello(HelloRequest) returns(HelloResponse){}
}
message HelloRequest{
string msg = 1;
}
message HelloResponse{
string msg = 1;
}
生成pb.go
生成的pb.go在proto目錄下斗搞,包名為 hello.proto中
option go_package
指定
# 在proto目錄下執(zhí)行
protoc -I=. --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
--grpc-gateway_out=. --grpc-gateway_opt=paths=source_relative \
./*.proto
實現(xiàn)server的業(yè)務(wù)邏輯
// 創(chuàng)建文件 server/hello.go
package server
import (
"context"
pb "grpcdemo/proto"
)
type HelloServer struct {
pb.UnimplementedHelloServer
}
func NewHelloServer() *HelloServer {
return &HelloServer{}
}
func (h *HelloServer) Greet(ctx context.Context, req *pb.HelloRequest) (*pb.HelloResponse, error) {
return &pb.HelloResponse{Msg: "test"}, nil
}
main.go 文件中啟動服務(wù)
package main
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
pb "grpcdemo/proto"
"grpcdemo/server"
"log"
"net/http"
"strings"
)
const PORT = "8888"
func main() {
err := RunServer()
if err != nil {
panic(err)
}
}
func RunServer() error {
httpMux := runHttpServer()
grpcS := runGrpcServer()
gatewayMux := runGatewayServer()
httpMux.Handle("/", gatewayMux)
return http.ListenAndServe(":"+PORT, grpcHandlerFunc(grpcS, httpMux))
}
func runGrpcServer() *grpc.Server {
s := grpc.NewServer()
pb.RegisterHelloServer(s, server.NewHelloServer())
reflection.Register(s)
return s
}
func runHttpServer() *http.ServeMux {
s := http.NewServeMux()
return s
}
func runGatewayServer() *runtime.ServeMux {
endpoint := ":" + PORT
gwmux := runtime.NewServeMux()
options := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := pb.RegisterHelloHandlerFromEndpoint(context.Background(), gwmux, endpoint, options)
if err != nil {
log.Fatal(err)
}
return gwmux
}
func grpcHandlerFunc(grpcServer *grpc.Server, httpHandler http.Handler) http.Handler {
return h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
httpHandler.ServeHTTP(w, r)
}
}), &http2.Server{})
}
測試響應(yīng)
http調(diào)用
curl -X POST http://localhost:8888/sayHello
---
{
"msg": "hello !"
}
grpc調(diào)用
新建client/main.go文件
// client/main.go
package main
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "grpcdemo/proto"
"log"
)
func main() {
conn, _ := grpc.Dial("localhost:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
c := pb.NewHelloClient(conn)
ctx := context.Background()
resp, err := c.SayHello(ctx, &pb.HelloRequest{Msg: "123"})
if err != nil {
log.Println(err)
return
}
log.Println(resp.Msg)
}