GRPC-go版本

1.安装GO,protobuf

只适合有梯子的

GO的安装没必要说了

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

选合适的版本,将解压后bin目录的protoc.exe放到GO的安装目录的bin下(省事)

打开CMD,输入protoc --version,正常如下图

环境变量有三个需要设置

  1. GOPATH:是GO的工程目录

  2. GOBIN:生成的exe目录

  3. PATH:电脑环境变量

2.安装相关包

2021-0721

好像要在grpcTest目录下,go mod init 之后进行安装,也有可能是我电脑问题,

1.golang 的proto工具包

go get -u github.com/golang/protobuf/proto

2.golang 的proto编译支持

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

3.安装grpc包

go get -u google.golang.org/grpc

4.安装grpc-gen插件

go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc

安装正常的话,在GOPATH的bin目录下会有两个exe

3.检验

去下载示例代码,放到GOPATH下,找到里面的helloworld

 git clone -b v1.35.0 https://github.com/grpc/grpc-go

在GOPATH下新建grpcTest,项目结构如下

helloworld.proto(官方)

// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License. syntax = "proto3"; option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
-->换成option go_package="." 表示生成的proto.go在当前目录
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto"; package helloworld; // The greeting 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;
}

生成.go文件

client.go(官方) server和client一样,我这就只列举client

/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/ // Package main implements a client for Greeter service.
package main import (
"context"
"log"
"os"
"time" "google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
--》改成 pb "grpcTest/proto"
) const (
address = "localhost:50051"
defaultName = "world"
) func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn) // Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}

然后去运行sever和client

ok了

还是建议大家多去看官方的介绍,不要怕英文,有点底子的都能看懂

Quick start | Go | gRPC

出现protoc-gen-go' is not recognized as***********就是你没配置好环境变量,把GOPATH下的bin加到PATH里去

参考链接

最新文章

  1. cron(CronTrigger)表达式用法
  2. WeCenter二次开发教程(一):熟悉模板结构
  3. Hello mybatis
  4. HOLOLENS的空间管理
  5. Python:IOError: image file is truncated 的解决办法
  6. centos python 2.7 安装
  7. 什么是cname a记录
  8. 20141128--JavaScript HTML DOM
  9. JS判断客户端是手机还是PC
  10. 如何使用NET Reactor为您的.Net(C#,VB.Net) 源代码加密
  11. wp-content-index文章目录插件使用效果调整
  12. Json对象直接存取数据库
  13. 新版703n刷openwrt
  14. awk-模式匹配
  15. tomcat安装完设定用户名和密码
  16. linux上redis安装配置及其防漏洞配置及其攻击方法
  17. 建立LINUX服务器
  18. 【mongodb系统学习之十】mongodb查询(三)
  19. python 字典dict类型合并(不能错过哦)
  20. 文件上传的一个坑 Apache上传组件和SpringMVC自带上传冲突

热门文章

  1. ASP.NET Core框架探索之Authorization
  2. NET经典书籍必读
  3. Spring系列26:Spring AOP 通知与顺序详解
  4. 问鼎杯预赛web writeup
  5. 【模板】动态 DP
  6. redis整理:常用命令,雪崩击穿穿透原因及方案,分布式锁实现思路,分布式锁redission(更新中)
  7. maven常用命令含义
  8. Mybatis框架基础入门(七)--关联查询
  9. Python使用pip安装No matching distribution found for PyYaml==5.3.1
  10. 分布式集群中为什么会有 Master?