当前位置:网站首页>Realization of four data flow modes of grpc based on Multilingual Communication
Realization of four data flow modes of grpc based on Multilingual Communication
2022-04-23 14:44:00 【Yi Yue Wang Chao】
grpc There are many data transmission modes :
- Simple mode : The client initiates a request , The server responds to data once , And ordinary rpc There is no difference between .
- Server side data flow mode : The client initiates a request , The server returns a continuous data stream . A typical example is that the client sends a stock code to the server , The server continuously returns the data of the stock to the client in real time . In addition, our commonly used subscription scenarios also belong to the service side stream mode .
- Client data stream mode . Contrary to the server-side data flow pattern , The client continuously sends data streams like the server , After sending , The server returns a response . A typical example is that the Internet of things terminal sends data to the server .
- Two way data flow pattern : The client and server can send real-time data streams to both sides for interaction , Typical applications are chat robots , Instant messaging tools, etc .
propto file
Various modes of proto The contents of the document are as follows :
syntax = "proto3";
option go_package = ".;proto"; // Indicates the current directory
service Greeter {
rpc GetStream(StreamReqData)returns(stream StreamResData); // Server stream mode
rpc PutStream(stream StreamReqData)returns(StreamResData); // Client stream mode
rpc Al1Stream(stream StreamReqData)returns(stream StreamResData); // Two way flow mode
}
message StreamReqData {
string data = 1;
}
message StreamResData {
string data = 1;
}
serve Server program
The server must implement proto Three functions in .
package main
import (
"OldPackageTest/stream_grpc_test/proto"
"fmt"
"google.golang.org/grpc"
"net"
"sync"
"time"
)
const PORT = ":50052"
type server struct {
}
// Server stream mode
func (s *server) GetStream(req *proto.StreamReqData, res proto.Greeter_GetStreamServer) error {
i := 0
for {
i++
_ = res.Send(&proto.StreamResData{
Data: fmt.Sprintf("%v", time.Now().Unix()),
})
time.Sleep(time.Second)
if i > 10 {
break
}
}
return nil
}
// Client stream mode
func (s *server) PutStream(cliStr proto.Greeter_PutStreamServer) error {
for {
if a, err := cliStr.Recv(); err != nil {
fmt.Println(err)
break
} else {
fmt.Println(a.Data)
}
}
return nil
}
// Two way flow mode
func (s *server) AllStream(allStr proto.Greeter_AllStreamServer) error {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
for {
data, _ := allStr.Recv()
fmt.Println(" Received client message :" + data.Data)
}
}()
go func() {
defer wg.Done()
for {
_ = allStr.Send(&proto.StreamResData{Data: " I'm the server "})
time.Sleep(time.Second)
}
}()
wg.Wait()
return nil
}
func main() {
lis, err := net.Listen("tcp", PORT)
if err != nil {
panic(err)
}
s := grpc.NewServer()
proto.RegisterGreeterServer(s, &server{})
err = s.Serve(lis)
if err != nil {
panic(err)
}
}
client Client program
package main
import (
"context"
"fmt"
"sync"
"time"
"google.golang.org/grpc"
"OldPackageTest/stream_grpc_test/proto"
)
func main() {
conn, err := grpc.Dial("localhost:50052", grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()
// Server stream mode
c := proto.NewGreeterClient(conn)
res, _ := c.GetStream(context.Background(), &proto.StreamReqData{Data: " For class network "})
for {
a, err := res.Recv() // If you know socket Programming words will understand send recv
if err != nil {
fmt.Println(err)
break
}
fmt.Println(a.Data)
}
// Client stream mode
putS, _ := c.PutStream(context.Background())
i := 0
for {
i++
_ = putS.Send(&proto.StreamReqData{
Data: fmt.Sprintf(" For class network %d", i),
})
time.Sleep(time.Second)
if i > 10 {
break
}
}
// Two way flow mode
allStr, _ := c.AllStream(context.Background())
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
for {
data, _ := allStr.Recv()
fmt.Println(" Received client message :" + data.Data)
}
}()
//1. Concentrated learning protobuf, grpc
go func() {
defer wg.Done()
for {
_ = allStr.Send(&proto.StreamReqData{Data: " For class network "})
time.Sleep(time.Second)
}
}()
wg.Wait()
}
版权声明
本文为[Yi Yue Wang Chao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231441296333.html
边栏推荐
- 51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
- Master in minutes --- ternary operator (ternary operator)
- pnpm安装使用
- 一个月把字节,腾讯,阿里都面了,写点面经总结……
- 科技的成就(二十一)
- Some little records~
- raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
- go基础 反射
- Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
猜你喜欢
你还不知道责任链模式的使用场景吗?
外包干了四年,废了...
[jz46 translate numbers into strings]
LotusDB 设计与实现—1 基本概念
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
抑郁症治疗的进展
Want to be an architect? Tamping the foundation is the most important
Detailed explanation of SAR command
随机推荐
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
Parameter stack pressing problem of C language in structure parameter transmission
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
Upgrade of openssh and modification of version number
QT interface optimization: double click effect
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
数组模拟队列进阶版本——环形队列(真正意义上的排队)
外包干了四年,废了...
1 - first knowledge of go language
帧同步 实现
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
一个月把字节,腾讯,阿里都面了,写点面经总结……
详解TCP的三次握手
2-GO variable operation
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
Arduino for esp8266串口功能简介
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
Find daffodils - for loop practice