当前位置:网站首页>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单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
- c语言在结构体传参时参数压栈问题
- 2-GO variable operation
- 帧同步 实现
- 2-Go变量操作
- 《JVM系列》 第七章 -- 字节码执行引擎
- Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
- 一个月把字节,腾讯,阿里都面了,写点面经总结……
- TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
- 单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
猜你喜欢
ASEMI整流模块MDQ100-16在智能开关电源中的作用
UML project example -- UML diagram description of tiktok
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
ASEMI超快恢复二极管与肖特基二极管可以互换吗
Find daffodils - for loop practice
1-初识Go语言
Detailed explanation of SAR command
LotusDB 设计与实现—1 基本概念
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
你还不知道责任链模式的使用场景吗?
随机推荐
How do I open the win10 startup folder?
Frame synchronization implementation
ASEMI三相整流桥和单相整流桥的详细对比
Resolve the conflict between computed attribute and input blur event
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
DVWA之暴力破解(Brute Force)Low-->high
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
想要成为架构师?夯实基础最重要
LotusDB 设计与实现—1 基本概念
qt之.pro文件详解
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
Want to be an architect? Tamping the foundation is the most important
Select receives both normal data and out of band data
Detailed explanation of C language P2 selection branch statement
8.3 语言模型与数据集
Use of ansible and common modules
Some little records~
自动化的艺术
QT interface optimization: double click effect