当前位置: 首页 > news >正文

十八、Rust gRPC 多 proto 演示

十八、Rust gRPC 多 proto 演示

  网上及各官方资料,基本是一个 proto 文件,而实际项目,大多是有层级结构的多 proto 文件形式,本篇文章 基于此诉求,构建一个使用多 proto 文件的 rust grpc 使用示例。

关于 grpc 的实现,找到两个库:

  • Tonic:https://github.com/hyperium/tonic,8.9k Star、852 Commits、2024-03-12 updated。

  • PingCAP 的 grpc-rs:https://github.com/tikv/grpc-rs,1.8k Star、357 Commits、2023-08 updated。

  据说 PingCAP 的 grpc-rs benchmark 稍高一些,但看关注度和提交量不如 tonic,且据说 tonic 开发体验更好一些,本篇以 tonic 为例。

编译 Protobuf,还需要 protoc,可以参考官方文档,这里先给出 macOS 的:

  • brew install protobuf
  • https://grpc.io/docs/protoc-installation/

关于 Tonic:Tonic 是基于 HTTP/2 的 gRPC 实现,专注于高性能,互通性和灵活性;

目录说明

.
├── Cargo.toml
├── README.md
├── build.rs
├── proto
│   ├── basic
│   │   └── basic.proto
│   ├── goodbye.proto
│   └── hello.proto
└── src├── bin│   ├── client.rs│   └── server.rs├── lib.rs└── proto-gen├── basic.rs├── goodbye.rs└── hello.rs
  • build.rs 存放通过 proto 生成 rs 的脚本;
  • proto 目录放置 grpc 的 proto 文件,定义服务和消息体;
  • src 常规意义上的项目源码目录;
    • proto-gen 目录存放 build.rs 编译 proto 后生成的 rs 文件;
    • lib.rs 引入 proto 的 rs 文件;
    • bin 目录下进行 proto 所定义服务的实现,此例为 客户端、服务端 的实现;

创建项目

  • 创建一个 lib 项目:
cargo new grpc --lib
  • Cargo.toml
[package]
name = "grpc"
version = "0.1.0"
edition = "2021"
description = "A demo to learn grpc with tonic."# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[[bin]]
name="grpc_server"
path="src/bin/server.rs"[[bin]]
name="grpc_client"
path="src/bin/client.rs"[dependencies]
prost = "0.12.3"
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
tonic = "0.11.0"[build-dependencies]
tonic-build = "0.11.0"

定义服务

  • grpc/proto/basic/basic.proto
syntax = "proto3";package basic;message BaseResponse {string message = 1;int32 code = 2;
}
  • grpc/proto/hello.proto
syntax = "proto3";import "basic/basic.proto";package hello;service Hello {rpc Hello(HelloRequest) returns (HelloResponse) {}
}message HelloRequest {string name = 1;
}message HelloResponse {string data = 1;basic.BaseResponse message = 2;
}
  • grpc/proto/goodbye.proto
syntax = "proto3";import "basic/basic.proto";package goodbye;service Goodbye {rpc Goodbye(GoodbyeRequest) returns (GoodbyeResponse) {}
}message GoodbyeRequest {string name = 1;
}message GoodbyeResponse {string data = 1;basic.BaseResponse message = 2;
}

配置编译

  Rust 约定:在 build.rs 中定义的代码,会在编译真正项目代码前被执行,因此,可以在这里先编译 protobuf 文件;

  • grpc/Cargo.toml 引入
[build-dependencies]
tonic-build = "0.11.0"
  • grpc/build.rs
use std::error::Error;
use std::fs;static OUT_DIR: &str = "src/proto-gen";fn main() -> Result<(), Box<dyn Error>> {let protos = ["proto/basic/basic.proto","proto/hello.proto","proto/goodbye.proto",];fs::create_dir_all(OUT_DIR).unwrap();tonic_build::configure().build_server(true).out_dir(OUT_DIR).compile(&protos, &["proto/"])?;rerun(&protos);Ok(())
}fn rerun(proto_files: &[&str]) {for proto_file in proto_files {println!("cargo:rerun-if-changed={}", proto_file);}
}

