-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.go
67 lines (59 loc) · 2 KB
/
main3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 使用 grpc-gateway 提供同端口同方法提供双流量支持
package main
import (
"context"
"log"
"net"
"net/http"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pb "github.com/pudongping/go-grpc-service/proto"
"github.com/pudongping/go-grpc-service/server"
"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"
)
// grpcHandlerFunc 将gRPC请求和HTTP请求分别调用不同的handler处理
func grpcHandlerFunc3(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
return h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
otherHandler.ServeHTTP(w, r)
}
}), &http2.Server{})
}
func main() {
// Create a listener on TCP port
lis, err := net.Listen("tcp", ":8091")
if err != nil {
log.Fatalln("Failed to listen:", err)
}
// 创建一个gRPC server对象
s := grpc.NewServer()
pb.RegisterTagServiceServer(s, server.NewTagServer())
// 注册反射服务,方便让 grpcurl 或者 grpcui 用作调试
// reflection 包是 gRPC 官方所提供的反射服务
reflection.Register(s)
// gRPC-Gateway mux
gwmux := runtime.NewServeMux()
dops := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err = pb.RegisterTagServiceHandlerFromEndpoint(context.Background(), gwmux, "127.0.0.1:8091", dops)
if err != nil {
log.Fatalln("Failed to register gwmux:", err)
}
mux := http.NewServeMux()
mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("pong"))
})
mux.Handle("/", gwmux)
// 定义HTTP server配置
gwServer := &http.Server{
Addr: "127.0.0.1:8091",
Handler: grpcHandlerFunc3(s, mux), // 请求的统一入口
}
log.Println("Serving on http://127.0.0.1:8091")
log.Fatalln(gwServer.Serve(lis)) // 启动HTTP服务
}