go语言实现gRPC远程调用

1 protobuf 协议定义

创建一个protobuf package, 如:my_rpc_proto;

在$GOPATH/src/下创建 go_lession/gRPC_test/my_rpc_proto/ 文件夹

里面创建protobuf协议文件 helloServer.proto

syntax = "proto3";

package my_rpc_proto;

// The HelloServer service definition.
service HelloServer {
      // 第一个远程调用接口
      rpc SayHello (HelloRequest) returns (HelloReply) {}
      // 第二个远程调用接口
      rpc GetHelloMsg (HelloRequest) returns (HelloMessage) {}
}

// The request message containing the user's name.
message HelloRequest {
      string name = 1;
}

// The response message containing the greetings
message HelloReply {
      string message = 1;
}

message HelloMessage {
    string msg = 1;
}

在当前文件下,编译helloServer.proto 文件

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

得到helloServer.pb.go文件

2 gRPC-Server编写

package main

import (
    "fmt"
    "net"

    pb "go_lession/gRPC_test/my_rpc_proto"
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    "google.golang.org/grpc/reflection"
)

const (
    port = ":18881"
)

type server struct{}

//实现RPC SayHello 接口
func (this *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    return &pb.HelloReply{Message: "hello" + in.Name}, nil
}

//实现RPC GetHelloMsg 接口
func (this *server) GetHelloMsg(ctx context.Context, in *pb.HelloRequest) (*pb.HelloMessage, error) {
    return &pb.HelloMessage{Msg: "this is from server HAHA!"}, nil
}

func main() {

    listen, err := net.Listen("tcp", port)
    if err != nil {
        fmt.Println("failed to listen : ", err)
        return
    }

    //得到一个gRPC 服务句柄
    srv := grpc.NewServer()

    //将 gRPC服务句柄 和 我们的server结构体绑定
    pb.RegisterHelloServerServer(srv, &server{})

    //注册服务
    reflection.Register(srv)

    //启动监听gRPC服务
    if err := srv.Serve(listen); err != nil {
        fmt.Println("failed to serve, ", err)
        return
    }

}

3 gRPC-Client编写

package main

import (
    "fmt"

    pb "go_lession/gRPC_test/my_rpc_proto"
    "golang.org/x/net/context"
    "google.golang.org/grpc"
)

const (
    address    = "localhost:18881"
    clientName = "GreenHat"
)

type server struct{}

func main() {

    //得到 gRPC 链接客户端句柄
    conn, err := grpc.Dial(address, grpc.WithInsecure())
    if err != nil {
        fmt.Println("did not connetc : ", err)
        return
    }
    defer conn.Close()

    //将 proto 里面的服务句柄 和 gRPC句柄绑定
    c := pb.NewHelloServerClient(conn)

    //远程调用 SayHello接口
    r1, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: clientName})
    if err != nil {
        fmt.Println("cloud not get Hello server ..", err)
        return
    }

    fmt.Println("HelloServer resp: ", r1.Message)

    //远程调用 GetHelloMsg接口
    r2, err := c.GetHelloMsg(context.Background(), &pb.HelloRequest{Name: clientName})
    if err != nil {
        fmt.Println("cloud not get hello msg ..", err)
        return
    }

    fmt.Println("HelloServer resp: ", r2.Msg)
}

运行server,在运行client

得到以下输出结果.

HelloServer resp:  helloGreenHat
HelloServer resp:  this is from server HAHA!

results matching ""

    No results matching ""