Rust使用gRPC

需要先安装protoc (Protocol Buffers Compiler),可据此Protobuf Compiler Installation下载

第一步:创建项目

  1. 创建两个新的Rust项目,分别作为服务端与客户端:

    复制代码
    cargo new rust_grpc_server
    
    cargo new rust_grpc_client
  2. 分别在项目根目录创建proto文件夹,并在其中创建一个叫hello.proto的文件

第二步:编写.proto文件

proto/hello.proto文件中分别写入以下内容:

复制代码
syntax = "proto3";

package hello;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings.
message HelloReply {
  string message = 1;
}

第三步:添加依赖

在server的项目的Cargo.toml文件中添加以下依赖:

复制代码
[dependencies]
tonic = "0.6"
tokio = { version = "1", features = ["full"] }
prost = "0.9"

[build-dependencies]
tonic-build = "0.6"

在client的项目的Cargo.toml文件中添加以下依赖:

复制代码
[dependencies]
tonic = "0.6"
tokio = { version = "1", features = ["full"] }
prost = "0.9"
rand = "0.8"

[build-dependencies]
tonic-build = "0.6"

第四步:创建build脚本

分别在根目录创建一个build.rs文件,添加以下代码, 根据.proto文件生成相应的Rust代码:

复制代码
fn main() {
    tonic_build::compile_protos("proto/hello.proto")
        .expect("Failed to compile proto files");
}

最终生成的代码类似

第五步:编写gRPC服务器

在server项目的src/main.rs中,创建一个gRPC服务器:

复制代码
use std::time::SystemTime;
use tonic::{transport::Server, Request, Response, Status};

pub mod hello {
    tonic::include_proto!("hello");
}

use hello::greeter_server::{Greeter, GreeterServer};
use hello::{HelloReply, HelloRequest};

#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
    async fn say_hello(
        &self,
        request: Request<HelloRequest>,
    ) -> Result<Response<HelloReply>, Status> {
        let reply = hello::HelloReply {
            message: format!(
                "Hello {}!,Current Time is {:?}",
                request.into_inner().name,
                SystemTime::now()
            ),
        };

        Ok(Response::new(reply))
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "[::1]:50051".parse()?;
    let greeter = MyGreeter::default();

    println!("GreeterServer listening on {}", addr);

    Server::builder()
        .add_service(GreeterServer::new(greeter))
        .serve(addr)
        .await?;

    Ok(())
}

第六步:编写gRPC客户端

在client项目的src/main.rs文件中,添加一个客户端来测试服务器:

复制代码
use rand::Rng;

pub mod hello {
    tonic::include_proto!("hello");
}

use hello::HelloRequest;

#[derive(Default)]
pub struct MyGreeter {}

// 客户端代码
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    // 客户端测试
    let mut client = hello::greeter_client::GreeterClient::connect("http://[::1]:50051").await?;

    // 随机选择一个字符串出来
    let names = ["张三", "李四", "王五"];

    let mut rng = rand::thread_rng();

    let random_name = names[rng.gen_range(0..names.len())];

    let request = tonic::Request::new(HelloRequest {
        name: random_name.into(),
    });

    let response = client.say_hello(request).await?;

    println!("RESPONSE={:?}", response.into_inner().message);

    Ok(())
}

编译和运行

  1. 在server项目根目录执行 cargo run来编译和运行项目,服务器将启动并监听在 [::1]:50051
  1. 在client项目根目录执行 cargo run来编译和运行项目,客户端将发送一个请求并打印出服务端的响应内容

本文由mdnice多平台发布

相关推荐
金銀銅鐵41 分钟前
[Java] 如何理解 class 文件中字段的 access flags?
java·后端
不懒不懒1 小时前
基于 Flask —— 异步任务处理接口服务
后端·python·flask
Xidaoapi1 小时前
Python FastAPI性能优化实战:8个让你的API快3倍的技巧
后端·程序员
William Dawson1 小时前
【通俗易懂!Spring四大核心注解源码解读:@Configuration、@ComponentScan、@Import、@EnableXXX实战】
java·后端·spring
倚栏听风雨2 小时前
Mac 本地开发:用 Nginx 配置自定义域名代理到本地服务
后端
fliter2 小时前
在 Rust 异步接口的丛林中生存:从同步 I/O 到手写异步状态机
后端
菜菜小狗的学习笔记2 小时前
八股(九)杂七杂八
java·后端·spring
逍遥德2 小时前
Java编程高频的“技术点”-01:自定义全局异常处理器
java·开发语言·spring boot·后端
小旭95273 小时前
商品详情实现与缓存问题(穿透、击穿、雪崩)解决方案
java·数据库·spring boot·后端·缓存
迷渡4 小时前
用 Rust 重写的 Bun 有 13365 个 unsafe!
开发语言·后端·rust