稍作解释:

  • OUT_DIR 全局定义 proto 文件编译后的输出位置(默认在 target/build 目录下)。
  • let protos = [...] 声明了所有待编译 proto 文件。
  • tonic_build::configure()
    • .build_server(true) 是否编译 server 端,项目以 proto 为基准,则编就完了。
    • .compile(&protos, &["proto/"])?; 开始编译。

最终生成:

  • grpc/src/proto-gen/
    • basic.rs、hello.rs、goodbye.rs

由 proto 生成的原代码,内容一般较长,这里不贴出,感兴趣的读者,运行一下就可以看到。另外翻看其代码,可以看到:

  • 为客户端生成的HelloClient类型:impl<T> HelloClient<T> 实现了CloneSyncSend,因此可以跨线程使用。
  • 为服务端生成的 HelloServer 类型:impl<T: Hello> HelloServer<T> {} 包含了 impl<T: Hello>,预示着我们创建 HelloServer 实现,假设为 HelloService 时,需实现该 Hello Trait

引入proto生成的文件

  • grpc/src/lib.rs
#![allow(clippy::derive_partial_eq_without_eq)]pub mod basic {include!("./proto-gen/basic.rs");
}pub mod hello {include!("./proto-gen/hello.rs");
}pub mod goodbye {include!("./proto-gen/goodbye.rs");
}
  • 这里使用了标准库提供的 include! 来引入源文件;

  • 如果没有定义 proto 编译输出位置的话,默认是在 target/build 目录下,此时需要使用 tonic 提供的 include_proto!("hello") 宏,来引入对应文件,而不用额外提供路径了,其中的 hello 为 grpc 的 “包名”(proto文件中的 “package xxx;”),具体来说就是:

    • 注释掉 grpc/build.rs.out_dir(OUT_DIR) 一行。
    • grpc/src/lib.rs 中:
      • include!("./proto-gen/basic.rs"); 改为 include_proto!("basic");
      • include!("./proto-gen/hello.rs"); 改为 include_proto!("hello");
      • include!("./proto-gen/goodbye.rs"); 改为 include_proto!("goodbye");
    • 但这样,在进行 server、client 实现、源码编写时,将无法正常引用,致使大量 “漂红” (只 IDE 下这样,如 CLion,不影响 shell 下编译及运行) 。
  • 参考官方文档:https://docs.rs/tonic/latest/tonic/macro.include_proto.html

服务实现

  服务端实现各语言基本类似,为对应 proto 定义,创建相应的 Service 实现即可:

  • grpc/src/bin/server.rs
use tonic::{Request, Response, Status};
use tonic::transport::Server;use grpc::basic::BaseResponse;
use grpc::goodbye::{GoodbyeRequest, GoodbyeResponse};
use grpc::goodbye::goodbye_server::{Goodbye, GoodbyeServer};
use grpc::hello;
use hello::{HelloRequest, HelloResponse};
use hello::hello_server::{Hello, HelloServer};#[derive(Default)]
pub struct HelloService {}#[tonic::async_trait]
impl Hello for HelloService {async fn hello(&self, req: Request<HelloRequest>) -> Result<Response<HelloResponse>, Status> {println!("hello receive request: {:?}", req);let response = HelloResponse {data: format!("Hello, {}", req.into_inner().name),message: Some(BaseResponse {message: "Ok".to_string(),code: 200,}),};Ok(Response::new(response))}
}#[derive(Default)]
pub struct GoodbyeService {}#[tonic::async_trait]
impl Goodbye for GoodbyeService {async fn goodbye(&self,req: Request<GoodbyeRequest>,) -> Result<Response<GoodbyeResponse>, Status> {println!("goodbye receive request: {:?}", req);let response = GoodbyeResponse {data: format!("Goodbye, {}", req.into_inner().name),message: Some(BaseResponse {message: "Ok".to_string(),code: 200,}),};Ok(Response::new(response))}
}#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {let addr = "0.0.0.0:50051".parse()?;println!("server starting at: {}", addr);Server::builder().add_service(HelloServer::new(HelloService::default())).add_service(GoodbyeServer::new(GoodbyeService::default())).serve(addr).await?;Ok(())
}
  • grpc/src/bin/client.rs
