RPC
Get Started with RPC in KubeMQ
Connect Your KubeMQ Cluster
To be able to communicate with KubeMQ interface ports running in Kubernetes cluster, a Port Forward of KubeMQ's ports is needed.
kubemqctl has a handy command that will do it for you:
kubemqctl set cluster proxySubscribe to Commands Channel
A receiver can subscribe to the hello-command channel with one of the following methods.
Run the following kubemqctl command:
kubemqctl commands rec "hello-command" -aWhen connected, the stream will block until receiving a command. Once a command will be received, kubemqctl automatically will send a Response.
The following cURL command is using KubeMQ's REST interface:
curl --location --request GET "http://localhost:9090/subscribe/requests?client_id=some_client_id&channel=hello-command&subscribe_type=commands" \
--header "Content-Type: application/json" \
--data ""Once a command is received a Send Response call should be invoked:
curl --location --request POST "http://localhost:9090/send/response"
--header "Content-Type: application/json"
--data '{"RequestID": "<put here request id from command request>","ClientID":"some_client_id","ReplyChannel": "put here the reply channel value from command request","Metadata" :"some_metadata", "Body": "c29tZSBlbmNvZGVkIGJvZHk=","Executed": true,"Error":""}'Important - The reply channel address is automatically generated by the KubeMQ and can be found in the command request ReplyChannel field.
The following .NET code snippet is using KubeMQ's .NET SDK with gRPC interface:
using System;
namespace RPC_Subscribe_to_a_Channel
{
class Program
{
static void Main(string[] args)
{
var ChannelName = "hello-command";
var ClientID = "hello-world-subscriber";
var KubeMQServerAddress = "localhost:50000";
KubeMQ.SDK.csharp.CommandQuery.Responder responder = new KubeMQ.SDK.csharp.CommandQuery.Responder(KubeMQServerAddress);
try
{
responder.SubscribeToRequests(new KubeMQ.SDK.csharp.Subscription.SubscribeRequest()
{
Channel = ChannelName,
SubscribeType = KubeMQ.SDK.csharp.Subscription.SubscribeType.Commands,
ClientID = ClientID
}, (commandReceive) => {
Console.WriteLine($"Command Received: Id:{commandReceive.RequestID} Channel:{commandReceive.Channel} Metadata:{commandReceive.Metadata} Body:{ KubeMQ.SDK.csharp.Tools.Converter.FromByteArray(commandReceive.Body)} ");
return new KubeMQ.SDK.csharp.CommandQuery.Response(commandReceive)
{
Body = new byte[0],
CacheHit = false,
Error = "None",
ClientID = ClientID,
Executed = true,
Metadata = string.Empty,
Timestamp = DateTime.UtcNow,
};
}, (errorHandler) =>
{
Console.WriteLine(errorHandler.Message);
});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("press any key to close RPC_Subscribe_to_a_Channel");
Console.ReadLine();
}
}
}When executed, a stream of events messages will be shown in the console.
The following Java code snippet is using KubeMQ's Java SDK with gRPC interface:
When executed, a stream of events messages will be shown in the console.
The following Go code snippet is using KubeMQ's Go SDK with gRPC interface:
When connected, once a command will be received in the channel, we create a Response and send back to the sender.
The following Python code snippet is using KubeMQ's Python SDK with gRPC interface:
When executed, a stream of events messages will be shown in the console.
The following JS code snippet is using KubeMQ's NodeJS SDK with gRPC interface:
The following PHP code snippet is using KubeMQ's REST interface:
Once a command is received a Send Response call should be invoked:
Important - The reply channel address is automatically generated by the KubeMQ and can be found in the command request ReplyChannel field.
The following Ruby code snippet is using KubeMQ's REST interface:
Once a command is received a Send Response call should be invoked:
Important - The reply channel address is automatically generated by the KubeMQ and can be found in the command request ReplyChannel field.
The following jQuery code snippet is using KubeMQ's REST interface:
Once a command is received a Send Response call should be invoked:
Important - the reply channel address is automatically generated by the KubeMQ and can be found in the command request ReplyChannel field.
Send to Commands Channel
After you have subscribed to a hello-command channel, you can send your command to it.
Run the following kubemqctl command:
kubemqctl commands send "hello-command" "some command"The following cURL command is using KubeMQ's REST interface:
curl --location --request POST "http://localhost:9090/send/request"
--header "Content-Type: application/json"
--data '{"RequestID": "688daec3-7f3e-4766-87fa-4cd1f4f03a23","RequestTypeData":1, "ClientID": "some_clientID","Channel": "hello-command","Metadata" :"some_metadata","Body": "c29tZSBlbmNvZGVkIGJvZHk=","Timeout": 10000}'The following .NET code snippet is using KubeMQ's .NET SDK with gRPC interface:
using System;
namespace RPC_Send_a_Command_Channel
{
class Program
{
static void Main(string[] args)
{
var ChannelName = "hello-command";
var ClientID = "hello-world-sender";
var KubeMQServerAddress = "localhost:50000";
var channel = new KubeMQ.SDK.csharp.CommandQuery.Channel(new KubeMQ.SDK.csharp.CommandQuery.ChannelParameters
{
RequestsType = KubeMQ.SDK.csharp.CommandQuery.RequestType.Command,
Timeout = 10000,
ChannelName = ChannelName,
ClientID = ClientID,
KubeMQAddress = KubeMQServerAddress
});
try
{
var result = channel.SendRequest(new KubeMQ.SDK.csharp.CommandQuery.Request
{
Body = KubeMQ.SDK.csharp.Tools.Converter.ToByteArray("hello kubemq - sending a command, please reply")
});
if (!result.Executed)
{
Console.WriteLine($"Response error:{result.Error}");
return;
}
Console.WriteLine($"Response Received:{result.RequestID} ExecutedAt:{result.Timestamp}");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}The following Java code snippet is using KubeMQ's Java SDK with gRPC interface:
The following Go code snippet is using KubeMQ's Go SDK with gRPC interface:
The following Python code snippet is using KubeMQ's Python SDK with gRPC interface:
The following JS code snippet is using KubeMQ's NodeJS SDK with gRPC interface:
The following PHP code snippet is using KubeMQ's REST interface:
The following Ruby code snippet is using KubeMQ's REST interface:
The following jQuery code snippet is using KubeMQ's REST interface:
Demo
Last updated
Was this helpful?