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
相关推荐
码力斜杠哥1 天前
Rust初习录(6)Rust的 if 玩法
开发语言·python·rust
Rust研习社1 天前
Rust 的 move 语义,一次讲透
后端·rust·编程语言
WMYeah1 天前
【无标题】
前端·rust·抽奖程序·跨平台抽奖程序
不开大的凯20772 天前
超级更新月的“硬菜“:模型、硬件与应用全面进入爆发期
人工智能·dubbo·文心一言
楼兰公子2 天前
buildroot 在编译rust时裁剪平台类型数量的方法
开发语言·后端·rust
Rust研习社2 天前
开源项目里的 deny.toml 是什么?
后端·rust·编程语言
铭毅天下2 天前
当搜索引擎遇上 Rust——深度解读下一代实时搜索引擎 INFINI Pizza
开发语言·后端·搜索引擎·rust
咸甜适中2 天前
rust语言学习笔记Trait之Default(默认值)
笔记·学习·rust
容智信息3 天前
AI Agent(智能体)的输出格式应该从 Markdown 转向 HTML吗?
前端·人工智能·rust·编辑器·html·prompt