官方文档

前置技能 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. 在ubuntu18.04下搭建kvm

    前一段时间一直在尝试Ubuntu上搭建xen,一直出现各种问题,各种坑 首先先感谢下面这个公司对我的耐心解答,非常感谢.特别是后面来的电话对我进行了详细的解答,所以选择搭建kvm. 1. 需要检查一下 ...

  2. NOde.js的安装和简介

    1.nodejs的安装 1.1 检测nodejs的版本 node -v (version:版本) 1.2 path配置nodejs 的环境变量(当前版本都是自动安装配置环境变量)指令: path 1. ...

  3. linux 修改系统时间 同步网络时间

    一.date命令 date -s time  修改系统时钟时间为time 设置时间和日期 例如:将系统日期设定成2018年6月8日的命令 命令 : "date -s 06/08/2018&q ...

  4. cmd 重定向

    关于cmd 命令的重定向输出 2>&1 mycommand >mylog.txt 2>&1 应该是最经典的用法了. 命令的结果可以通过" %> &qu ...

  5. ipwry源码

    qqwry.ipwry都是cnss(http://blog.csdn.net/cnss/article/details/136069)出品,终于找到了源码,下载地址:http://download.c ...

  6. PAT_B1002数字分类

    #include<stdio.h> #include<iostream> using namespace std; int main(){ ,a2=,a3=,a5=; ; ; ...

  7. shell正则表达式提取数字

    grep 提取数字 grep -Po "\d+\.\d+"

  8. SpringBoot整合ActiveMQ和开启持久化

    一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  9. AI: 绘制图标的实例

    AI绘制矢量电影图标 http://www.fevte.com/tutorial-2299-1.html AI绘制水晶质感QUICKTIME图标 http://wenku.baidu.com/view ...

  10. HDU 6186 CS Course (连续位运算)

    CS Course Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...