Post

gRPC 和 ui 操作

gRPC

安装 protoc 工具

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

  • -I 参数:指定 import 路径,可以指定多个-I 参数,编译时按顺序查找,不指定时默认查找当前目录
  • –go_out :golang 编译支持,支持以下参数 - plugins=plugin1+plugin2 - 指定插件,目前只支持 grpc,即:plugins=grpc - M 参数 - 指定导入的.proto 文件路径编译后对应的 golang 包名(不指定本参数默认就是.proto 文件中 import 语句的路径) - import_prefix=xxx - 为所有 import 路径添加前缀,主要用于编译子目录内的多个 proto 文件,这个参数按理说很有用,尤其适用替代一些情况时的 M 参数,但是实际使用时有个蛋疼的问题导致并不能达到我们预想的效果,自己尝试看看吧 - import_path=foo/bar - 用于指定未声明 package 或 go_package 的文件的包名,最右面的斜线前的字符会被忽略 - 末尾 :编译文件路径 .proto 文件路径(支持通配符)

    完整实例

使用前安装:go get -u github.com/golang/protobuf/protoc-gen-go

1
2
3
4
5
6
7
protoc -I . --go_out=plugins=grpc,Mfoo/bar.proto=bar,import_prefix=foo/,import_path=foo/bar:. ./*.proto
protoc -I . --go_out=plugins=grpc:. dict.protoc
protoc -I . --php_out=plugins=grpc:. zhly_pay.protoc
protoc --java_out=:. zhly_pay.protoc
protoc --js_out=:. zhly_pay.protoc
protoc -I=$DIR  yun_mao.protoc  \ --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$OUT_DIR
protoc --js_out=import_style=commonjs,binary:. yun_mao.protoc

下载工具依赖

go get google.golang.org/grpc go get -u github.com/golang/protobuf/protoc-gen-go go get -u github.com/golang/protobuf/

创建.protoc 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
syntax = "proto3";  //语法声明

package gRPCDemo; //包名

// Greeter 微服务
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// HelloRequest 请求数据格式
message HelloRequest {
  string name = 1;
}

// HelloReply 响应数据格式
message HelloReply {
  string message = 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
syntax = "proto3";

package protoc;

service Greeter {
  // 1、 查找最空闲的 tcpcluster
 rpc LookupCluster(LookupClusterRequest) returns (LookupClusterResponse){}

  // 2、根据 clientid 发送数据。
    rpc SendOutByClientId(SendOutByClientIdRequest) returns (SendOutByClientIdResponse){}

  // 3、查找每个 tcpcluster 终端数
  rpc LookUpAllClusterNumber(LookUpAllClusterNumberRequest) returns(LookUpAllClusterNumberResponse){}

}


 // 1、 查找最空闲的 tcpcluster Request
message LookupClusterRequest{
    string args = 1;
}

 // 1、 查找最空闲的 tcpcluster Response
message LookupClusterResponse{
   string clientId = 1;
}

// 2、根据 clientid 发送数据  Request
message SendOutByClientIdRequest{
    string clientId = 1;
    string  mgs = 2;
}

// 2、根据 clientid 发送数据  Response
message SendOutByClientIdResponse{
    int64 code = 1;
}

// 3、查找每个 tcpcluster 终端数  Request
message LookUpAllClusterNumberRequest{

}

// 3、查找每个 tcpcluster 终端数  Response
message LookUpAllClusterNumberResponse{
        map<string, int64> number = 1;
}



grpcui

1
2
3
4
5
6
7
8
9
10
go get github.com/fullstorydev/grpcui
go install github.com/fullstorydev/grpcui/cmd/grpcui

验证下
grpcui -help
成功则会在 gopath/bin 目录下生成 grpcui

grpcui -bind 0.0.0.0 -port 8506 -plaintext 47.244.225.92:8502
            webiui地址          端口号             grpc地址
grpcui -bind 0.0.0.0 -port 8506 -plaintext 0.0.0.0:8502
GRPC 基础

在 Go 中使用 gRPC 进行远程过程调用(RPC)需要以下步骤:

  1. 定义 protobuf 文件:首先,我们需要定义所使用的协议缓冲区(protobuf)文件。该文件描述了服务接口和消息类型。

  2. 生成代码:接下来,我们需要使用 protobuf 编译器将 protobuf 文件编译成相应的 Go 文件。可以使用 protoc 命令来完成此操作,也可以使用插件来自动生成代码。

  3. 实现服务器:我们需要实现 gRPC 服务器,这涉及到实现 protobuf 接口中定义的所有方法,以及相应的处理程序。

  4. 实现客户端:我们需要实现 gRPC 客户端,这涉及到使用 gRPC 客户端 API 来调用服务器上实现的方法。

下面是一个简单的示例:

  1. 定义 protobuf 文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
syntax = "proto3";

package helloworld;

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

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}
  1. 生成代码:
1
$ protoc --go_out=plugins=grpc:. helloworld.proto

这将生成名为 helloworld.pb.go 的文件。

  1. 实现服务器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import (
  "context"
  "log"
  "net"

  "google.golang.org/grpc"
  pb "path/to/helloworld"
)

const (
  port = ":50051"
)

type server struct{}

func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
  log.Printf("Received: %v", in.GetName())
  return &pb.HelloReply{Message: "Hello " + in.GetName()}, 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{})
  if err := s.Serve(lis); err != nil {
    log.Fatalf("failed to serve: %v", err)
  }
}
  1. 实现客户端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

import (
  "context"
  "log"

  "google.golang.org/grpc"
  pb "path/to/helloworld"
)

const (
  address = "localhost:50051"
)

func main() {
  conn, err := grpc.Dial(address, grpc.WithInsecure())
  if err != nil {
    log.Fatalf("did not connect: %v", err)
  }
  defer conn.Close()
  c := pb.NewGreeterClient(conn)
  name := "world"
  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.GetMessage())
}
This post is licensed under CC BY 4.0 by the author.