GRPC-go版本
2024-10-20 13:41:27
GRPC-go版本
1.安装GO,protobuf
只适合有梯子的
GO的安装没必要说了
protobuf :https://github.com/protocolbuffers/protobuf/releases
选合适的版本,将解压后bin目录的protoc.exe放到GO的安装目录的bin下(省事)
打开CMD,输入protoc --version
,正常如下图
环境变量有三个需要设置
GOPATH:是GO的工程目录
GOBIN:生成的exe目录
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了
还是建议大家多去看官方的介绍,不要怕英文,有点底子的都能看懂
出现protoc-gen-go' is not recognized as***********
就是你没配置好环境变量,把GOPATH下的bin加到PATH里去
参考链接
- Quick start | Go | gRPC
- Go gRPC教程-环境安装(一) - 烟花易冷人憔悴 - 博客园 (cnblogs.com)
- (5条消息) 完美解决:protoc-gen-go-grpc: program not found or is not executable_李纳克斯的博客-CSDN博客
最新文章
- cron(CronTrigger)表达式用法
- WeCenter二次开发教程(一):熟悉模板结构
- Hello mybatis
- HOLOLENS的空间管理
- Python:IOError: image file is truncated 的解决办法
- centos python 2.7 安装
- 什么是cname a记录
- 20141128--JavaScript HTML DOM
- JS判断客户端是手机还是PC
- 如何使用NET Reactor为您的.Net(C#,VB.Net) 源代码加密
- wp-content-index文章目录插件使用效果调整
- Json对象直接存取数据库
- 新版703n刷openwrt
- awk-模式匹配
- tomcat安装完设定用户名和密码
- linux上redis安装配置及其防漏洞配置及其攻击方法
- 建立LINUX服务器
- 【mongodb系统学习之十】mongodb查询(三)
- python 字典dict类型合并(不能错过哦)
- 文件上传的一个坑 Apache上传组件和SpringMVC自带上传冲突
热门文章
- ASP.NET Core框架探索之Authorization
- NET经典书籍必读
- Spring系列26:Spring AOP 通知与顺序详解
- 问鼎杯预赛web writeup
- 【模板】动态 DP
- redis整理:常用命令,雪崩击穿穿透原因及方案,分布式锁实现思路,分布式锁redission(更新中)
- maven常用命令含义
- Mybatis框架基础入门(七)--关联查询
- Python使用pip安装No matching distribution found for PyYaml==5.3.1
- 分布式集群中为什么会有 Master?