现代的服务端技术栈:Golang/Protobuf/gRPC详解

译注:
并发与并行:并发是虚拟的并行,比如通过时间切片技术在单核cpu上运行多个任务,让每个使用者“以为”自己在独占这一cpu资源;并行是实际的同一时间多任务同时运行,多数是指在多核cpu的场景下。
队列与双端队列:队列遵循先入先出的原则,从一端存数,从另一端取数,双端队列支持从队列的两端存数和取数。
阻塞和非阻塞:阻塞和非阻塞描述了程序等待返回结果时的状态,阻塞代表不返回结果就挂起,不进行任何操作;非阻塞是在没返回结果时可以执行其他任务。
合作和抢占:高优先级任务可以打断其他正在运行的低优先级任务,则调度器是抢占式的;反之,则是合作式的。
服务端编程的阵营中有很多新面孔,一水儿的谷歌血统。在谷歌开始将golang应用于其产品系统后,golang快速的吸引了大量的关注。随着微服务架构的兴起,人们开始关注一些现代的数据通信解决方案,如grpc和protobuf。在本文中,我们会对以上这些概念作一些简要的介绍。
一、golang
golang又称go语言,是一个开源的、多用途的编程语言,由google研发,并由于种种原因,正在日益流行。golang已经有10年的历史,并且据google称已经在生产环境中使用了接近7年的时间,这一点可能让大多数人大跌眼镜。
golang的设计理念是,简单、现代、易于理解和快速上手。golang的创造者将golang设计为一个普通的程序员可以在一个周末的时间就可以掌握,并达到使用golang进行工作的程度。这一点我已经亲身证实。golang的创造者,都是c语言原始草案的专家组成员,可以说,golang根红苗正,值得信赖。
理都懂,但话说回来,为什么我们需要另一门编程语言呢
多数场景下,确实并不需要。事实上,go语言并不能解决其他语言或工具无法解决的新问题。但在一些强调效率、优雅与直观的场景下,人们通常会面临一系列的相关问题,这正是go所致力于解决的领域。go的主要特点是:
一流的并发支持
内核十分简单,语言优雅、现代
高性能
提供现代软件开发所需要的原生工具支持
我将简要介绍go是如何提供上述的支持的。在go语言的官网可以了解更多的特性和细节。
一流的并发支持
并发是多数服务端应用所需要考虑的主要问题之一,考虑到现代微处理器的特性,并发也成为编程语言的主要关切之一。go语言引入了“goroutine”的理念。可以把“goroutine”理解为一个“轻量级的用户空间线程”(现实中,当然远比这要复杂得多,同一线程可能会附着多路的goroutine,但这样的提法可以让你有一个大致的概念)。所谓“轻量级”,可以这样理解,由于采用了十分袖珍的堆栈,你可以同时启动数以百万计的goroutine,事实上这也是go语言官方所推荐的方式。在go语言中,任何函数或方法都可以生成一个goroutine。比如,只需要运行“go myasynctask()”就可以从“myasynctask”函数生成一个goroutine。示例代码如下:
// this function performs the given task concurrently by spawing a goroutine
// for each of those tasks.
func performasynctasks(task []task) {
for _, task := range tasks {
// this will spawn a separate goroutine to carry out this task.
// this call is non-blocking
go task.execute()


goroutineexample.go hosted with ? by github
(左右滑动查看全部代码)
怎么样,是不是很简单?go是一门简单的语言,因此注定是以这样的方式来解决问题。你可以为每个独立的异步任务生成一个goroutine而不需要顾虑太多事情。如果处理器支持多核运行,go语言运行时会自动的以并行的方式运行所有的goroutine。那么,goroutine之间是如何通信的呢,答案是channel。
“channel”也是go语言的一个概念,用于进行goroutine之间的通信。通过channel,你可以向另一个goroutine传递各种信息(比如go语言概念里的type或者struct甚至是channel)。一个channel大体上是一个“双端阻塞队列”(也可以单端的)。如果需要goroutine基于特定条件触发下一步的行动,也可以利用channel来实现goroutine的协作阻塞任务模式。
在编写异步或者并发的代码时,goroutine和channel这两个概念赋予了编程者大量的灵活性和简便性。可以籍此很容易的建立其他很有用的库,比如goroutine pool,举个简单的例子:
package executor
import (
"log"
"sync/atomic"

// the executor struct is the main executor for tasks.
// 'maxworkers' represents the maximum number of simultaneous goroutines.
// 'activeworkers' tells the number of active goroutines spawned by the executor at given time.
// 'tasks' is the channel on which the executor receives the tasks.
// 'reports' is channel on which the executor publishes the every tasks reports.
// 'signals' is channel that can be used to control the executor. right now, only the termination
// signal is supported which is essentially is sending '1' on this channel by the client.
type executor struct {
maxworkers    int64
activeworkers int64
tasks   chan task
reports chan report
signals chan int

// newexecutor creates a new executor.
// 'maxworkers' tells the maximum number of simultaneous goroutines.
// 'signals' channel can be used to control the executor.
func newexecutor(maxworkers int, signals chan int) *executor {
chansize := 1000
if maxworkers > chansize {
chansize = maxworkers

executor := executor{
maxworkers: int64(maxworkers),
tasks:      make(chan task, chansize),
reports:    make(chan report, chansize),
signals:    signals,

go executor.launch()
return &executor

// launch starts the main loop for polling on the all the relevant channels and handling differents
// messages.
func (executor *executor) launch() int {
reports := make(chan report, executor.maxworkers)
for {
select {
case signal := <-executor.signals:
if executor.handlesignals(signal) == 0 {
return 0

case r := <-reports:
executor.addreport(r)
default:
if executor.activeworkers < executor.maxworkers && len(executor.tasks) > 0 {
task := <-executor.tasks
atomic.addint64(&executor.activeworkers, 1)
go executor.launchworker(task, reports)




// handlesignals is called whenever anything is received on the 'signals' channel.
// it performs the relevant task according to the received signal(request) and then responds either
// with 0 or 1 indicating whether the request was respected(0) or rejected(1).
func (executor *executor) handlesignals(signal int) int {
if signal == 1 {
log.println("received termination request...")
if executor.inactive() {
log.println("no active workers, exiting...")
executor.signals <- 0
return 0

executor.signals <- 1
log.println("some tasks are still active...")

return 1

// launchworker is called whenever a new task is received and executor can spawn more workers to spawn
// a new worker.
// each worker is launched on a new goroutine. it performs the given task and publishes the report on
// the executor's internal reports channel.
func (executor *executor) launchworker(task task, reports chan<- report) {
report := task.execute()
if len(reports) < cap(reports) {
reports <- report
} else {
log.println("executor's report channel is full...")

atomic.addint64(&executor.activeworkers, -1)

// addtask is used to submit a new task to the executor is a non-blocking way. the client can submit
// a new task using the executor's tasks channel directly but that will block if the tasks channel is
// full.
// it should be considered that this method doesn't add the given task if the tasks channel is full
// and it is up to client to try again later.
func (executor *executor) addtask(task task) bool {
if len(executor.tasks) == cap(executor.tasks) {
return false

executor.tasks <- task
return true

// addreport is used by the executor to publish the reports in a non-blocking way. it client is not
// reading the reports channel or is slower that the executor publishing the reports, the executor's
// reports channel is going to get full. in that case this method will not block and that report will
// not be added.
func (executor *executor) addreport(report report) bool {
if len(executor.reports) == cap(executor.reports) {
return false

executor.reports <- report
return true

// inactive checks if the executor is idle. this happens when there are no pending tasks, active
// workers and reports to publish.
func (executor *executor) inactive() bool {
return executor.activeworkers == 0 && len(executor.tasks) == 0 && len(executor.reports) == 0

executor.go hosted with ? by github
(左右滑动查看全部代码)
内核十分简单,语言优雅、现代
与其他多数的现代语言不同,golang本身并没有提供太多的特性。事实上,严格限制特性集的范围正是go语言的显著特征,且go语言着意于此。go语言的设计与java的编程范式不同,也不支持如python一样的多语言的编程范式。go只是一个编程的骨架结构。除了必要的特性,其他一无所有。
看过go语言之后,第一感觉是其不遵循任何特定的哲学或者设计指引,所有的特性都是以引用的方式解决某一个特定的问题,不会画蛇添足做多余的工作。比如,go语言提供方法和接口但没有类;go语言的编译器生成动态链接库,但同时保留垃圾回收器;go语言有严格的类型但不支持泛型;go语言有一个轻量级的运行时但不支持异常。
go的这一设计理念的主要用意在于,在表达想法、算法或者编码的环节,开发者可以尽量少想或者不去想“在某种编程语言中处理此事的最佳方案”,让不同的开发者可以更容易理解对方的代码。不支持泛型和异常使得go语言并不那么完美,也因此在很多场景下束手束脚 ,因此在“go 2”版本中,官方加入了对这些必要特性的考虑。
高性能
单线程的执行效率并不足以评估一门语言的优劣,当语言本身聚焦于解决并发和并行问题的时候尤其如此。即便如此,golang还是跑出了亮眼的成绩,仅次于一些硬核的系统编程语言,如c/c++/rust等等,并且golang还在不断的改进。考虑到go是有垃圾回收机制的语言,这一成绩实际上相当的令人印象深刻,这使得go语言的性能可以应付几乎所有的使用场景。
(image source: medium)
提供现代软件开发所需要的原生工具支持
是否采用一种新的语言或工具,直接取决于开发者体验的好坏。就go语言来说,其工具集是用户采纳的主要考量。同最小化的内核一样,go的工具集也采用了同样的设计理念,最小化,但足够应付需要。执行所有go语言工具,都采用 go 命令及其子命令,并且全部是以命令行的方式。
go语言中并没有类似pip或者npm这类包管理器。但只需要下面的命令,就可以得到任何的社区包:
go get github.com/farkaskid/webcrawler/blob/master/executor/executor.go
(左右滑动查看全部代码)
是的,这样就行。可以直接从github或其他地方拉取所需要的包。所有的包都是源代码文件的形态。
对于package.json这类的包,我没有看到与 goget 等价的命令。事实上也没有。在go语言中,无须在一个单一文件中指定所有的依赖,可以在源文件中直接使用下面的命令:
import "github.com/xlab/pocketsphinx-go/sphinx"
(左右滑动查看全部代码)
那么,当执行go build命令的时候,运行时会自动的运行 goget 来获取所需要的依赖。完整的源码如下:
package main
import (
"encoding/binary"
"bytes"
"log"
"os/exec"
"github.com/xlab/pocketsphinx-go/sphinx"
pulse "github.com/mesilliac/pulse-simple" // pulse-simple

var buffsize int
func readint16(buf []byte) (val int16) {
binary.read(bytes.newbuffer(buf), binary.littleendian, &val)
return

func createstream() *pulse.stream {
ss := pulse.samplespec{pulse.sample_s16le, 16000, 1}
buffsize = int(ss.usectobytes(1 * 1000000))
stream, err := pulse.capture("pulse-simple test", "capture test", &ss)
if err != nil {
log.panicln(err)

return stream

func listen(decoder *sphinx.decoder) {
stream := createstream()
defer stream.free()
defer decoder.destroy()
buf := make([]byte, buffsize)
var bits []int16
log.println("listening...")
for {
_, err := stream.read(buf)
if err != nil {
log.panicln(err)

for i := 0; i < buffsize; i += 2 {
bits = append(bits, readint16(buf[i:i+2]))

process(decoder, bits)
bits = nil


func process(dec *sphinx.decoder, bits []int16) {
if !dec.startutt() {
panic("decoder failed to start utt")

dec.processraw(bits, false, false)
dec.endutt()
hyp, score := dec.hypothesis()
if score > -2500 {
log.println("predicted:", hyp, score)
handleaction(hyp)


func executecommand(commands ...string) {
cmd := exec.command(commands[0], commands[1:]...)
cmd.run()

func handleaction(hyp string) {
switch hyp {
case "sleep":
executecommand("loginctl", "lock-session")
case "wake up":
executecommand("loginctl", "unlock-session")
case "poweroff":
executecommand("poweroff")


func main() {
cfg := sphinx.newconfig(
sphinx.hmmdiroption("/usr/local/share/pocketsphinx/model/en-us/en-us"),
sphinx.dictfileoption("6129.dic"),
sphinx.lmfileoption("6129.lm"),
sphinx.logfileoption("commander.log"),

dec, err := sphinx.newdecoder(cfg)
if err != nil {
panic(err)

listen(dec)

client.go hosted with ? by github
(左右滑动查看全部代码)
上述的代码将把所有的依赖声明与源文件绑定在一起。
如你所见,go语言是如此的简单、最小化但仍足够满足需要并且十分优雅。go语言提供了诸多的直接的工具支持,既可用于单元测试,也可以用于benchmark的火焰图。诚然,正如前面所讲到的特性集方面的限制,go语言也有其缺陷。比如, goget 并不支持版本化,一旦源文件中引用了某个url,就将锁定于此。但是,go也还在逐渐的演进,一些依赖管理的工具也正在涌现。
golang最初是设计用来解决google的一些产品问题,比如厚重的代码库,以及满足编写高效并发类应用的急迫需求。在需要利用现代处理器的多核特性的场景,go语言使得在应用和库文件的编程方面变得更加容易。并且,这些都不需要开发者来考虑。go语言是一门现代的编程语言,简单是其主旨,go语言永远不会考虑超过这一主旨的范畴。
二、protobuf(protocol buffers)
protobuf 或者说 protocol buffers是由google研发的一种二进制通信格式,用以对结构化数据进行序列化。格式是什么意思?类似于json这样?是的。protobuf已经有10年的历史,在google内部也已经使用了一段时间。
既然已经有了json这种通信格式,并且得到了广泛的应用,为什么需要protobuf?
与golang一样,protobuf实际上并有解决任何新的问题,只是在解决现有的问题方面更加高效,更加现代化。与golang不同的是,protobuf并不一定比现存的解决方案更加优雅。下面是protobuf的主要特性:
protobuf是一种二进制格式,不同于json和xml,后者是基于文本的也因此相对比较节省空间。
protobuf提供了对于schema的精巧而直接的支持
protobuf为生成解析代码和消费者代码提供直接的多语言支持。
protobuf的二进制格式带来的是传输速度方面的优化
那么protobuf是不是真的很快?简单回答,是的。根据google developer的数据,相对于xml来说,protobuf在体积上只有前者的1/3到1/10,在速度上却要快20到100倍。毋庸置疑的是,由于采用了二进制格式,序列化的数据对于人类来说是不可读的。
(image source: beating json performance with protobuf)
相对其他传输协议格式来说,protobuf采用了更有规划性的方式。首先需要定义 .proto 文件,这种文件与schema类似,但更强大。在 .proto 文件中定义消息结构,哪些字段是必选的哪些是可选的,以及字段的数据类型等。接下来,protobuf编译器会生成用于数据访问的类,开发者可以在业务逻辑中使用这些类来更方便的进行数据传输。
观察某个服务的 .proto 文件,可以清晰的获知通信的细节以及暴露的特性。一个典型的 .proto 文件类似如下:
message person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum phonetype {
mobile = 0;
home = 1;
work = 2;

message phonenumber {
required string number = 1;
optional phonetype type = 2 [default = home];

repeated phonenumber phone = 4;

protobufexample.proto hosted with ? by github
(左右滑动查看全部代码)
曝个料:stack overflow的大牛jon skeet也是protobuf项目的主要贡献者之一。
三、grpc
grpc,物如其名,是一种功能齐备的现代的rpc框架,提供了诸多内置支持的机制,如负载均衡、跟踪、健康检查和认证等。grpc由google在2015年开源,并由此日益火爆。
既然已经有了rest,还搞个rpc做什么?
在soa架构的时代,有相当长的时间,基于wsdl的soap协议是系统间通信的解决方案。彼时,通信协议的定义是十分严格的,庞大的单体架构系统暴露大量的接口用于扩展。
随着b/s理念的兴起,服务器和客户端开始解耦,在这样的架构下,即使客户端和服务端分别进行独立的编码,也不影响对服务的调用。客户端想查询一本书的信息,服务端会根据请求提供相关的列表供客户端浏览。rest范式主要解决的就是这种场景下的问题,rest允许服务端和客户端可以自由的通信,而不需要定义严格的契约以及独有的语义。
从某种意义上讲,此时的服务已经开始变得像是单体式架构系统一样,对于某个特定的请求,会返回一坨毫无必要的数据,用以满足客户端的“浏览”需求。但这并不是所有场景下都会发生的情况,不是么?
跨入微服务的时代
采用微服务架构理由多多。最常提及的事实是,单体架构太难扩展了。以微服务架构设计大型系统,所有的业务和技术需求都倾向于实现成互相合作的组件,这些组件就是“微”服务。
微服务不需要以包罗万象的信息响应用户请求,而仅需要根据请求完成特定的任务并给出所需要的回应。理想情况下,微服务应该像一堆可以无縫组装的函数。
使用rest做为此类服务的通信范式变得不那么有效。一方面,采用rest api确实可以让服务的表达能力更强,但同时,如果这种表达的能力既非必要也并不出自设计者的本意,我们就需要根据不同的因素考虑其他范式了。
grpc尝试在如下的技术方面改进传统的http请求:
默认支持http/2协议,并可以享受该协议带来的所有好处
采用protobuf格式用于机器间通信
得益于http/2协议,提供了对流式调用的专有支持
对所有常用的功能提供了插件化的支持,如认证、跟踪、负载均衡和健康检查等。
当然,既然是rpc框架,仍旧会有服务定义和接口描述语言(dsl)的相关概念,rest世代的开发者可能会感觉这些概念有些格格不入,但是由于grpc采用protobuf做为通信格式,就不会显得像以前那么笨拙。
protobuf的设计理念使得其既是一种通信格式,又可以是一种协议规范工具,在此过程中无需做任何额外的工作。一个典型的grpc服务定义类似如下:
service helloservice {
rpc sayhello (hellorequest) returns (helloresponse);

message hellorequest {
string greeting = 1;

message helloresponse {
string reply = 1;

servicedefinition.proto hosted with ? by github
(左右滑动查看全部代码)
只需要为服务定义一个 .proto 文件,并在其中描述接口名称,服务的需求,以及以protobuf格式返回的消息即可。protobuf编译器会生成客户端和服务端代码。客户端可以直接调用这些代码,服务端可以用这些代码实现api来填充业务逻辑。
四、结语
golang,protobuf和grpc是现代服务端编程的后起之秀。golang简化了并发/并行应用的编程,grpc和protobuf的结合提供了更高效的通信并同时提供了更愉悦的开发者体验。

学习KNN算法的基本原理,并用Python实现该算法以及阐述其应用价值
工业网关应用分享:PLC远程监控与远程维护
外媒曝华为MateX 塑料屏幕不耐划
巡检机器人助力天然气站安全管理
科创板福光股份董事会秘书黄健介绍、履历信息
现代的服务端技术栈:Golang/Protobuf/gRPC详解
物联网技术让消防栓更智慧
小米电视4怎么样?在工艺探索上努力到无能为力
蔚来ES8这款豪华SUV即将上市!百万路虎也没这气质!首款纯电动suv迎来了量产
宏碁智能佛珠了解一下
第十届电感变压器行业年度评选正式启动!
IGBT驱动电路
天翼又添一员悍将 飞利浦D900新机上市
ODU,全方位满足高速数据传输的要求
一文详解行星齿轮变速器
欧洲半导体计划反超,底牌是什么?
5G to B正值突破式创新的“天使期”
佳能推出了单反系的高端镜头EF 85mm f/1.4L IS USM具有防抖设计
ARM vs x86云数据库性能对比分析
2020主动降噪高性能耳机排行_分体式无线蓝牙耳机