当前位置:网站首页>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 flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
- 51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
- Don't you know the usage scenario of the responsibility chain model?
- 555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
- 压缩映射定理
- 2-Go变量操作
- Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
- 八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
- 8.3 语言模型与数据集
- One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
猜你喜欢

MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program

Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)

数组模拟队列进阶版本——环形队列(真正意义上的排队)

外包幹了四年,廢了...

OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
![[jz46 translate numbers into strings]](/img/e0/f32249d3d2e48110ed9ed9904367c6.png)
[jz46 translate numbers into strings]

ASEMI超快恢复二极管与肖特基二极管可以互换吗

555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等

金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)

51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
随机推荐
epoll 的EPOLLONESHOT 事件———实例程序
LotusDB 设计与实现—1 基本概念
详解TCP的三次握手
Epoll's et, lt working mode -- example program
Want to be an architect? Tamping the foundation is the most important
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
Basic regular expression
we引用My97DatePicker 实现时间插件使用
vscode中文插件不生效问题解决
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
多语言通信基础 06 go实现grpc的四种数据流模式实现
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
外包幹了四年,廢了...
想要成为架构师?夯实基础最重要
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
OpenFaaS实战之四:模板操作(template)
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
capacitance