31

gRPC远程过程调用之golang

 5 years ago
source link: https://studygolang.com/articles/15482?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

gRPC是Google出品,支持多种语言,但是国内安装会有点问题,下面整理一下,方便今后配环境的复习。

安装grpc

go get google.golang.org/grpc

结果出现了如下错误:

package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc"(https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

参考 https://blog.csdn.net/cjj198561/article/details/78133193 可以使用如下方式进行安装。

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
 
cd $GOPATH/src/
go install google.golang.org/grpc

安装protocal buffer compiler

https://github.com/google/protobuf/releases

下载后使用源码安装,固定套路为:

tar -zxvf xxxx.tar.gz
cd xxxx/
./configure
make
make install

漫长等待后,就完事了。不过这次还没完,好像是需要将一个环境变量加到path中。

export LD_LIBRARY_PATH=/usr/local/lib

创建proto文件

helloworld.proto 放到 $GOPATH/src/helloworld 目录下,方便自己待会的引用。

syntax = "proto3";

package grpcusage;

service Hello {
    rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
    string Name = 1;
}

message HelloReply {
    string Message = 1;
}

然后通过protoc 根据此模板来生成对应的golang的grpc代码

#格式 protoc --go_out=plugins=grpc:{go代码输出路径} {proto文件}
protoc --go_out=plugins=grpc:./ ./helloworld.proto

gRPC代码编写

server.go

package main

import (
    "golang.org/x/net/context"
    pb "helloworld"

    "net"
    "log"
    "google.golang.org/grpc"
    "fmt"
)

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() {
    conn, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("grpc server listening at: 50051 port")
    server := grpc.NewServer()
    pb.RegisterHelloServer(server, &Server{})
    server.Serve(conn)
}

client.go

package main

import (
    "google.golang.org/grpc"
    "log"
    pb "helloworld"
    "os"
    "context"
        "fmt"
)

const (
    address = "localhost:50051"
    defaultName = "郭璞"
)

func main()  {
    conn, err := grpc.Dial(address, grpc.WithInsecure())
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()
    client := pb.NewHelloClient(conn)

    name := defaultName
    if len(os.Args) > 1 {
        name = os.Args[1]
    }
    request, err := client.SayHello(context.Background(), &pb.HelloRequest{Name:name})
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(request.Message)
}

测试服务

启动server端程序。

➜  grpcusage go run server.go
grpc server listening at: 50051 port

启动client程序

➜  client go run client.go
hello 郭璞
➜  client

如此便实现了grpc在golang中的简单使用,暂且记录下,以备不时之需。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK