官方文档

前置技能 protobuf

什么是 gRPC?

A high performance, open-source universal RPC framework

RPC : Remote Procedure Call

在gRPC中,客户端应用程序可以直接在其他计算机上的服务器应用程序上调用方法,就好像它是本地对象一样,这使您更轻松地创建分布式应用程序和服务。 与许多RPC系统一样,gRPC围绕定义服​​务的思想,指定可通过其参数和返回类型远程调用的方法。 在服务器端,服务器实现此接口并运行gRPC服务器以处理客户端调用。 在客户端,客户端具有一个存根(在某些语言中仅称为客户端),提供与服务器相同的方法。

如何工作?

在默认情况下 gRPC 使用 protocol bufers 进行序列化结构化数据(serializing structured data),当然也可以使用其他数据格式,比如JSON。建议将proto3gRPC一起使用

工作步骤:(默认使用protobuf的情况下)

  • helloworld.proto文件中定义gRPC服务,将RPC方法参数和返回类型指定为protobuf消息,像下面这样:
// The greeter service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
} // The request message containing the user's name.
message HelloRequest {
string name = 1;
} // The response message containing the greetings
message HelloReply {
string message = 1;
}
  • 生成代码,gRPC使用protoc工具以及gRPC的插件来生成代码

安装和使用

首先先安装protobufprotoc(见前置技能链接)以及对应语言的支持

对于不同的语言,需要为protoc安装对应的gRPC插件

c++

$ # 克隆仓库,-c选项是添加代理,可以不用
$ git -c http.proxy=socks5://192.168.0.103:1080 clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
$ cd grpc
$ git -c http.proxy=socks5://192.168.0.103:1080 submodule update --init
$ mkdir build && cd build
$ cmake ..
$ make -j4
$ sudo make install

仓库目录grpc/examples/cpp/helloworld下有示例源码

仓库目录grpc/examples/protos下有.proto文件,包括helloworld.proto

生成代码的命令是:

