当前位置:网站首页>golang实现grpc的tcp示例
golang实现grpc的tcp示例
2022-04-21 19:17:00 【panguixiang】
Golang实现GRPC的tcp示例(windows环境为例)
背书:rpc是一个高效的序列化工具类似json,xml,grpc是google开发的跨平台和语言的rpc框架
1.安装grpc包:
go get github.com/golang/protobuf/proto
go get google.golang.org/grpc
go get google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
上面安装好后,会在GOPATH/bin下生成protoc-gen-go.exe
注意:GOPATH 不明白是啥玩意的请自行百度
2.下载protoc.exe到GOPATH的bin目录:
windows平台编译受限,很难自己手动编译,直接去网站下载一个,
地址:https://github.com/protocolbuffers/protobuf/releases/tag/v3.9.0
,同样放在GOPATH/bin下
3.编写proto描述文件:
项目包路径:local.com/blog 一个博客项目为例,
这里随便写一个:hello.proto,放在local.com/blog/proto文件目录下,提供一个SayHello服务:

4. 编译生成.pb.go文件
使用命令生成编译后的go文件(进入proto文件目录),执行如下两个命令:
protoc --go_out=. hello.proto
protoc --go-grpc_out=. hello.proto
不出问题的话会生成如下两个go文件:

第一个包含了服务的定义,第二个包含请求和返回参数的定义。这两个文件客户端和服务端都需要直接拷贝给客户端服务和服务端服务即可正常使用。
5.实现grpc的服务端(这里以tcp协议为例):


6.grpc客户端(比较简单):

Grpc的认证,拦截器,trace调用追踪,http服务等这里不做介绍,可以参考:
https://www.topgoer.com/%E5%BE%AE%E6%9C%8D%E5%8A%A1/gRPC/OpenSSL%E5%AE%89%E8%A3%85.html
版权声明
本文为[panguixiang]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_3131854/5239137
边栏推荐
- MusicPlayer2.1版本
- MySQL de duplication query
- Seata (I) service configuration and startup
- AAAI 2022 更细粒度的不实信息检测
- APM 行业认知系列 - 四
- EasyGBS关闭了录像计划,为何还有录像文件生成?
- 5.1 overview of triggers in fundamentals of digital electronic technology
- 山东大学项目实训(四)多个点标记添加点击事件
- 【手部姿态估计】【论文解读】3D Human Pose Estimation in RGBD Images for Robotic Task Learning
- [network protocol] IP addr
猜你喜欢

Looking at the changes of robot education in the 21st century

MySQL (III) index optimization and case analysis

Instruction of crystal Chem active GIP ELISA Kit
![[advanced C language] ⑥ detailed explanation of function pointer](/img/30/729fe5d804ac1a423397ada599d767.png)
[advanced C language] ⑥ detailed explanation of function pointer

Comparison of F (ab ') 2 IgG isotype in abbexa goat

Automatic control principle Chapter 5 - frequency method (mind map)

On the significance of sustainable development of maker Education

MySQL cannot use MySQL - U root - P to start error reporting and solve it

HMS Core 6.4.0版本发布公告

全职加入清华,丘成桐:为祖国、为全球数学界培养数学人才
随机推荐
分布式事务基础
The courage to face Ali
自动控制原理第6章——控制系统的校正及综合(思维导图)
【Leetcode】每日一题:山羊拉丁文
To deal with doget & dopost Chinese garbled code
Low density lipoprotein research - LDL-C content detection kit scheme
Wide application of medical robot in AI field
Edgeboard records
Openkg open source series | Encyclopedia of characters knowledge map (Southeast University)
Apply El tooltip (bubble text prompt box) in El tabs
669. Pruning binary search tree
哪个期货交易平台最安全呢?我该怎么选择?
Rk3399 - add USB to serial driver
到底什么是外包?
Looking at the changes of robot education in the 21st century
Apache Doris creates dynamic partitions
APM 行业认知系列 - 四
In the El input search in ement, the matching input suggestions after input must have the value attribute
SIGIR'22「阿里」MetaCVR:元学习缓解小规模推荐中数据分布波动问题
第五章 使用 matplotlib 绘制饼图