krpc-rust v0.2.0 发布,兼容Dubbo协议,支持服务注册与发现

github.com/kwsc98/krpc...

krpc-rust 一个最像RPC框架的Rust-RPC框架

krpc-rust是一个高性能,轻量级的rpc框架,通过使用Rust宏来解决目前主流rpc框架使用复杂,性能低等问题,不需要通过脚本和脚手架生成rpc调用代码,通过宏来进行编译期"反射"来实现高性能的调用,来满足rpc调用的简易性,同时支持Dubbo3服务的注册发现和互相调用;

快速开始

Server

rust 复制代码
#[derive(Serialize, Deserialize, Default, Debug)]
struct ReqDto {
    name: String,
}

#[derive(Serialize, Deserialize, Default, Debug)]
struct ResDto {
    res: String,
}

#[derive(Clone)]
struct DemoService {
    _db: String,
}

krpc_server! {
   //设置包路径
   "org.apache.dubbo.springboot.demo",
   //设置service-name
   DemoService,
   //设置service-versions
   None,
   async fn sayHello(&self,req : String) -> Result<String> {
      info!("res : {:?}" ,req);
      return Ok("Hello ".to_owned() + &req);
   }
   async fn sayHelloV2(&self,req : ReqDto) -> Result<ResDto> {
      info!("res : {:?}" ,req);
      return Ok(ResDto{res :  "Hello ".to_owned() + &req.name + " V2"});
   }
}

#[tokio::main(worker_threads = 512)]
async fn main() {
    krpc_common::init_log();
    let server: DemoService = DemoService {
        _db: "我是一个DB数据库".to_string(),
    };
    KrpcServer::build(
        //配置注册中心
        RegisterBuilder::new(
            &format!("127.0.0.1:{}", "2181"),
            "default",
            RegisterType::ZooKeeper,
        ),
        //设置监听端口
        "8081",
    )
    .add_rpc_server(Box::new(server))
    .run()
    .await;
}

Client

rust 复制代码
//初始化KrpcClient
lazy_static! {
    static ref CLI: KrpcClient = KrpcClient::build(
        //配置注册中心
        RegisterBuilder::new(
            &format!("127.0.0.1:{}", "2181"),
            "default",
            RegisterType::ZooKeeper,
        )
    );
}

#[derive(Serialize, Deserialize, Default, Debug)]
struct ReqDto {
    name: String,
}

#[derive(Serialize, Deserialize, Default, Debug)]
struct ResDto {
    res : String,
}

struct DemoService;
//声明Rpc接口
krpc_client! {
   CLI,
   //设置API包路径
   "org.apache.dubbo.springboot.demo",
   //设置service-name
   DemoService,
   //设置service-versions
   None,
   async fn sayHello(&self,req : String) -> Result<String>
   async fn sayHelloV2(&self,req : ReqDto) -> Result<ResDto>
} 

#[tokio::main(worker_threads = 512)]
async fn main() {
    krpc_common::init_log();
    let client = DemoService;
    let res = client.sayHello("world".to_string()).await;
    info!("{:?}",res);
    let res = client.sayHelloV2(ReqDto{name:"world".to_string()}).await;
    info!("{:?}",res);
}

Dubbo3

本项目同时兼容dubbo3协议,可以很方便的与Java版本的Dubbo3项目通过接口暴露的方式进行服务注册发现和互调。

Rust的Server和Client完全不用改造就如上示例即可。

Java版本的Dubbo3项目,代码层面不需要改造,只需要添加一些依赖和配置(因Dubbo3使用接口暴露的方式默认不支持json序列化协议,而是采用fastjson2的二进制序列化格式,所以这里我们需手动添加fastjson1的支持)

这里我们使用duboo3的官方示例dubbo-samples-spring-boot项目进行演示 github.com/apache/dubb...

首先我们需要把Server和Client的服务的pom.xml都添加fastjson1的maven依赖

java 复制代码
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-serialization-fastjson</artifactId>
    <version>2.7.23</version>
</dependency>

Java-Server

java 复制代码
@DubboService
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

Server-application.yml

java 复制代码
dubbo:
  application:
    name: dubbo-springboot-demo-provider
  protocol:
    name: tri
    port: 50052
    //添加fastjson的支持
    prefer-serialization: fastjson
  registry:
    address: zookeeper://${zookeeper.address:127.0.0.1}:2181

Java-Client

java 复制代码
@Component
public class Task implements CommandLineRunner {
    @DubboReference
    private DemoService demoService;

    @Override
    public void run(String... args) throws Exception {
        String result = demoService.sayHello("world");
        System.out.println("Receive result ======> " + result);

        new Thread(()-> {
            while (true) {
                try {
                    Thread.sleep(1000);
                    System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }
        }).start();
    }
}

Client-application.yml

java 复制代码
dubbo:
  application:
    name: dubbo-springboot-demo-consumer
  registry:
    address: zookeeper://${zookeeper.address:127.0.0.1}:2181
相关推荐
AI智动派1 天前
从 Python 到 Rust:深入解析 LLM Agent 工具调用的内存安全与异步并发重构实践
rust
_朱志鹏1 天前
Rust练手项目1--minigrep
rust
ssshooter2 天前
Tauri 项目实践:客户端与 Web 端的授权登录实现方案
前端·后端·rust
AI智动派2 天前
《深入 Rust Async/Await:如何实现一个带超时保护与安全沙箱的 LLM Agent 循环》
rust
范特西林4 天前
一次 to_bits() 引发的 Rust 与 C++ 底层思考
rust
冬奇Lab5 天前
一天一个开源项目(第42篇):OpenFang - 用 Rust 构建的 Agent 操作系统,16 层安全与 7 个自主 Hands
人工智能·rust·开源
量子位5 天前
Transformer论文作者重造龙虾,Rust搓出钢铁版,告别OpenClaw裸奔漏洞
rust·openai·ai编程
哈里谢顿5 天前
Rust 语言入门博客
rust
DongLi017 天前
rustlings 学习笔记 -- exercises/06_move_semantics
rust
clamlss7 天前
💥 踩坑实录:Dubbo 为什么把我的自定义异常“吃”了?
dubbo