$ protoc -I . --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` helloworld.proto

go

$ export https_proxy=socks5://127.0.0.1:1080  # 这个是我自己代理
$ go get -u google.golang.org/grpc

然后应可以在$GOPATH/src/google.golang.org/grpc/examples/helloworld目录找到示例

生成代码的命令$ protoc -I. ./helloworld.proto --go_out=plugins=grpc:.

将在当前目录生成helloworld.pb.go文件

使用:

// server 端
package main import (
"context"
"log"
"net" "google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
) const (
port = ":50051"
) // server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
} // SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Hello " + in.GetName()}, 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{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
} // client 端
package main import (
"context"
"log"
"os"
"time" "google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
) const (
address = "localhost:50051"
defaultName = "world"
) func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
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.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}

gRPC用法的更多相关文章

  1. Java 开发 gRPC 服务和客户端

    新建一个普通的Maven项目: 配置pom文件,导入gRPC的依赖和插件 pom 中增加外部依赖 <dependency>     <groupId>io.grpc</g ...

  2. grpc介绍

    grpc入门(一) 一.什么是grpc grpc是谷歌开源的一款高性能的rpc框架 (https://grpc.io),可以使用protocol buffers作为IDL(Interface Defi ...

  3. 入门干货之Grpc的.Net实现-MagicOnion

    此文章简单残暴,学习成本较低,你可以跟着我一起撸代码,一起吐槽,一起砸键盘.以下操作均为 core2.0 环境. 0x01.Grpc 1.介绍  Google主导开发的RPC框架,使用HTTP/2协议 ...

  4. google的grpc在golang中的使用

    GRPC是google开源的一个高性能.跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,基于Netty 4.x. 前面写过一篇golang标准库的rpc包的用法,这篇文章接着讲一 ...

  5. gRPC源码分析(c++)

    首先需要按照grpc官网上说的办法从github上下载源码,编译,然后跑一跑对应的测试代码.我分析的代码版本为v1.20.0. 在cpp的helloworld例子中,client端,第一个函数是创建c ...

  6. protobuffer、gRPC、restful gRPC的相互转化

    转自:https://studygolang.com/articles/12510 文档 grpc中文文档 grpc-gateway,restful和grpc转换库 protobuf 官网 proto ...

  7. gRPC初探——概念介绍以及如何构建一个简单的gRPC服务

    目录 引言 1. gRPC简介 2. 使用Protocol Buffers进行服务定义 2.1 定义消息 2.2 定义服务接口 3.构建简单的gRPC服务 3.1 编写proto文件,定义消息和接口 ...

  8. 带入gRPC:gRPC Deadlines

    带入gRPC:gRPC Deadlines 原文地址:带入gRPC:gRPC Deadlines项目地址:https://github.com/EDDYCJY/go... 前言 在前面的章节中,已经介 ...

  9. Gonet2 游戏server框架解析之gRPC提高(5)

    上一篇blog是关于gRPC框架的基本使用,假设说gRPC仅仅是远程发几个參数,那和一个普通的http请求也没多大区别了. 所以今天我就来学习一下gRPC高级一点的用法. 流! 流能够依据用法,分为单 ...

随机推荐

  1. jmeter使用—远程分布式

    今天要说的是在远程服务器上使用多台服务器进行noGUI分布式使用jmeter压测. 1.首先准备几台服务器,服务器上都需要安装同一个版本的jmeter. 2.在服务器上启动jmeter的方式是在jme ...

  2. API文档自动生成,Swagger的配置

    ASP.NET的部署方式 第一步:引用程序集 打开NuGet程序包管理器,搜索Swagger,安装第一个,注意画圈的地方, 已经包含主程序和UI了,安装完成后会在根目录App_Start文件夹下生成S ...

  3. Android教程2020 - RecyclerView响应点击

    本文介绍RecyclerView设置点击的方法.这里给出比较常见的使用方式. Android教程2020 - 系列总览 本文链接 前面我们已经知道如何用RecyclerView显示一列数据. 用户点击 ...

  4. IdentityServer4身份认证授权入门

    一.简介 IdentityServer4 是为ASP.NET Core 系列量身打造的一款基于 OpenID Connect 和 OAuth 2.0 认证框架 特点: 1.认证服务 2.单点登录登出( ...

  5. Rancher2.x 一键式部署 Prometheus + Grafana 监控 Kubernetes 集群

    目录 1.Prometheus & Grafana 介绍 2.环境.软件准备 3.Rancher 2.x 应用商店 4.一键式部署 Prometheus 5.验证 Prometheus + G ...

  6. C++输出中文字符

    注:本文转载自互联网,感谢作者整理!   1. cout 场景1: 在源文件中定义 const char* str = "中文" 在 VC++ 编译器上,由于Windows环境用 ...

  7. 9. HanLP《自然语言处理入门》笔记--9.关键词、关键句和短语提取

    笔记转载于GitHub项目:https://github.com/NLP-LOVE/Introduction-NLP 9. 信息抽取 信息抽取是一个宽泛的概念,指的是从非结构化文本中提取结构化信息的一 ...

  8. ARTS Week 5

    Nov 25, 2019 ~ Dec 1, 2019 Algorithm 深度优先搜索--书籍分配 题目描述:有b1-b5五本书,要分配给五个学生,分别是a1-a5.但每个学生都有其喜欢的书,要检查是 ...

  9. 调用caffe脚本将图片转换为了lmdb格式

    #!/usr/bin/env sh # Create the imagenet lmdb inputs # N.B. set the path to the imagenet train + val ...

  10. Codeforces 1011C Fly(二分+模拟)

    题意: 是有n个星球,1代表地球,然后输入一个m表示火箭的重量,然后输入了两组n个数,第一组表示在每个星球起飞时消耗一吨燃料的质量数,a[i]就表示每a[i]吨就要消耗1吨燃料,第二组就表示在每个星球 ...