|
| 1 | +# 带入gRPC:让你的服务同时提供 HTTP 接口 |
| 2 | + |
| 3 | +项目地址:https://github.com/EDDYCJY/go-grpc-example |
| 4 | + |
| 5 | +## 前言 |
| 6 | + |
| 7 | +- 接口需要提供给其他业务组访问,但是 RPC 协议不同无法内调,对方问能否走 HTTP 接口,怎么办? |
| 8 | + |
| 9 | +- 微信(公众号、小程序)等第三方回调接口只支持 HTTP 接口,怎么办 |
| 10 | + |
| 11 | +我相信你在实际工作中都会遇到如上问题,在 gRPC 中都是有解决方案的,本章节将会进行介绍 🤔 |
| 12 | + |
| 13 | +## 为什么可以同时提供 HTTP 接口 |
| 14 | + |
| 15 | +关键一点,gRPC 的协议是基于 HTTP/2 的,因此应用程序能够在单个 TCP 端口上提供 HTTP/1.1 和 gRPC 接口服务(两种不同的流量) |
| 16 | + |
| 17 | +## 怎么同时提供 HTTP 接口 |
| 18 | + |
| 19 | +### 检测协议 |
| 20 | + |
| 21 | +``` |
| 22 | +if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { |
| 23 | + server.ServeHTTP(w, r) |
| 24 | +} else { |
| 25 | + mux.ServeHTTP(w, r) |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### 流程 |
| 30 | + |
| 31 | +1. 检测请求协议是否为 HTTP/2 |
| 32 | +2. 判断 Content-Type 是否为 application/grpc(gRPC 的默认标识位) |
| 33 | +3. 根据协议的不同转发到不同的服务处理 |
| 34 | + |
| 35 | +## gRPC |
| 36 | + |
| 37 | +### TLS |
| 38 | + |
| 39 | +在前面的章节,为了便于展示因此没有简单封装 |
| 40 | + |
| 41 | +在本节需复用代码,重新封装了,可详见:[go-grpc-example](https://github.com/EDDYCJY/go-grpc-example/tree/master/pkg/gtls) |
| 42 | + |
| 43 | +### 目录结构 |
| 44 | + |
| 45 | +新建 simple_http_client、simple_http_server 目录,目录结构如下: |
| 46 | + |
| 47 | +``` |
| 48 | +go-grpc-example |
| 49 | +├── client |
| 50 | +│ ├── simple_client |
| 51 | +│ ├── simple_http_client |
| 52 | +│ └── stream_client |
| 53 | +├── conf |
| 54 | +├── pkg |
| 55 | +│ └── gtls |
| 56 | +├── proto |
| 57 | +├── server |
| 58 | +│ ├── simple_http_server |
| 59 | +│ ├── simple_server |
| 60 | +│ └── stream_server |
| 61 | +``` |
| 62 | + |
| 63 | +### Server |
| 64 | + |
| 65 | +在 simple_http_server 目录下新建 server.go,写入文件内容: |
| 66 | + |
| 67 | +``` |
| 68 | +package main |
| 69 | +
|
| 70 | +import ( |
| 71 | + "context" |
| 72 | + "log" |
| 73 | + "net/http" |
| 74 | + "strings" |
| 75 | +
|
| 76 | + "github.com/EDDYCJY/go-grpc-example/pkg/gtls" |
| 77 | + pb "github.com/EDDYCJY/go-grpc-example/proto" |
| 78 | +
|
| 79 | + "google.golang.org/grpc" |
| 80 | +) |
| 81 | +
|
| 82 | +type SearchService struct{} |
| 83 | +
|
| 84 | +func (s *SearchService) Search(ctx context.Context, r *pb.SearchRequest) (*pb.SearchResponse, error) { |
| 85 | + return &pb.SearchResponse{Response: r.GetRequest() + " HTTP Server"}, nil |
| 86 | +} |
| 87 | +
|
| 88 | +const PORT = "9003" |
| 89 | +
|
| 90 | +func main() { |
| 91 | + certFile := "../../conf/server/server.pem" |
| 92 | + keyFile := "../../conf/server/server.key" |
| 93 | + tlsServer := gtls.Server{ |
| 94 | + CertFile: certFile, |
| 95 | + KeyFile: keyFile, |
| 96 | + } |
| 97 | +
|
| 98 | + c, err := tlsServer.GetTLSCredentials() |
| 99 | + if err != nil { |
| 100 | + log.Fatalf("tlsServer.GetTLSCredentials err: %v", err) |
| 101 | + } |
| 102 | +
|
| 103 | + mux := GetHTTPServeMux() |
| 104 | +
|
| 105 | + server := grpc.NewServer(grpc.Creds(c)) |
| 106 | + pb.RegisterSearchServiceServer(server, &SearchService{}) |
| 107 | +
|
| 108 | + http.ListenAndServeTLS(":"+PORT, |
| 109 | + certFile, |
| 110 | + keyFile, |
| 111 | + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 112 | + if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { |
| 113 | + server.ServeHTTP(w, r) |
| 114 | + } else { |
| 115 | + mux.ServeHTTP(w, r) |
| 116 | + } |
| 117 | +
|
| 118 | + return |
| 119 | + }), |
| 120 | + ) |
| 121 | +} |
| 122 | +
|
| 123 | +func GetHTTPServeMux() *http.ServeMux { |
| 124 | + mux := http.NewServeMux() |
| 125 | + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 126 | + w.Write([]byte("eddycjy: go-grpc-example")) |
| 127 | + }) |
| 128 | +
|
| 129 | + return mux |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +- http.NewServeMux:创建一个新的 ServeMux,ServeMux 本质上是一个路由表。它默认实现了 ServeHTTP,因此返回 Handler 后可直接通过 HandleFunc 注册 pattern 和处理逻辑的方法 |
| 134 | +- http.ListenAndServeTLS:可简单的理解为提供监听 HTTPS 服务的方法,重点的协议判断转发,也在这里面 |
| 135 | + |
| 136 | +其实,你理解后就会觉得很简单,核心步骤:判断 -> 转发 -> 响应。我们改变了前两步的默认逻辑,仅此而已 |
| 137 | + |
| 138 | +### Client |
| 139 | + |
| 140 | +在 simple_http_server 目录下新建 client.go,写入文件内容: |
| 141 | + |
| 142 | +``` |
| 143 | +package main |
| 144 | +
|
| 145 | +import ( |
| 146 | + "context" |
| 147 | + "log" |
| 148 | +
|
| 149 | + "google.golang.org/grpc" |
| 150 | +
|
| 151 | + "github.com/EDDYCJY/go-grpc-example/pkg/gtls" |
| 152 | + pb "github.com/EDDYCJY/go-grpc-example/proto" |
| 153 | +) |
| 154 | +
|
| 155 | +const PORT = "9003" |
| 156 | +
|
| 157 | +func main() { |
| 158 | + tlsClient := gtls.Client{ |
| 159 | + ServerName: "go-grpc-example", |
| 160 | + CertFile: "../../conf/server/server.pem", |
| 161 | + } |
| 162 | + c, err := tlsClient.GetTLSCredentials() |
| 163 | + if err != nil { |
| 164 | + log.Fatalf("tlsClient.GetTLSCredentials err: %v", err) |
| 165 | + } |
| 166 | +
|
| 167 | + conn, err := grpc.Dial(":"+PORT, grpc.WithTransportCredentials(c)) |
| 168 | + if err != nil { |
| 169 | + log.Fatalf("grpc.Dial err: %v", err) |
| 170 | + } |
| 171 | + defer conn.Close() |
| 172 | +
|
| 173 | + client := pb.NewSearchServiceClient(conn) |
| 174 | + resp, err := client.Search(context.Background(), &pb.SearchRequest{ |
| 175 | + Request: "gRPC", |
| 176 | + }) |
| 177 | + if err != nil { |
| 178 | + log.Fatalf("client.Search err: %v", err) |
| 179 | + } |
| 180 | +
|
| 181 | + log.Printf("resp: %s", resp.GetResponse()) |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +## 验证 |
| 186 | + |
| 187 | +### gRPC Client |
| 188 | + |
| 189 | +``` |
| 190 | +$ go run client.go |
| 191 | +2018/10/04 14:56:56 resp: gRPC HTTP Server |
| 192 | +``` |
| 193 | + |
| 194 | +### HTTP/1.1 访问 |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | +## 总结 |
| 199 | + |
| 200 | +通过本章节,表面上完成了同端口提供双服务的功能,但实际上,应该是加深了 HTTP/2 的理解和使用,这才是本质 |
| 201 | + |
| 202 | +## 拓展 |
| 203 | + |
| 204 | +如果你有一个需求,是要**同时提供** RPC 和 RESTful JSON API 两种接口的,不要犹豫,点进去:[gRPC + gRPC Gateway 实践](https://segmentfault.com/a/1190000013339403) |
| 205 | + |
| 206 | +## 问题 |
| 207 | + |
| 208 | +你以为这个方案就万能了吗,不。Envoy Proxy 的支持就不完美,无法同时监听一个端口的两种流量 😤 |
0 commit comments