use tonic::Request;
use tonic::transport::Endpoint;use grpc::goodbye::goodbye_client::GoodbyeClient;
use grpc::goodbye::GoodbyeRequest;
use grpc::hello;
use hello::hello_client::HelloClient;
use hello::HelloRequest;#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {let addr = Endpoint::from_static("https://127.0.0.1:50051");let mut hello_cli = HelloClient::connect(addr.clone()).await?;let request = Request::new(HelloRequest {name: "tonic".to_string(),});let response = hello_cli.hello(request).await?;println!("hello response: {:?}", response.into_inner());let mut goodbye_cli = GoodbyeClient::connect(addr).await?;let request = Request::new(GoodbyeRequest {name: "tonic".to_string(),});let response = goodbye_cli.goodbye(request).await?;println!("goodbye response: {:?}", response.into_inner());Ok(())
}

运行及测试

cargo run --bin grpc_server
cargo run --bin grpc_client

故障时重新编译:cargo clean && cargo build

关于 Github Action

  • 需添加步骤
- name: Install protocrun: sudo apt-get install -y protobuf-compiler

  
  
  完事 ~~
  
  

参考资料:

  • Rust grpc 实现 - https://jasonkayzk.github.io/2022/12/03/Rust%E7%9A%84GRPC%E5%AE%9E%E7%8E%B0Tonic/

  • Tonic 流式 grpc - https://github.com/hyperium/tonic/blob/master/examples/routeguide-tutorial.md

  • 开源库 - https://github.com/tokio-rs/prost

  • Tonic - https://github.com/hyperium/tonic

相关文章:

十八、Rust gRPC 多 proto 演示

十八、Rust gRPC 多 proto 演示 网上及各官方资料&#xff0c;基本是一个 proto 文件&#xff0c;而实际项目&#xff0c;大多是有层级结构的多 proto 文件形式&#xff0c;本篇文章 基于此诉求&#xff0c;构建一个使用多 proto 文件的 rust grpc 使用示例。 关于 grpc 的实现…...

【Linux】Linux64位环境下编译32位报错skipping incompatible的解决办法

本文首发于 ❄️慕雪的寒舍 问题 如题&#xff0c;当我尝试在wsl2的ubuntu中使用-m32选项编译32位程序的时候&#xff0c;出现了下面的两种报错 ❯ g -m32 test.cpp -o test1 && ./test1 In file included from test.cpp:1: /usr/include/stdio.h:27:10: fatal error…...

vue指令v-model

<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8"> <meta name"viewport" content"widthdevice-width, initial-scale1.0"> <title>vue指令v-model</title> </head>…...

CentOS安装MySQL数据库

一、更新yum源 #下载对应repo文件 wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo #清除缓存 yum clean all #生成新缓存 yum makecache #更新 yum update -y 二、安装MySQL #获取源 wget http://repo.mysql.com/mysql80-community-release-el7-3.…...

从B2B转向B2B2C模式:工业品牌史丹利百得的转型历程

图片来源&#xff1a;Twitter 在当今数据驱动的营销环境中&#xff0c;企业努力更好了解客户&#xff0c;并在整个客户旅程中提供个性化体验。史丹利百得&#xff08;Stanley Black & Decker&#xff09;是一家领先的工具和工业设备供应商&#xff0c;近年来开始重大转型。…...

Redis群集模式和rsync远程同步

一、Redis群集模式 1.1 概念 1.2 作用 1.2.1 Redis集群的数据分片 1.2.2 Redis集群的主从复制模型 1.3 搭建Redis 群集模式 1.3.1 开启群集功能 1.3.2 启动redis节点 1.3.3 启动集群 1.3.4 测试群集 二、rsync远程同步 2.1 概念 2.2 同步方式 2.3 备份的方式 2.4…...

JAVA—抽象—定义抽象类Converter及其子类WeightConverter

同样&#xff0c;我们由这道题引出抽象类&#xff0c;抽象方法这个概念。 按下面要求定义类Converter及其子类WeightConverter 定义抽象类&#xff1a;Converter&#xff1a; 定义一个抽象类Converter&#xff0c;表示换算器&#xff0c;其定义的如下&#xff1a; 一个私有…...

面对复杂多变的网络攻击,企业应如何守护网络安全

企业上云&#xff0c;即越来越多的企业把业务和数据&#xff0c;迁移到云端。随着云计算、大数据、物联网、人工智能等技术的发展&#xff0c;用户、应用程序和数据无处不在&#xff0c;企业之间的业务边界逐渐被打破&#xff0c;网络攻击愈演愈烈&#xff0c;手段更为多。 当前…...

计算机网络练习-计算机网络概述与性能指标

计算机网络概述 ----------------------------------------------------------------------------------------------------------------------------- 1. 计算机网络最据本的功能的是( )。 1,差错控制 Ⅱ.路由选择 Ⅲ,分布式处理 IV.传输控制 …...

vite vue3 ts import.meta在vscode中报错

问题描述&#xff1a;开发使用的框架为vitevue3ts&#xff0c;在开发过程中莫名其妙报仅当“--module”选项为“es2020”、“esnext”或“系统”时才允许使用“import.meta”元属性 问题解决&#xff1a; 通过更改tsconfig.json的module为esnext&#xff0c;es2022等&#xff0…...

Java synchronized(详细)

​ synchronized 一&#xff0c;介绍 在Java中&#xff0c;synchronized关键字用于解决多线程并发访问共享资源时可能出现的线程安全问题。当多个线程同时访问共享资源时&#xff0c;如果没有合适的同步机制&#xff0c;可能会导致以下问题&#xff1a; 竞态条件&#xff08…...

算法设计与分析实验报告python实现(排序算法、三壶谜题、交替放置的碟子、带锁的门)

一、 实验目的 1&#xff0e;加深学生对算法设计方法的基本思想、基本步骤、基本方法的理解与掌握&#xff1b; 2&#xff0e;提高学生利用课堂所学知识解决实际问题的能力&#xff1b; 3&#xff0e;提高学生综合应用所学知识解决实际问题的能力。 二、实验任务 1、排序算法…...

实训问题总结——ajax用get可以成功调用controller方法,用POST就出404错误

因为传输密码时必须用POST。 还有用GET传输参数&#xff0c;说有非法字符&#xff0c;想试试POST是否可以解决。 404错误的三个大致原因&#xff0c;1&#xff1a;找不到对的请求路径&#xff0c;2&#xff1a;请求方式错误&#xff0c;3、请求参数错误。 后来可以调用了。但…...

1、认识MySQL存储引擎吗?

目录 1、MySQL存储引擎有哪些&#xff1f; 2、默认的存储引擎是哪个&#xff1f; 3、InnoDB和MyISAM有什么区别吗&#xff1f; 3.1、关于事务 3.2、关于行级锁 3.3、关于外键支持 3.4、关于是否支持MVCC 3.5、关于数据安全恢复 3.6、关于索引 3.7、关于性能 4、如何…...

微信小程序媒体查询

在微信小程序中&#xff0c;media媒体查询不支持screen关键字&#xff0c;因为小程序页面是再webview中渲染的&#xff0c;而不是在浏览器中渲染的。 在设置样式时&#xff0c;可以使用 wxss 文件中的 media 规则来根据屏幕宽度或高度设置不同的样式。 device-width:设备屏幕…...

前端(动态雪景背景+动态蝴蝶)

