gRPC是Google开源的一个高性能、跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,基于Netty 4.x +。

gRPC与thrift、avro-rpc、WCF等其实在总体原理上并没有太大的区别,简而言之GRPC并没有太多突破性的创新。

protobuf相对于用Json方式传输,效率有很大提高,Kubernetes也从最初的Json转换成了gRPC

gRPC 在服务端提供一个 gRPC Server,客户端的库是 gRPC Stub。

典型的场景是客户端发送请求,同步或异步调用服务端的接口。

gRPC在服务端和客户端可以使用多种语言,包括Go, Python, Java or Ruby等。

下面以GoLang为例,展示一个gRPC Client调用gRPC Server的整个过程:

1. 准备gRPC环境

1) 安装Go语言环境,gRPC要求Go 1.5或以上。

2) 安装gRPC

$ go get google.golang.org/grpc

3) 安装Protocol Buffers v3

下载protoc-3.0.0-linux-x86_64.zip:https://github.com/google/protobuf/releases

解压并拷贝bin/protoc到$GOPATH (/usr/local/go/bin)

4) 安装protoc plugin for Go

$ go get -u github.com/golang/protobuf/protoc-gen-go

拷贝bin/protoc-gen-go到$GOPATH (/usr/local/go/bin)

2. 编写helloworld.proto协议文件,简单完成一个加法运算

syntax = "proto3";

package helloworld;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayAdd (AddRequest) returns (AddReply) {}
} // The request message containing the a+b.
message AddRequest {
uint32 a = ;
uint32 b =;
} // The response message containing the c.
message AddReply {
uint32 c = ;
}

3. 使用protoc-gen-go生成gRPC服务端和客户端代理文件helloworld.pb.go

$ protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld

4. 编写gRPC服务端文件greeter_server/main.go

package main

import (
"log"
"net" "golang.org/x/net/context"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc/reflection"
) const (
port = ":50051"
) // server is used to implement helloworld.GreeterServer.
type server struct{} // SayAdd implements helloworld.GreeterServer
func (s *server) SayAdd(ctx context.Context, in *pb.AddRequest) (*pb.AddReply, error) {
return &pb.AddReply{C: in.A + in.B}, nil
} func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
// Register reflection service on gRPC server.
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}

5. 编写gRPC客户端文件greeter_client/main.go

package main

import (
"log"
"os" "golang.org/x/net/context"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
) const (
address = "localhost:50051"
) func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
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.
rs, errs := c.SayAdd(context.Background(), &pb.AddRequest{A: , B:})
if errs != nil {
log.Fatalf("could not greet: %v", errs)
}
log.Printf("Add: %d", rs.C)
}

6. 运行gRPC服务端

$ go run greeter_server/main.go

7. 运行gRPC客户端

$ go run greeter_client/main.go

输出结果: Add: 3

 Demo下载地址:http://files.cnblogs.com/files/edisonxiang/helloworld.zip

