当前位置:网站首页>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
边栏推荐
- AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
- 四层和八层电梯控制系统Proteus仿真设计,51单片机,附仿真和Keil C代码
- Arduino for esp8266串口功能简介
- 《JVM系列》 第七章 -- 字节码执行引擎
- Find daffodils - for loop practice
- GIS数据处理-cesium中模型位置设置
- 想要成为架构师?夯实基础最重要
- 8.2 文本预处理
- OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
- Swift - literal, literal protocol, conversion between basic data types and dictionary / array
猜你喜欢

Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)

基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】

Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)

【Servlet】Servlet 详解(使用+原理)

基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等

1 - first knowledge of go language

QT interface optimization: QT border removal and form rounding

51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc

自动化的艺术

8.5 循环神经网络简洁实现
随机推荐
数组模拟队列进阶版本——环形队列(真正意义上的排队)
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
epoll 的 ET,LT工作模式———实例程序
多语言通信基础 06 go实现grpc的四种数据流模式实现
科技的成就(二十一)
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
we引用My97DatePicker 实现时间插件使用
QT actual combat: Yunxi chat room
《JVM系列》 第七章 -- 字节码执行引擎
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
vscode中文插件不生效问题解决
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
Arduino for esp8266串口功能简介
Detailed explanation of SAR command
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
【Servlet】Servlet 详解(使用+原理)
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
Four ways of SSH restricting login
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?