1.CSS样式 <style>html, body, a, div, span, table, tr, td, strong, ul, ol, li, h1, h2, h3, p, input {font-weight: inherit;font-size: inherit;list-style: none;border-spacing: 0;border: 0;border-collapse: collapse;text-decoration: none;padding: 0;margi…...

软考-系统集成项目管理中级-新一代信息技术

本章历年考题分值统计 本章重点常考知识点汇总清单(掌握部分可直接理解记忆) 本章历年考题及答案解析 32、2019 年上半年第 23 题 云计算通过网络提供可动态伸缩的廉价计算能力&#xff0c;(23)不属于云计算的特点。 A.虚拟化 B.高可扩展性 C.按需服务 D.优化本地存储 【参考…...

【卷积神经网络进展】

打基础日常记录 CNN基础知识1. 感知机2. DNN 深度神经网络&#xff08;全连接神经网络&#xff09;DNN 与感知机的区别DNN特点&#xff0c;全连接神经网络DNN前向传播和反向传播 3. CNN结构【提取特征分类】4. CNN应用于文本 CNN基础知识 1. 感知机 单层感知机就是一个二分类…...

yarn的安装和使用

windows mac 环境 yarn的安装和使用 yarn安装 npm install -g yarnyarn设置代理 yarn config set registry https://registry.npm.taobao.org -gyarn官方源 yarn config set registry https://registry.yarnpkg.comyarn使用 // 查看板本 yarn --version// 安装指定包 yarn…...

Golang | Leetcode Golang题解之第10题正则表达式匹配

题目&#xff1a; 题解&#xff1a; func isMatch(s string, p string) bool {m, n : len(s), len(p)matches : func(i, j int) bool {if i 0 {return false}if p[j-1] . {return true}return s[i-1] p[j-1]}f : make([][]bool, m 1)for i : 0; i < len(f); i {f[i] m…...

Vim 调用外部命令学习笔记

Vim 外部命令集成完全指南 文章目录 Vim 外部命令集成完全指南核心概念理解命令语法解析语法对比 常用外部命令详解文本排序与去重文本筛选与搜索高级 grep 搜索技巧文本替换与编辑字符处理高级文本处理编程语言处理其他实用命令 范围操作示例指定行范围处理复合命令示例 实用技…...

CMake基础:构建流程详解

目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...

生成 Git SSH 证书

&#x1f511; 1. ​​生成 SSH 密钥对​​ 在终端&#xff08;Windows 使用 Git Bash&#xff0c;Mac/Linux 使用 Terminal&#xff09;执行命令&#xff1a; ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" ​​参数说明​​&#xff1a; -t rsa&#x…...

QT: `long long` 类型转换为 `QString` 2025.6.5

在 Qt 中&#xff0c;将 long long 类型转换为 QString 可以通过以下两种常用方法实现&#xff1a; 方法 1&#xff1a;使用 QString::number() 直接调用 QString 的静态方法 number()&#xff0c;将数值转换为字符串&#xff1a; long long value 1234567890123456789LL; …...

稳定币的深度剖析与展望

一、引言 在当今数字化浪潮席卷全球的时代&#xff0c;加密货币作为一种新兴的金融现象&#xff0c;正以前所未有的速度改变着我们对传统货币和金融体系的认知。然而&#xff0c;加密货币市场的高度波动性却成为了其广泛应用和普及的一大障碍。在这样的背景下&#xff0c;稳定…...

rnn判断string中第一次出现a的下标

# coding:utf8 import torch import torch.nn as nn import numpy as np import random import json""" 基于pytorch的网络编写 实现一个RNN网络完成多分类任务 判断字符 a 第一次出现在字符串中的位置 """class TorchModel(nn.Module):def __in…...

Linux离线(zip方式)安装docker

目录 基础信息操作系统信息docker信息 安装实例安装步骤示例 遇到的问题问题1&#xff1a;修改默认工作路径启动失败问题2 找不到对应组 基础信息 操作系统信息 OS版本&#xff1a;CentOS 7 64位 内核版本&#xff1a;3.10.0 相关命令&#xff1a; uname -rcat /etc/os-rele…...

Kafka入门-生产者

生产者 生产者发送流程&#xff1a; 延迟时间为0ms时&#xff0c;也就意味着每当有数据就会直接发送 异步发送API 异步发送和同步发送的不同在于&#xff1a;异步发送不需要等待结果&#xff0c;同步发送必须等待结果才能进行下一步发送。 普通异步发送 首先导入所需的k…...

C++.OpenGL (20/64)混合(Blending)

混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...

CSS | transition 和 transform的用处和区别

省流总结&#xff1a; transform用于变换/变形&#xff0c;transition是动画控制器 transform 用来对元素进行变形&#xff0c;常见的操作如下&#xff0c;它是立即生效的样式变形属性。 旋转 rotate(角度deg)、平移 translateX(像素px)、缩放 scale(倍数)、倾斜 skewX(角度…...