Golang gRPC 安装及使用实例(Window、Ubuntu)

一、Windows下安装

需要完成golang的安装。

1、下载最新版的安装程序

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

到github中,选择适合自己的版本进行下载。
下载后是一个压缩包,随便找个地方解压,把 bin 目录中的 protoc.exe 放到 golang GOPATH 的 bin 目录中。

Golang的GOPATH确认方法:

  1. 命令行执行: go env
  2. 找到 “GOPATH”
2、添加环境变量

在安装goalng时应该都加入过环境变量了

2.1、确认Golang的 bin 目录地址:C:\Users\qhs\go\bin
2.2、将该地址放入环境变量的 “path” 中即可。
2.3、重启命令行终端,输入protoc --version 能够显示版本号表示配置正确。
完成安装

二、Ubuntu下安装

1、提前安装golang并设置为国内代理

go env -w GOPROXY=https://goproxy.cn,direct

2、更新apt

sudo apt-get update

3、安装 protoc(Protocol Buffers 编译器)和 protobuf(Golang 的 Protocol Buffers 插件)

sudo apt-get install -y protobuf-compiler golang-goprotobuf-dev

4、构建项目并初始化

在项目中正确引入了 protobuf 的库文件并安装了依赖:
go get -u github.com/golang/protobuf/protoc-gen-go
输入protoc --version 能够显示版本号表示配置正确。

完成安装

三、生成代码

1、创建项目

随便找个目录,创建新的项目: test_grpc

2、初始化mod:

go mod init test_grpc

3、新建proto文件

在项目中新建目录mypro,新建proto文件hello.proto 内容如下:

syntax = "proto3"; // 指定proto版本
package hello_grpc;     // 指定默认包名

// 指定golang包名
option go_package = "/hello_grpc";

//定义rpc服务
service HelloService {
  // 定义函数
  rpc SayHello (HelloRequest) returns (HelloResponse) {}
}

// HelloRequest 请求内容
message HelloRequest {
  string name = 1;
  string message = 2;
}

// HelloResponse 响应内容
message HelloResponse{
  string name = 1;
  string message = 2;
}
4、生成go代码
  • 进入目录,与 hello.proto 处于同一个目录中。
    cd mypro
  • 生成普通的 Protocol Buffers 消息代码
    protoc --go_out=. hello.proto
  • 生成 gRPC 服务代码
    protoc --go-grpc_out=. hello.proto
  • 合并两个命令
    protoc --go_out=. --go-grpc_out=. hello.proto
  • mypro目录下,会自动生成hello_grpc目录,目录中包含hello.pb.go文件。

四、服务端与客户端代码

1、创建服务端文件
package main

import (
    "context"
    "fmt"
    "google.golang.org/grpc"
    "myrpc/mypro/hello_grpc"
    "net"
)

type HelloService struct {
}

func (h *HelloService) SayHello(ctx context.Context, req *hello_grpc.HelloRequest) (res *hello_grpc.HelloResponse, err error) {
    fmt.Println("Hello: ", req)
    return &hello_grpc.HelloResponse{
        Name:    "qhs",
        Message: "say hello",
    }, nil
}

func main() {
    //监听端口
    listen, err := net.Listen("tcp", ":8080")
    if err != nil {
        return
    }
    // 创建一个新的grpc服务端
    s := grpc.NewServer()
    server := HelloService{}
    // 注册为grpc服务
    hello_grpc.RegisterHelloServiceServer(s, &server)
    fmt.Println("grpc server running :8080")
    // 处理客户端的请求
    err = s.Serve(listen)
}
2、创建客户端文件
package main

import (
    "context"
    "fmt"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    "myrpc/mypro/hello_grpc"
)

func main() {
    addr := ":8080"
    // 使用 grpc.Dial 创建一个到指定地址的 gRPC 连接。
    conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        return
    }
    defer conn.Close()

    //初始化客户端
    client := hello_grpc.NewHelloServiceClient(conn)
    res, err := client.SayHello(context.Background(), &hello_grpc.HelloRequest{
        Name:    "client qhs ",
        Message: "client qhs msg",
    })
    if err != nil {
        return
    }

    fmt.Println(res)
}
3、目录结构

最终目录结构如下:

|- test_grpc
|-- client
|---- hello.go
|-- mypro
|---- hello_grpc
|------  hello.pd.go
|---- hello.proto
|-- service
|---- hello.go
|-- go.mod
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容