gRPC GoLang Test的更多相关文章

  1. 【Networking】gRPC golang 相关资料

    参考资料: Golang gRPC 示例:  http://www.cnblogs.com/YaoDD/p/5504881.html grpc golang学习心得(1)----安装与测试:   ht ...

  2. gRPC Golang/Python使用

    gRPC Golang/Python使用 以前开发网站都是用http协议,学过TCP/IP协议的人都知道,在传输层TCP的基础上,应用层HTTP就是填充了一定规则的文本. 1.gRPC使用和介绍 工作 ...

  3. Golang gRPC 和 gRPC-gateway 结合使用

    一.安装 go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway go get -u github.com/g ...

  4. Golang gRPC 使用

    一.概念 1.gRPC默认使用protocol buffers,这是google开源的一套成熟的结构数据序列化机制(当然也可以使用其他数据格式如JSON),可以用proto files创建gRPC服务 ...

  5. gRPC helloworld service, RESTful JSON API gateway and swagger UI

    概述 本篇博文完整讲述了如果通过 protocol buffers 定义并启动一个 gRPC 服务,然后在 gRPC 服务上提供一个 RESTful JSON API 的反向代理 gateway,最后 ...

  6. 开源RPC(gRPC/Thrift)框架性能评测

    海量互联网业务系统只能依赖分布式架构来解决,而分布式开发的基石则是RPC:本文主要针对两个开源的RPC框架(gRPC. Apache Thrift),以及配合GoLang.C++两个开发语言进行性能对 ...

  7. grpc,protoc, protoc-gen-go,rust

    Rust 与服务端编程的碎碎念https://zhuanlan.zhihu.com/p/30028047 GRPC:golang使用protobuf https://segmentfault.com/ ...

  8. 【学习笔记】Golang学习方向整理

    前言 作为一个Java开发,给大家说Golang方向,好吓人...溜了溜了... 哦对了,如有不对的地方,还请指出.感谢! 某面试平台golang技能要求简要摘录 掌握 GO 语言,熟悉常用 pack ...

  9. golang下的grpc

    facebook的thrift也是开源rpc库,性能高出grpc一倍以上,grpc发展的较晚,期待以后有长足的进步.简单来说thrift = grpc + protobuf gRPC基于HTTP/2标 ...

随机推荐

  1. 2、Semantic-UI之网格布局

    2.1 网格布局   在semantic-ui中提供了16个网格,使用class="column",当然也可以通过数字来表示当前网格大小.   在Semantic-UI中定义的网格 ...

  2. 简单介绍Java的静态分派和动态分派

    最近复习JVM的知识,对于静态分派和动态分派的理解有点混乱,于是自己尝试写写代码,在分析中巩固知识. 有如下一段代码,请问每一段分别输出什么? package com.khlin.my.test; c ...

  3. vmware中安装centos 6.7

    centos 6.7 软件下载地址:http://b.mirrors.lanunion.org/CentOS/6.7/isos/i386/ 引用:http://www.cnblogs.com/sees ...

  4. 减少C盘空间占用的技巧

    1.搜索C盘中大小大于某个值的文件:C:\Windows\SoftwareDistribution这个文件夹下很多大文件 2.搜索*.log文件 3.C:\Users\Guangshan\AppDat ...

  5. Mathcad操作tips:算式输入、变量定义与计算

    算式输入 1. 数字与符号相乘,输入时不必手动输入乘号(“*”). 2. 以下有助于算式的可视化:a. 使用Math工具栏输入,并合理使用tab键:b. 合理使用空格键. 3. 输入开根号时,可用快捷 ...

  6. IO--RAID

    RAID IO计算 Raid 0 –每个磁盘的I/O计算= (读+写) /磁盘个数 Raid 1 --每个磁盘的I/O计算= [读+(2*写)]/2 Raid 5 --每个磁盘的I/O计算= [读+( ...

  7. SOLR企业搜索平台 三 (schema.xml配置和solrj的使用)

    标签:solrj 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://3961409.blog.51cto.com/3951409/8 ...

  8. 工作中的Buff加成-结构化思考力:第一章:认识结构化思维及其作用

    一:引言 为了更好的说明结构思考力,我们先来做几个小测试. PS:如果你能做到,请留言,因为我要和你交好友,因为你是人才啊,可以挖一挖,挖到我的公司中. 第一个测试:请在三秒内记住下列数字.数字顺序不 ...

  9. WPF成长之路------帧动画(1)

    最近公司的一个项目因为是WPF的,而自己已经很长一段时间没有接触过WPF了,再加上之前没有做过wpf的动画效果,因此在学习的过程中也顺便记录一下,说不定以后还会用上,同时也算是总结一下吧!刚开始写博客 ...

  10. droup

    Oracle Drop表并未直接删除 drop table xx purge   drop表 执行drop table xx 语句     drop后的表被放在回收站(user_recyclebin) ...