5.2 Hello World
這節將開始編寫一個複雜的Hello World,涉及到許多的知識,建議大家認真思考其中的概念
需求
由於本實踐偏向Grpc+Grpc Gateway的方面,我們的需求是同一個服務端支援Rpc和Restful Api,那麼就意味著http2、TLS等等的應用,功能方面就是一個服務端能夠接受來自grpc和Restful Api的請求並響應
一、初始化目錄
我們先在$GOPATH中新建grpc-hello-world資料夾,我們專案的初始目錄目錄如下:
grpc-hello-world/
├── certs
├── client
├── cmd
├── pkg
├── proto
│ ├── google
│ │ └── api
└── server
certs:證書憑證client:客戶端cmd:命令列pkg:第三方公共模組proto:protobuf的一些相關檔案(含.proto、pb.go、.pb.gw.go),google/api中用於存放annotations.proto、http.protoserver:服務端
二、製作證書
在服務端支援Rpc和Restful Api,需要用到TLS,因此我們要先製作證書
進入certs目錄,生成TLS所需的公鑰金鑰檔案
私鑰
openssl genrsa -out server.key 2048
openssl ecparam -genkey -name secp384r1 -out server.key
openssl genrsa:生成RSA私鑰,命令的最後一個引數,將指定生成金鑰的位數,如果沒有指定,預設512openssl ecparam:生成ECC私鑰,命令為橢圓曲線金鑰引數生成及操作,本文中ECC曲線選擇的是secp384r1
自簽名公鑰
openssl req -new -x509 -sha256 -key server.key -out server.pem -days 3650
openssl req:生成自簽名證書,-new指生成證書請求、-sha256指使用sha256加密、-key指定私鑰檔案、-x509指輸出證書、-days 3650為有效期,此後則輸入證書擁有者資訊
填寫資訊
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:grpc server name
Email Address []:
三、proto
編寫
1、 google.api
我們看到proto目錄中有google/api目錄,它用到了google官方提供的兩個api描述檔案,主要是針對grpc-gateway的http轉換提供支援,定義了Protocol Buffer所擴充套件的HTTP Option
annotations.proto檔案:
// Copyright (c) 2015, Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
package google.api;
import "google/api/http.proto";
import "google/protobuf/descriptor.proto";
option java_multiple_files = true;
option java_outer_classname = "AnnotationsProto";
option java_package = "com.google.api";
extend google.protobuf.MethodOptions {
// See `HttpRule`.
HttpRule http = 72295728;
}
http.proto檔案:
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
package google.api;
option cc_enable_arenas = true;
option java_multiple_files = true;
option java_outer_classname = "HttpProto";
option java_package = "com.google.api";
// Defines the HTTP configuration for a service. It contains a list of
// [HttpRule][google.api.HttpRule], each specifying the mapping of an RPC method
// to one or more HTTP REST API methods.
message Http {
// A list of HTTP rules for configuring the HTTP REST API methods.
repeated HttpRule rules = 1;
}
// Use CustomHttpPattern to specify any HTTP method that is not included in the
// `pattern` field, such as HEAD, or "*" to leave the HTTP method unspecified for
// a given URL path rule. The wild-card rule is useful for services that provide
// content to Web (HTML) clients.
message HttpRule {
// Selects methods to which this rule applies.
//
// Refer to [selector][google.api.DocumentationRule.selector] for syntax details.
string selector = 1;
// Determines the URL pattern is matched by this rules. This pattern can be
// used with any of the {get|put|post|delete|patch} methods. A custom method
// can be defined using the 'custom' field.
oneof pattern {
// Used for listing and getting information about resources.
string get = 2;
// Used for updating a resource.
string put = 3;
// Used for creating a resource.
string post = 4;
// Used for deleting a resource.
string delete = 5;
// Used for updating a resource.
string patch = 6;
// Custom pattern is used for defining custom verbs.
CustomHttpPattern custom = 8;
}
// The name of the request field whose value is mapped to the HTTP body, or
// `*` for mapping all fields not captured by the path pattern to the HTTP
// body. NOTE: the referred field must not be a repeated field.
string body = 7;
// Additional HTTP bindings for the selector. Nested bindings must
// not contain an `additional_bindings` field themselves (that is,
// the nesting may only be one level deep).
repeated HttpRule additional_bindings = 11;
}
// A custom pattern is used for defining custom HTTP verb.
message CustomHttpPattern {
// The name of this custom HTTP verb.
string kind = 1;
// The path matched by this custom verb.
string path = 2;
}
hello.proto
這一小節將編寫Demo的.proto檔案,我們在proto目錄下新建hello.proto檔案,寫入檔案內容:
syntax = "proto3";
package proto;
import "google/api/annotations.proto";
service HelloWorld {
rpc SayHelloWorld(HelloWorldRequest) returns (HelloWorldResponse) {
option (google.api.http) = {
post: "/hello_world"
body: "*"
};
}
}
message HelloWorldRequest {
string referer = 1;
}
message HelloWorldResponse {
string message = 1;
}
在hello.proto檔案中,引用了google/api/annotations.proto,達到支援HTTP Option的效果
- 定義了一個
serviceRPC服務HelloWorld,在其內部定義了一個HTTP Option的POST方法,HTTP響應路徑為/hello_world - 定義
message型別HelloWorldRequest、HelloWorldResponse,用於響應請求和返回結果
編譯
在編寫完.proto檔案後,我們需要對其進行編譯,就能夠在server中使用
進入proto目錄,執行以下命令
# 编译google.api
protoc -I . --go_out=plugins=grpc,Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. google/api/*.proto
#编译hello_http.proto为hello_http.pb.proto
protoc -I . --go_out=plugins=grpc,Mgoogle/api/annotations.proto=grpc-hello-world/proto/google/api:. ./hello.proto
#编译hello_http.proto为hello_http.pb.gw.proto
protoc --grpc-gateway_out=logtostderr=true:. ./hello.proto
執行完畢後將生成hello.pb.go和hello.gw.pb.go,分別針對grpc和grpc-gateway的功能支援
四、命令列模組 cmd
介紹
這一小節我們編寫命令列模組,為什麼要獨立出來呢,是為了將cmd和server兩者解耦,避免混淆在一起。
我們採用 Cobra 來完成這項功能,Cobra既是建立強大的現代CLI應用程式的庫,也是生成應用程式和命令檔案的程式。提供了以下功能:
- 簡易的子命令列模式
- 完全相容posix的命令列模式(包括短和長版本)
- 巢狀的子命令
- 全域性、本地和級聯
flags - 使用
Cobra很容易的生成應用程式和命令,使用cobra create appname和cobra add cmdname - 智慧提示
- 自動生成commands和flags的幫助資訊
- 自動生成詳細的help資訊
-h,--help等等 - 自動生成的bash自動完成功能
- 為應用程式自動生成手冊
- 命令別名
- 定義您自己的幫助、用法等的靈活性。
- 可選與viper緊密整合的apps
編寫server
在編寫cmd時需要先用server進行測試關聯,因此這一步我們先寫server.go用於測試
在server模組下 新建server.go檔案,寫入測試內容:
package server
import (
"log"
)
var (
ServerPort string
CertName string
CertPemPath string
CertKeyPath string
)
func Serve() (err error){
log.Println(ServerPort)
log.Println(CertName)
log.Println(CertPemPath)
log.Println(CertKeyPath)
return nil
}
編寫cmd
在cmd模組下 新建root.go檔案,寫入內容:
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "grpc",
Short: "Run the gRPC hello-world server",
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
新建server.go檔案,寫入內容:
package cmd
import (
"log"
"github.com/spf13/cobra"
"grpc-hello-world/server"
)
var serverCmd = &cobra.Command{
Use: "server",
Short: "Run the gRPC hello-world server",
Run: func(cmd *cobra.Command, args []string) {
defer func() {
if err := recover(); err != nil {
log.Println("Recover error : %v", err)
}
}()
server.Serve()
},
}
func init() {
serverCmd.Flags().StringVarP(&server.ServerPort, "port", "p", "50052", "server port")
serverCmd.Flags().StringVarP(&server.CertPemPath, "cert-pem", "", "./certs/server.pem", "cert pem path")
serverCmd.Flags().StringVarP(&server.CertKeyPath, "cert-key", "", "./certs/server.key", "cert key path")
serverCmd.Flags().StringVarP(&server.CertName, "cert-name", "", "grpc server name", "server's hostname")
rootCmd.AddCommand(serverCmd)
}
我們在grpc-hello-world/目錄下,新建檔案main.go,寫入內容:
package main
import (
"grpc-hello-world/cmd"
)
func main() {
cmd.Execute()
}
講解
要使用Cobra,按照Cobra標準要建立main.go和一個rootCmd檔案,另外我們有子命令server
1、rootCmd: rootCmd表示在沒有任何子命令的情況下的基本命令
2、&cobra.Command:
Use:Command的用法,Use是一個行用法訊息Short:Short是help命令輸出中顯示的簡短描述Run:執行:典型的實際工作功能。大多數命令只會實作這一點;另外還有PreRun、PreRunE、PostRun、PostRunE等等不同時期的執行命令,但比較少用,具體使用時再檢視亦可
3、rootCmd.AddCommand:AddCommand向這父命令(rootCmd)新增一個或多個命令
4、serverCmd.Flags().StringVarP():
一般來說,我們需要在init()函式中定義flags和處理設定,以serverCmd.Flags().StringVarP(&server.ServerPort, "port", "p", "50052", "server port")為例,我們定義了一個flag,值儲存在&server.ServerPort中,長命令為--port,短命令為-p,,預設值為50052,命令的描述為server port。這一種呼叫方式成為Local Flags
我們延伸一下,如果覺得每一個子命令都要設一遍覺得很麻煩,我們可以採用Persistent Flags:
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
作用:
flag是可以持久的,這意味著該flag將被分配給它所分配的命令以及該命令下的每個命令。對於全域性標記,將標記作為根上的持久標誌。
另外還有Local Flag on Parent Commands、Bind Flags with Config、Required flags等等,使用到再 傳送 瞭解即可
測試
回到grpc-hello-world/目錄下執行go run main.go server,檢視輸出是否為(此時應為預設值):
2018/02/25 23:23:21 50052
2018/02/25 23:23:21 dev
2018/02/25 23:23:21 ./certs/server.pem
2018/02/25 23:23:21 ./certs/server.key
執行go run main.go server --port=8000 --cert-pem=test-pem --cert-key=test-key --cert-name=test-name,檢驗命令列引數是否正確:
2018/02/25 23:24:56 8000
2018/02/25 23:24:56 test-name
2018/02/25 23:24:56 test-pem
2018/02/25 23:24:56 test-key
若都無誤,那麼恭喜你cmd模組的編寫正確了,下一部分開始我們的重點章節!
五、服務端模組 server
編寫hello.go
在server目錄下新建檔案hello.go,寫入檔案內容:
package server
import (
"golang.org/x/net/context"
pb "grpc-hello-world/proto"
)
type helloService struct{}
func NewHelloService() *helloService {
return &helloService{}
}
func (h helloService) SayHelloWorld(ctx context.Context, r *pb.HelloWorldRequest) (*pb.HelloWorldResponse, error) {
return &pb.HelloWorldResponse{
Message : "test",
}, nil
}
我們建立了helloService及其方法SayHelloWorld,對應.proto的rpc SayHelloWorld,這個方法需要有2個引數:ctx context.Context用於接受上下文引數、r *pb.HelloWorldRequest用於接受protobuf的Request引數(對應.proto的message HelloWorldRequest)
*編寫server.go
這一小章節,我們編寫最為重要的服務端程式部分,涉及到大量的grpc、grpc-gateway及一些網路知識的應用
1、在pkg下新建util目錄,新建grpc.go檔案,寫入內容:
package util
import (
"net/http"
"strings"
"google.golang.org/grpc"
)
func GrpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
if otherHandler == nil {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
grpcServer.ServeHTTP(w, r)
})
}
return 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)
}
})
}
GrpcHandlerFunc函式是用於判斷請求是來源於Rpc客戶端還是Restful Api的請求,根據不同的請求註冊不同的ServeHTTP服務;r.ProtoMajor == 2也代表著請求必須基於HTTP/2
2、在pkg下的util目錄下,新建tls.go檔案,寫入內容:
package util
import (
"crypto/tls"
"io/ioutil"
"log"
"golang.org/x/net/http2"
)
func GetTLSConfig(certPemPath, certKeyPath string) *tls.Config {
var certKeyPair *tls.Certificate
cert, _ := ioutil.ReadFile(certPemPath)
key, _ := ioutil.ReadFile(certKeyPath)
pair, err := tls.X509KeyPair(cert, key)
if err != nil {
log.Println("TLS KeyPair err: %v\n", err)
}
certKeyPair = &pair
return &tls.Config{
Certificates: []tls.Certificate{*certKeyPair},
NextProtos: []string{http2.NextProtoTLS},
}
}
GetTLSConfig函式是用於取得TLS設定,在內部,我們讀取了server.key和server.pem這類證書憑證檔案
tls.X509KeyPair:從一對PEM編碼的資料中解析公鑰/私鑰對。成功則返回公鑰/私鑰對http2.NextProtoTLS:NextProtoTLS是談判期間的NPN/ALPN協議,用於HTTP/2的TLS設定tls.Certificate:返回一個或多個證書,實質我們解析PEM呼叫的X509KeyPair的函式宣告就是func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (Certificate, error),返回值就是Certificate
總的來說該函式是用於處理從證書憑證檔案(PEM),最終取得tls.Config作為HTTP2的使用引數
3、修改server目錄下的server.go檔案,該檔案是我們服務裡的核心檔案,寫入內容:
package server
import (
"crypto/tls"
"net"
"net/http"
"log"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
pb "grpc-hello-world/proto"
"grpc-hello-world/pkg/util"
)
var (
ServerPort string
CertName string
CertPemPath string
CertKeyPath string
EndPoint string
)
func Serve() (err error){
EndPoint = ":" + ServerPort
conn, err := net.Listen("tcp", EndPoint)
if err != nil {
log.Printf("TCP Listen err:%v\n", err)
}
tlsConfig := util.GetTLSConfig(CertPemPath, CertKeyPath)
srv := createInternalServer(conn, tlsConfig)
log.Printf("gRPC and https listen on: %s\n", ServerPort)
if err = srv.Serve(tls.NewListener(conn, tlsConfig)); err != nil {
log.Printf("ListenAndServe: %v\n", err)
}
return err
}
func createInternalServer(conn net.Listener, tlsConfig *tls.Config) (*http.Server) {
var opts []grpc.ServerOption
// grpc server
creds, err := credentials.NewServerTLSFromFile(CertPemPath, CertKeyPath)
if err != nil {
log.Printf("Failed to create server TLS credentials %v", err)
}
opts = append(opts, grpc.Creds(creds))
grpcServer := grpc.NewServer(opts...)
// register grpc pb
pb.RegisterHelloWorldServer(grpcServer, NewHelloService())
// gw server
ctx := context.Background()
dcreds, err := credentials.NewClientTLSFromFile(CertPemPath, CertName)
if err != nil {
log.Printf("Failed to create client TLS credentials %v", err)
}
dopts := []grpc.DialOption{grpc.WithTransportCredentials(dcreds)}
gwmux := runtime.NewServeMux()
// register grpc-gateway pb
if err := pb.RegisterHelloWorldHandlerFromEndpoint(ctx, gwmux, EndPoint, dopts); err != nil {
log.Printf("Failed to register gw server: %v\n", err)
}
// http服务
mux := http.NewServeMux()
mux.Handle("/", gwmux)
return &http.Server{
Addr: EndPoint,
Handler: util.GrpcHandlerFunc(grpcServer, mux),
TLSConfig: tlsConfig,
}
}
server流程剖析
我們將這一大塊程式碼,分成以下幾個部分來理解
一、啟動監聽
net.Listen("tcp", EndPoint)用於監聽本地的網路地址通知,它的函式原型func Listen(network, address string) (Listener, error)
引數:network必須傳入tcp、tcp4、tcp6、unix、unixpacket,若address為空或為0則會自動選擇一個埠號 返回值:透過檢視原始碼我們可以得知其返回值為Listener,結構體原型:
type Listener interface {
Accept() (Conn, error)
Close() error
Addr() Addr
}
透過分析得知,最後net.Listen會返回一個監聽器的結構體,返回給接下來的動作,讓其執行下一步的操作,它可以執行三類操作
Accept:接受等待並將下一個連線返回給ListenerClose:關閉ListenerAddr:返回Listener的網路地址
二、取得TLS
透過util.GetTLSConfig解析得到tls.Config,傳達給http.Server服務的TLSConfig設定項使用
三、建立內部服務
createInternalServer函式,是整個服務端的核心流轉部分
程式採用的是HTT2、HTTPS也就是需要支援TLS,因此在啟動grpc.NewServer前,我們要將認證的中介軟體註冊進去
而前面所取得的tlsConfig僅能給HTTP使用,因此第一步我們要建立grpc的TLS認證憑證
1、建立grpc的TLS認證憑證
新增引用google.golang.org/grpc/credentials的第三方包,它實作了grpc庫支援的各種憑證,該憑證封裝了客戶機需要的所有狀態,以便與伺服器進行身份驗證並進行各種斷言,例如關於客戶機的身份,角色或是否授權進行特定的呼叫
我們呼叫NewServerTLSFromFile來達到我們的目的,它能夠從輸入證書檔案和伺服器的金鑰檔案構造TLS證書憑證
func NewServerTLSFromFile(certFile, keyFile string) (TransportCredentials, error) {
//LoadX509KeyPair读取并解析来自一对文件的公钥/私钥对
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
//NewTLS使用tls.Config来构建基于TLS的TransportCredentials
return NewTLS(&tls.Config{Certificates: []tls.Certificate{cert}}), nil
}
2、設定grpc ServerOption
以grpc.Creds(creds)為例,其原型為func Creds(c credentials.TransportCredentials) ServerOption,該函式返回ServerOption,它為伺服器連線設定憑據
3、建立grpc服務端
函式原型:
func NewServer(opt ...ServerOption) *Server
我們在此處建立了一個沒有註冊服務的grpc服務端,還沒有開始接受請求
grpcServer := grpc.NewServer(opts...)
4、註冊grpc服務
pb.RegisterHelloWorldServer(grpcServer, NewHelloService())
5、建立grpc-gateway關聯元件
ctx := context.Background()
dcreds, err := credentials.NewClientTLSFromFile(CertPemPath, CertName)
if err != nil {
log.Println("Failed to create client TLS credentials %v", err)
}
dopts := []grpc.DialOption{grpc.WithTransportCredentials(dcreds)}
context.Background:返回一個非空的空上下文。它沒有被登出,沒有值,沒有過期時間。它通常由主函式、初始化和測試使用,並作為傳入請求的頂級上下文credentials.NewClientTLSFromFile:從客戶機的輸入證書檔案構造TLS憑證grpc.WithTransportCredentials:設定一個連線級別的安全憑據(例:TLS、SSL),返回值為type DialOptiongrpc.DialOption:DialOption選項設定我們如何設定連線(其內部具體由多個的DialOption組成,決定其設定連線的內容)
6、建立HTTP NewServeMux及註冊grpc-gateway邏輯
gwmux := runtime.NewServeMux()
// register grpc-gateway pb
if err := pb.RegisterHelloWorldHandlerFromEndpoint(ctx, gwmux, EndPoint, dopts); err != nil {
log.Println("Failed to register gw server: %v\n", err)
}
// http服务
mux := http.NewServeMux()
mux.Handle("/", gwmux)
runtime.NewServeMux:返回一個新的ServeMux,它的內部對映是空的;ServeMux是grpc-gateway的一個請求多路複用器。它將http請求與模式匹配,並呼叫相應的處理程式RegisterHelloWorldHandlerFromEndpoint:如函式名,註冊HelloWorld服務的HTTP Handle到grpc端點http.NewServeMux:分配并返回一个新的ServeMuxmux.Handle:為給定模式註冊處理程式
(帶著疑問去看程式)為什麼gwmux可以放入mux.Handle中?
首先我們看看它們的原型是怎麼樣的
(1)http.NewServeMux()
func NewServeMux() *ServeMux {
return new(ServeMux)
}
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
(2)runtime.NewServeMux?
func NewServeMux(opts ...ServeMuxOption) *ServeMux {
serveMux := &ServeMux{
handlers: make(map[string][]handler),
forwardResponseOptions: make([]func(context.Context, http.ResponseWriter, proto.Message) error, 0),
marshalers: makeMarshalerMIMERegistry(),
}
...
return serveMux
}
(3)http.NewServeMux()的Handle方法
func (mux *ServeMux) Handle(pattern string, handler Handler)
透過分析可得知,兩者NewServeMux都是最終返回serveMux,Handler中匯出的方法僅有ServeHTTP,功能是用於響應HTTP請求
我們回到Handle interface中,可以得出結論就是任何結構體,只要實作了ServeHTTP方法,這個結構就可以稱為Handle,ServeMux會使用該Handler呼叫ServeHTTP方法處理請求,這也就是自定義Handler
而我們這裡正是將grpc-gateway中註冊好的HTTP Handler無縫的植入到net/http的Handle方法中
補充:在go中任何結構體只要實作了與介面相同的方法,就等同於實作了介面
7、註冊具體服務
if err := pb.RegisterHelloWorldHandlerFromEndpoint(ctx, gwmux, EndPoint, dopts); err != nil {
log.Println("Failed to register gw server: %v\n", err)
}
在這段程式碼中,我們利用了前幾小節的
- 上下文
gateway-grpc的請求多路複用器- 服務網路地址
- 設定好的安全憑據
註冊了HelloWorld這一個服務
四、建立tls.NewListener
func NewListener(inner net.Listener, config *Config) net.Listener {
l := new(listener)
l.Listener = inner
l.config = config
return l
}
NewListener將會建立一個Listener,它接受兩個引數,第一個是來自內部Listener的監聽器,第二個引數是tls.Config(必須包含至少一個證書)
五、服務開始接受請求
在最後我們呼叫srv.Serve(tls.NewListener(conn, tlsConfig)),可以得知它是http.Server的方法,並且需要一個Listener作為引數,那麼Serve內部做了些什麼事呢?
func (srv *Server) Serve(l net.Listener) error {
defer l.Close()
...
baseCtx := context.Background() // base is always background, per Issue 16220
ctx := context.WithValue(baseCtx, ServerContextKey, srv)
for {
rw, e := l.Accept()
...
c := srv.newConn(rw)
c.setState(c.rwc, StateNew) // before Serve can return
go c.serve(ctx)
}
}
粗略的看,它建立了一個context.Background()上下文物件,並呼叫Listener的Accept方法開始接受外部請求,在取得到連線資料後使用newConn建立連線物件,在最後使用goroutine的方式處理連線請求,達到其目的
補充:對於HTTP/2支援,在呼叫Serve之前,應將srv.TLSConfig初始化為提供的Listener的TLS設定。如果srv.TLSConfig非零,並且在Config.NextProtos中不包含字串h2,則不啟用HTTP/2支援
六、驗證功能
編寫測試客戶端
在grpc-hello-world/下新建目錄client,新建client.go檔案,新增內容:
package main
import (
"log"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
pb "grpc-hello-world/proto"
)
func main() {
creds, err := credentials.NewClientTLSFromFile("../certs/server.pem", "dev")
if err != nil {
log.Println("Failed to create TLS credentials %v", err)
}
conn, err := grpc.Dial(":50052", grpc.WithTransportCredentials(creds))
defer conn.Close()
if err != nil {
log.Println(err)
}
c := pb.NewHelloWorldClient(conn)
context := context.Background()
body := &pb.HelloWorldRequest{
Referer : "Grpc",
}
r, err := c.SayHelloWorld(context, body)
if err != nil {
log.Println(err)
}
log.Println(r.Message)
}
由於客戶端只是展示測試用,就簡單的來了,原本它理應歸類到cobra的管控下,設定管理等等都應可控化
在看這篇文章的你,可以試試將測試客戶端歸類好
啟動服務端
回到grpc-hello-world/目錄下,啟動服務端go run main.go server,成功則僅返回
2018/02/26 17:19:36 gRPC and https listen on: 50052
執行測試客戶端
回到client目錄下,啟動客戶端go run client.go,成功則返回
2018/02/26 17:22:57 Grpc
執行測試Restful Api
curl -X POST -k https://localhost:50052/hello_world -d '{"referer": "restful_api"}'
成功則返回{"message":"restful_api"}
最終目錄結構
grpc-hello-world
├── certs
│ ├── server.key
│ └── server.pem
├── client
│ └── client.go
├── cmd
│ ├── root.go
│ └── server.go
├── main.go
├── pkg
│ └── util
│ ├── grpc.go
│ └── tls.go
├── proto
│ ├── google
│ │ └── api
│ │ ├── annotations.pb.go
│ │ ├── annotations.proto
│ │ ├── http.pb.go
│ │ └── http.proto
│ ├── hello.pb.go
│ ├── hello.pb.gw.go
│ └── hello.proto
└── server
├── hello.go
└── server.go
至此本節就結束了,推薦一下jergoo的文章,大家有時間可以看看
另外本節涉及了許多元件間的知識,值得大家細細的回味,非常有意義!