Skip to content

简介

https://grpc.io/

https://github.com/grpc/grpc-go

1
2
3
4
5
6
7
# linux
$ apt install -y protobuf-compiler
# mac
$ brew install protobuf

$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1

Update your PATH so that the protoc compiler can find the plugins:

1
export PATH="$PATH:$(go env GOPATH)/bin"

随着微服务和云原生相关技术的发展,应用程序的架构模式已从传统的单体架构或分层架构转向了分布式的计算架构。
尽管分布式架构本身有一定的开发成本和运维成本,但它所带来的收益是显而易见的。

在实现分布式应用程序时,我们才必须考虑两个因素: 网络协议和传输载荷的编码。
从最早的 RMI+Java 原生序列话,到 HTTP+JSON/XML,业界一直在尝试寻找一种兼具高效性和可读性的方案。
虽然在实践微服务的过程中,大家普遍愿意采用更具语义化的 HTTP+JSON 形式,但这种形式有其自身的局限性,比如其网络传输载荷低效、接口规范松散等。
正是在这样的背景下,gRPC 应运而生,借助优异的性能和谷歌的大力推广,gRPC 受到众多大厂青睐。
目前,gRPC 已经成为云原生计算基金会的孵化项目,并被广泛应用于众多开源项目和企业级项目中

gRPC 最大的特点是高性能,HTTP/2+protocol buffers 的组合使其在性能方面具备了天然的优势,这也是 gRPC 广受欢迎的重要原因。
但是,相对于更成熟稳定的 HTTP+JSON 组合,gRPC 的风险不容低估,比如其协议不够稳定、社区相对较小等,这些都是在做技术选型的时候需要考虑的重要因素。
正如我们所熟知的,从来就没有 "银弹",作为技术从业者,我们只能去分析和对比各种可用的技术,根据自身需求选择最合适的技术方案

安装依赖

Protocol Buffer Compiler Installation

https://grpc.io/docs/protoc-installation/

Linux, using apt or apt-get, for example:

1
2
$ apt install -y protobuf-compiler
$ protoc --version  # Ensure compiler version is 3+

MacOS, using Homebrew:

1
2
$ brew install protobuf
$ protoc --version  # Ensure compiler version is 3+

Go plugins for the protocol compiler:

1
2
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1

Update your PATH so that the protoc compiler can find the plugins:

1
$ export PATH="$PATH:$(go env GOPATH)/bin"