To run the examples, you need to clone DataKernel from GitHub:
$ git clone https://github.com/softindex/datakernel And import it as a Maven project. Check out branch v3.0. Before running the example, build the project.
The example Simple RPC Example is located at datakernel -> examples -> cloud -> rpc.
The RPC strategies examples are located at datakernel -> cloud-rpc -> src -> test -> RpcStrategiesTest.
Simple RPC Example
In the “Hello World” client and server RPC Example, the client sends a request which contains the word “World” to server. When
server receives it, it sends a response that contains the word “Hello “. If everything completes successfully, we get the
Let’s have a look at the implementation:
RpcExample class extends Launcher to help us manage application lifecycle.
We need to provide RpcServer and RpcClient with relevant configurations and required dependencies using
DataKernel DI. RpcClient sends requests to the specified server according to the provided RpcStrategies
(getting a single RPC-service).
For RpcServer we define the type of messages to proceed, a corresponding RpcRequestHandler and a listener port.
Since we extend Launcher, we will also override 2 methods: getModule to provide ServiceGraphModule
and run to represents the main logic of the example.
Finally, we define the main method, which will launch our example.
RPC module contains pre-defined strategies for requests arrangement between RPC servers or shards of servers.
Round-Robin is one of the simplest of strategies: it just goes through the servers or shards in a cyclic way one by
So, in this example we create an RPC pool with 5 equal connections and set roundRobin strategy for them. Next, we
create a sender for the pool with the previously defined strategy. And that’s it, 100 requests will be equally
distributed between the servers:
Round-Robin and First Available Strategies Combined
You can simply combine RPC strategies. In this example we will combine roundRobin and firstAvailable strategies.
First, we create 4 connections without putting connection3 into the pool. Then we start sending 20 requests.
As a result, all the requests will be equally distributed between connection1 (as it is always firstAvailable) and
connection4 (as connection3 isn’t available for the pool):
Sharding and First Valid Strategies Combined
You can also create your own sharding functions and combine them with other strategies if needed. In this example we
create 5 equal connections but don’t put connection2 into the pool. Next, we provide a simple sharding function which
distributes requests between shards in accordance to the content of the request.
We split the connections into two shards,
and set firstValidResult strategy for both of them. This strategy sends request to all available servers.
Now, we manually send 7 requests:
4 with 0 message, so they’ll be sent to the first shard’s connection1
3 with 1, so they’ll all be sent to all three connections of the second shard
Rendezvous Hashing Strategy
Rendezvous hashing strategy pre-calculates the hash function for the RpcSender and creates a map of RPC servers.
The map is stored in cache and will be re-calculated only if servers go online/offline.
In this example requests will be equally distributed between connection1, connection2, connection3:
When we remove some of the connections from the pool, hash function is recalculated:
Type Dispatch Strategy
This strategy simply distributes requests among shards in accordance to the type of the request. In the example
all String requests are sent on the first shard which has firstValidResult strategy for the servers. Request
with all other types are sent to the second shard with firstAvailable strategy. As a result, connection1 and
connection2 will process 35 requests, connection3 - 25 requests, while connection4 and connection5 - 0 requests
as connection3 was always firstAvailable: