rpc是什么?
所谓rpc(remote procedure call 远程过程调用)框架实际是提供了一套机制,使得应用程序之间可以进行通信,而且也遵从server/client模型。使用的时候客户端调用server端提供的接口就像是调用本地的函数一样。
grpc是什么?
与许多rpc系统一样,grpc基于定义服务的思想,指定可以使用其参数和返回类型远程调用的方法。默认情况下,grpc使用协议缓冲区作为接口定义语言(idl)来描述服务接口和有效负载消息的结构。
grpc有什么好处以及在什么场景下需要用grpc
既然是server/client模型,那么我们直接用restful api不是也可以满足吗,为什么还需要rpc呢?下面我们就来看看rpc到底有哪些优势
grpc vs. restful api
grpc和restful api都提供了一套通信机制,用于server/client模型通信,而且它们都使用http作为底层的传输协议(严格地说, grpc使用的http2.0,而restful api则不一定)。不过grpc还是有些特有的优势,如下:
使用场景
但是,通常我们不会去单独使用grpc,而是将grpc作为一个部件进行使用,这是因为在生产环境,我们面对大并发的情况下,需要使用分布式系统来去处理,而grpc并没有提供分布式系统相关的一些必要组件。而且,真正的线上服务还需要提供包括负载均衡,限流熔断,监控报警,服务注册和发现等等必要的组件。不过,这就不属于本篇文章讨论的主题了,我们还是先继续看下如何使用grpc。
grpc的使用通常包括如下几个步骤
protobuf的安装
mac:brew install protobuf
windows:protoc 下载:,然后将 bin 路径添加到 path 环境变量下去
linux:
安装需要的依赖包:
[root@localhost ~]# yum -y install autoconf automake libtool curl make g++ unzip [root@localhost ~]# unzip protobuf-master.zip [root@localhost ~]# cd protobuf-master
生成configure文件的脚本文件,如果不执行这步,以下操作将通不过
[root@localhost protobuf-master]# ./autogen.sh [root@localhost protobuf-master]# ./configure
可以修改安装目录通过 ./configure --prefix=命令,统一安装在/usr/local/protobuf下
[root@localhost protobuf-master]# ./configure --prefix=/usr/local/protobuf [root@localhost protobuf-master]# make [root@localhost protobuf-master]# make check [root@localhost protobuf-master]# make install [root@localhost protobuf-master]# ldconfig # refresh shared library cache.
安装成功
[root@localhost protobuf-master]# protoc -i=./ --cpp_out=./ test.proto
安装grpc包
go get -u github.com/golang/protobuf/{proto,protoc-gen-go} go get -u google.golang.org/grpc protoc --go_out=plugins=grpc:. *.proto
定义接口和数据类型
syntax = "proto3"; package rpc_package; // define a service service helloworldservice { // define the interface and data type rpc sayhello (hellorequest) returns (helloreply) {} } // define the data type of request message hellorequest { string name = 1; } // define the data type of response message helloreply { string message = 1; }
使用protobuf生成工具生成对应语言的库函数
protoc --go_out=plugins=grpc:. helloworld.proto
server.go
// server.go import ( "log" "net" "golang.org/x/net/context" "google.golang.org/grpc" pb "helloworld/helloworld" ) const ( port = ":50051" ) type server struct {} func (s *server) sayhello(ctx context.context, in *pb.hellorequest) (*pb.helloreply, error) { return &pb.helloreply{message: "hello " + in.name}, nil } func main() { lis, err := net.listen("tcp", port) if err != nil { log.fatal("failed to listen: %v", err) } s := grpc.newserver() pb.registergreeterserver(s, &server{}) s.serve(lis) }
client.go
package main //client.go import ( "log" "os" "golang.org/x/net/context" "google.golang.org/grpc" pb "helloworld/helloworld" ) const ( address = "localhost:50051" defaultname = "world" ) func main() { conn, err := grpc.dial(address, grpc.withinsecure()) if err != nil { log.fatal("did not connect: %v", err) } defer conn.close() c := pb.newgreeterclient(conn) name := defaultname if len(os.args) >1 { name = os.args[1] } r, err := c.sayhello(context.background(), &pb.hellorequest{name: name}) if err != nil { log.fatal("could not greet: %v", err) } log.printf("greeting: %s", r.message) }
以上就是golang 微服务之grpc与protobuf的使用的详细内容,更多关于golang grpc与protobuf的资料请关注移动技术网其它相关文章!
如对本文有疑问, 点击进行留言回复!!
VSCode1.4 搭建Golang的开发调试环境(遇到很多问题)
网友评论