<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## OverView gRPC是由Google主導開發的RPC框架,使用HTTP/2協議并用ProtoBuf作為序列化工具。其客戶端提供Objective-C、Java接口,服務器側則有Java、Golang、C++等接口,從而為移動端(iOS/Androi)到服務器端通訊提供了一種解決方案。 當然在當下的環境下,這種解決方案更熱門的方式是RESTFull API接口。該方式需要自己去選擇編碼方式、服務器架構、自己搭建框架(JSON-RPC)。gRPC官方對REST的聲音是: * 和REST一樣遵循HTTP協議(明確的說是HTTP/2),但是gRPC提供了全雙工流 * 和傳統的REST不同的是gRPC使用了靜態路徑,從而提高性能 * 用一些格式化的錯誤碼代替了HTTP的狀態碼更好的標示錯誤 ## Installtion protobuf 安裝,exec: ``` go get -u github.com/golang/protobuf/proto // golang protobuf 庫 go get -u github.com/golang/protobuf/protoc-gen-go //protoc --go_out 工具 ``` gRPC-go可以通過golang 的get命令直接安裝,非常方便。 ``` go get google.golang.org/grpc ``` ## Example ### 編寫.proto文件,生成pb.go文件 hello.proto ``` syntax = "proto3"; option objc_class_prefix = "HLW"; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; } ``` 這里定義了一個服務Greeter,其中有個API `SayHello`。其接受參數為`HelloRequest`類型,返回`HelloReply`類型。這里`HelloRequest`和`HelloReply`就是普通的PB定義 服務端的定義 ``` // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} } ``` `service`定義了一個server。其中的接口可以是四種類型 * rpc GetFeature(Point) returns (Feature) {} 類似普通的函數調用,客戶端發送請求Point到服務器,服務器返回相應Feature. * rpc ListFeatures(Rectangle) returns (stream Feature) {} 客戶端發起一次請求,服務器端返回一個流式數據,比如一個數組中的逐個元素 * rpc RecordRoute(stream Point) returns (RouteSummary) {} 客戶端發起的請求是一個流式的數據,比如數組中的逐個元素,服務器返回一個相應 * rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} 客戶端發起的請求是一個流式數據,比如數組中的逐個元素,二服務器返回的也是一個類似的數據結構 使用protoc命令生成相關文件: ``` protoc --go_out=plugins=grpc:. helloworld.proto ls helloworld.pb.go helloworld.proto ``` ### 服務器端程序 server.go ``` package main import ( "log" "net" pb "Grpc/helloworld" "golang.org/x/net/context" "google.golang.org/grpc" ) const ( port = ":50051" ) // server is used to implement helloworld.GreeterServer. type server struct{} // SayHello implements helloworld.GreeterServer func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { return &pb.HelloReply{Message: "Hello " + in.Name}, nil } func main() { lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) s.Serve(lis) } ``` client.go ``` package main import ( "log" "os" "strconv" pb "Grpc/helloworld" "golang.org/x/net/context" "google.golang.org/grpc" ) const ( address = "192.168.0.106:50051" defaultName = "world" ) func main() { // Set up a connection to the server. conn, err := grpc.Dial(address, grpc.WithInsecure()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn) // Contact the server and print out its response. name := defaultName if len(os.Args) > 1 { name = os.Args[1] } r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.Message) go func() { for i:= 0; i<= 1000000; i ++{ ni := name + ":" + strconv.Itoa(i) r, err = c.SayHello(context.Background(), &pb.HelloRequest{Name: ni}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.Message) } }() var cd chan int <- cd } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看