zoukankan      html  css  js  c++  java
  • grpc安装

    转载 https://www.cnblogs.com/YaoDD/p/5504881.html

    1、安装gRPC runtime

    go get google.golang.org/grpc

    为了自动生成Golang的gRPC代码,需要安装protocal buffers compiler以及对应的GoLang插件

    2、protocal buffer安装

    从https://github.com/google/protobuf/releases下载安装包,例如:protobuf-cpp-3.0.0-beta-3.zip,解压后进入目录执行

    再添加环境变量:export LD_LIBRARY_PATH=/usr/local/lib,之后protoc命令即可运行

    ./configure
    make && make install
    

      

    3、安装GoLang protoc 插件

    go get -a github.com/golang/protobuf/protoc-gen-go
    

     进入github.com/golang/protobuf/protoc-gen-go目录,执行

    go build
    

      完成之后,拷贝protoc-gen-go到$GOPATH/bin目录下

    4、定义service

    一个RPC service就是一个能够通过参数和返回值进行远程调用的method,我们可以简单地将它理解成一个函数。因为gRPC是通过将数据编码成protocal buffer来实现传输的。因此,我们通过protocal buffers interface definitioin language(IDL)来定义service method,同时将参数和返回值也定义成protocal buffer message类型。具体实现如下所示,包含下面代码的文件叫helloworld.proto:

    syntax = "proto3";
     
    option java_package = "io.grpc.examples";
     
    package helloworld;
     
    // The greeter 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;
    }

    5、接着,根据上述定义的service,我们可以利用protocal buffer compiler ,即protoc生成相应的服务器端和客户端的GoLang代码。生成的代码中包含了客户端能够进行RPC的方法以及服务器端需要进行实现的接口。

    假设现在所在的目录是$GOPATH/src/helloworld/helloworld,我们将通过如下命令生成gRPC对应的GoLang代码:

    protoc --go_out=plugins=grpc:. helloworld.proto

    此时,将在目录下生成helloworld.pb.go文件。

    6、接着,在目录$GOPATH/src/helloworld/下创建server.go 和client.go,分别用于服务器和客户端的实现。

    package main
     
    // server.go
     
    import (
        "log"
        "net"
     
        "golang.org/x/net/context"
        "google.golang.org/grpc"
        pb "helloworld/helloworld"
    )
     
    const (
        port = ":50051"
    )
     
    type server struct {}
     
    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.Fatal("failed to listen: %v", err)
        }
        s := grpc.NewServer()
        pb.RegisterGreeterServer(s, &server{})
        s.Serve(lis)
    }

      

    package main
     
    //client.go
     
    import (
        "log"
        "os"
     
        "golang.org/x/net/context"
        "google.golang.org/grpc"
        pb "helloworld/helloworld"
    )
     
    const (
        address     = "localhost:50051"
        defaultName = "world"
    )
     
    func main() {
        conn, err := grpc.Dial(address, grpc.WithInsecure())
        if err != nil {
            log.Fatal("did not connect: %v", err)
        }
        defer conn.Close()
        c := pb.NewGreeterClient(conn)
     
        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.Fatal("could not greet: %v", err)
        }
        log.Printf("Greeting: %s", r.Message)
    }

    这里需要注意的是包pb是我们之前生成的helloworld.pb.go所在的包,并非必须如上述代码所示在$GOPATH/src/helloworld/helloworld目录下。

    7、最后运行如下代码进行演示即

    go run server.go
    go run client.go
  • 相关阅读:
    flutter 中 List 和 Map 的用法
    AndroidStudio中Flutter打包APK
    flutter中的异步机制 Future
    [控件] 画饼状图的控件
    用CAShapeLayer写股市K线图动画效果
    使用Instruments中的CoreAnimation分析动画
    山寨Facebook的Shimmer效果
    maskView遮罩中多张图片的动画
    将CAGradientLayer用作maskView的遮罩图层
    [翻译] JKLLockScreenViewController
  • 原文地址:https://www.cnblogs.com/zengyjun/p/10064552.html
Copyright © 2011-2022 走看看