4 modes of Client-to-Server data interaction in GRPC

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Project Address: Grpc-modes

Recently, using the GRPC introductory microservices development, we summarize the 4 modes of operation in GRPC

Directory structure

$GOPATH└── grpc    ├── simple                  // 简单模式 RPC    │   ├── client        │   │   └── client.go        # 客户端代码    │   ├── proto                │   │   ├── user.pb.go        │   │   └── user.proto       # 通信的 protobuf 协议    │   └── server    │       └── server.go        # 服务端代码    ├── server-side-streaming    // 服务端流式 RPC     ├── client-side-streaming    // 客户端流式 RPC     └── bidirectional-streaming  // 客户端与服务端双向流式 RPC

UserService Micro-Service

A microservices is defined in this project: UserService it has only one RPC:GetUserInfo()

syntax = "proto3";package grpc.simple;// 定义 UserService 微服务service UserService {    // 微服务中获取用户信息的 RPC 函数    rpc GetUserInfo (UserRequest) returns (UserResponse);}// 客户端请求的格式message UserRequest {    int32 ID = 1;}// 服务端响应的格式message UserResponse {    string name = 1;    int32 age = 2;}

GetUserInfo()simulates a database in a function that stores the user's name and age:

// ID 为 key,用户信息为 value 模拟数据库查询结果var users = map[int32]pb.UserResponse{    1: {Name: "Dennis MacAlistair Ritchie", Age: 70},    2: {Name: "Ken Thompson", Age: 75},    3: {Name: "Rob Pike", Age: 62},}

The client requests the ID, and returns the user information as a response after the query.

Four modes of data interaction between client and server

Simpe Simple Mode RPC

The client initiates a request to the service side, and the server returns a response.

The client requests the user data with ID 2, and the server returns the user data with ID 2:

Server-side streaming service-side streaming RPC

The client initiates a request to the service side, and the server returns a continuous stream of traffic response.

The client requests 1 of the user data, and the server returns 1, 2, 3 of the user data stream:

Client-side streaming client-side streaming RPC

The client sends a continuous stream of traffic to the server, and the server returns a response.

The client requests 1, 2, 3 of the user data stream, and the server returns 3 of the user data:

Grpc of bidirectional streaming bidirectional data flow mode

The client sends a continuous stream of traffic to the server, and the server returns the interactive data stream.

The client requests 1, 2, 3 of the user data stream in turn, and the server returns 1, 2, and 3 of the user data stream:

At last

Recently in the system of learning Golang micro-services, starting from Grpc, to Go-micro, Docker micro-services, such as weekly updates. Welcome to follow my blog Wuyinblog

Hope this project is helpful to you ️

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.