-
Notifications
You must be signed in to change notification settings - Fork 0
RPC Protocol
Request/reply, often called RPC, is a popular pattern to implement with a messaging broker like RabbitMQ. One of the popular use cases is a microservices based architecture where one service requests data from another service.
A complete documentation is available here.
Note that RabbitMQ version 4.0 is the minimal version that supports the RPC protocol.
On following example, IRpcServerService and IRpcCleintService will be used. No extension method for the IServiceCollection is available so it will be required to register them at startup.
var rpcServerService = serviceProvider.GetRequiredService<IRpcServerService>();
var disposable = await this.rpcServerService.ListenForRequestAsync<int, string>("Primary", "rpc_request", OnReceiveAsync);
....
private static Task<string> OnReceiveAsync(int arg)
{
return Task.FromResult(arg.ToString(CultureInfo.InvariantCulture));
}var rpcServerService = serviceProvider.GetRequiredService<IRpcClientService<string>>();
var observable = await rpcClientService.SendRequestAsync("Primary", "rpc_request", 41);
observable.Subscribe(response => Console.WriteLine(response));Results:
When the Micro Service B pushes the message, the RPC server will apply a "ToString()" operation on the received int value and response to the client with the string representation. A new line will be written in the Console of Micro Service B with the content as "41".
copyright @ Starion Group S.A.