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
相关推荐
ftpeak7 小时前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app
咸甜适中7 小时前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
咸甜适中12 小时前
rust语言 (1.88) egui (0.32.2) 学习笔记(逐行注释)(二十八)使用图片控件显示图片
笔记·学习·rust·egui
huli332015 小时前
pingora_web:首款基于 Cloudflare Pingora 的企业级 Rust Web 框架
rust
Pomelo_刘金16 小时前
如何优雅地抽离 Rust 子工程:以 rumqttd 为例
rust
几颗流星17 小时前
Rust 常用语法速记 - 错误处理
后端·rust
向上的车轮1 天前
如何用 Rust 重写 SQLite 数据库(二):是否有市场空间?
数据库·rust·sqlite
烈风1 天前
004 Rust控制台打印输出
开发语言·后端·rust
a7360152 天前
二十二、包管理与发布 (Cargo 进阶)
开发语言·rust
编码浪子2 天前
趣味学RUST基础篇(异步补充)
开发语言·后端·rust