A Load Balancer for distributing incoming requests to a list of registered providers. Load Balancer can be configured with the following policies:
Invocation Policies:
- RandomInvocationPolicy: The Provider that is going to process the message is being selected randomly.
- RoundRobinInvocationPolicy: The Provider that is going to process the message is being selected via round-robin algorithm.
Heartbeat Policies:
- DefaultHeartbeatPolicy: Unhealthy Providers are removed from the list.
- ImprovedHeartbeatPolicy: Unhealthy Providers are removed from the list, after receiving N successful heartbeat, Provider is being added to list.
Implemented using Java 8. You can compile it via:
mvnw clean package -DskipTests
or if maven installed:
mvn clean package -DskipTests
REMARK! Unit tests include multithreaded testcases. If you enable tests, it may take a bit more time in packaging the application.
...
//instantiate the load balancer
LoadBalancer loadBalancer = new LoadBalancer.Builder()
.withInvocationPolicy(new RoundRobinInvocationPolicy())
.withHeartbeatPolicy(new ImprovedHeartbeatPolicy())
.withClusterCapacity(4)
.withHeartbeatFrequency(10)
.build();
//with no Provider
List<Provider> providerList = new ArrayList<>();
loadBalancer.register(providerList);
//Make request to the loadbalancer,since there is no provider to process the request, a null respose will be returned.
String response = loadBalancer.get();
...
loadbalancer.shutdown();