google / tarpc

An RPC framework for Rust with a focus on ease of use.
MIT License
3.17k stars 194 forks source link

How to implement router? #462

Open mzdk100 opened 2 months ago

mzdk100 commented 2 months ago

I need to define different services to handle different tasks, such as:

#[tarpc::service]
pub trait UserService {
    async fn login(name: String);
}

#[tarpc::service]
pub trait OrderService {
    async fn query(name: String);
}

The current problem is how to register these two services into the framework, and then create clients using 'UserServiceClient::new' and 'OrderServiceClient::new' and call them?

tikue commented 1 month ago

Thanks for the question! Tarpc doesn't natively support registering multiple services on a single server. I can suggest a few options:

mzdk100 commented 1 month ago

The "multiplexing transport" solution is good, but it requires writing a lot of code. Can't the tarpc framework handle these things itself? The microservice framework should be designed to be modular so that we can organize the code more conveniently.

tikue commented 1 month ago

The main reason tarpc can't easily support native multiplexing is because the transport is typically generic over the request/response type.

Could the multiplexing code be written generically as a library? Then it wouldn't have to be duplicated by each user. If it were proven popular enough, perhaps it could be merged into tarpc.

mzdk100 commented 1 month ago

Currently, I'm using the quic-rpc library which can meet my need for modularizing the code, but writing code is not as concise as using the tarpc framework.