美文网首页
同一端口同一方法提供grpc和http流量支持

同一端口同一方法提供grpc和http流量支持

作者: lesterhnu | 来源:发表于2022-06-15 17:05 被阅读0次

项目地址 https://github.com/lesterhnu/grpcdemo

安装工具链

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目录下执行
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

实现server的业务逻辑

// 创建文件 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 文件中启动服务

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{})
}

测试响应

http调用

curl -X POST http://localhost:8888/sayHello
---
{
  "msg": "hello !"
}

grpc调用

新建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)
}

相关文章

网友评论

      本文标题:同一端口同一方法提供grpc和http流量支持

      本文链接:https://www.haomeiwen.com/subject/hjvxvrtx.html