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

网站建设主机耗电量义乌公司网站制作

网站建设主机耗电量,义乌公司网站制作,德清网站建设,360手机优化大师安卓版十八、Rust gRPC 多 proto 演示 网上及各官方资料#xff0c;基本是一个 proto 文件#xff0c;而实际项目#xff0c;大多是有层级结构的多 proto 文件形式#xff0c;本篇文章 基于此诉求#xff0c;构建一个使用多 proto 文件的 rust grpc 使用示例。 关于 grpc 的实现…十八、Rust gRPC 多 proto 演示 网上及各官方资料基本是一个 proto 文件而实际项目大多是有层级结构的多 proto 文件形式本篇文章 基于此诉求构建一个使用多 proto 文件的 rust grpc 使用示例。 关于 grpc 的实现找到两个库 Tonichttps://github.com/hyperium/tonic8.9k Star、852 Commits、2024-03-12 updated。 PingCAP 的 grpc-rshttps://github.com/tikv/grpc-rs1.8k Star、357 Commits、2023-08 updated。 据说 PingCAP 的 grpc-rs benchmark 稍高一些但看关注度和提交量不如 tonic且据说 tonic 开发体验更好一些本篇以 tonic 为例。 编译 Protobuf还需要 protoc可以参考官方文档这里先给出 macOS 的 brew install protobufhttps://grpc.io/docs/protoc-installation/ 关于 TonicTonic 是基于 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.rsbuild.rs 存放通过 proto 生成 rs 的脚本proto 目录放置 grpc 的 proto 文件定义服务和消息体src 常规意义上的项目源码目录 proto-gen 目录存放 build.rs 编译 proto 后生成的 rs 文件lib.rs 引入 proto 的 rs 文件bin 目录下进行 proto 所定义服务的实现此例为 客户端、服务端 的实现 创建项目 创建一个 lib 项目 cargo new grpc --libCargo.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]] namegrpc_server pathsrc/bin/server.rs[[bin]] namegrpc_client pathsrc/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.0grpc/build.rs use std::error::Error; use std::fs;static OUT_DIR: str src/proto-gen;fn main() - Result(), Boxdyn 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类型implT HelloClientT 实现了Clone、Sync 及 Send因此可以跨线程使用。为服务端生成的 HelloServer 类型implT: Hello HelloServerT {} 包含了 implT: 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: RequestHelloRequest) - ResultResponseHelloResponse, 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: RequestGoodbyeRequest,) - ResultResponseGoodbyeResponse, 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(), Boxdyn 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(), Boxdyn 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
http://www.eeditor.cn/news/122683/

相关文章:

  • 网站建设需要哪些硬件WordPress自动readmore
  • 百度给做网站吗如何做好网页设计
  • 做太空泥的几分钟网站怎么看百度指数
  • 定制型网站制作酒店预订网站建设
  • 西安企业建站费用做网站虚拟主机
  • 网络营销就是建立企业网站沈阳百度广告
  • 鞍钢节能公司网站开发建立网站内容
  • 网站建设竞品分析自己怎么做dj 视频网站
  • 郑州网站建设 易云互联网页设置
  • 中山市交通建设发展集团网站成都建设网站高级工程师查询
  • 外管局网站 报告怎么做网站怎么做下载网页代码吗
  • 网站建设流程及细节上海著名网站建设
  • 网站建设提供的网站资料wordpress的文要登录才能看
  • 企业网站建设实训心得长沙自动seo
  • 旅游网站排行榜前十名官网销售类网站模板
  • 做网站 傻瓜软件怎么做网店运营
  • 站长之家官网官方网站建设需要做哪些东西
  • 怎样自己做公司网站太原网站设计开发公司
  • 网站建设页面美工网页制作工作
  • 保定网站seowordpress html生成app
  • 免费网站建设垂询186 6159 6345免费咨询律师在线解答
  • 全世界做会展介绍的网站排名python报班一般多少钱
  • 网站建设和管理中 经验wordpress做账号登录
  • 织梦网站修改数据库表数据品牌网站建设方
  • 建设网站的网站安全宁波建设工程造价信息网地址
  • 换友网站给一个企业做网站
  • 手机版的学习网站网络服务商电话
  • 做公司网站需要了解哪些东西网站建设ppt方案
  • 手机搭建网站教程视频淘宝客网站源码和模版有什么区别
  • 谷歌网站收录提交网站建设技术的发展