View on GitHub

ScaleCube Reactive Microservices

The Future is reactive - The Future is right here!

Using Service Dispatchers

Another possible method to make a service call is using dispatchers, dispatchers are useful in case we want to address any service by name, this method may come handy in case we want to dispatch several services for example lets consider a transparent API-Gateway that routes and dispatch service requests to the microservices cluster.

Dispatchers VS Service Proxy:

Service Proxy: uses a java interface and restrictive type safe approach when making a service call. using a service proxy there is almost no boiler plate code and the business logic is clean and decoupled by a pure java interface.

Dispatchers: service dispatchers uses Message communication using message headers to address a service. dispatchers have the exact same behavior as service proxy without the requirement to have the dependency of any specific java interface

Creating a service dispatcher:

ServiceCall service = microservices.dispatcher().create();

Calling the Greeting Service:

service.invoke(ServiceCall
          .request("my.greeting.service", "greeting")
          .data(Message.builder().data("joe").build())
          .build()).whenComplete((success, error) -> {
            // service response success/error.
           });
}

Since Service Dispatchers are not coupled with a java interface they have one more benefit of using timeout per a service request where proxy have only a default timeout that apply for all service requests made by the proxy.

service.invoke(ServiceCall
    .request("my.greeting.service", "greeting")
    .data(Message.builder().data("joe").build())
    .build(), Duration.ofSeconds(1))
      .whenComplete((success, error) -> {
      // service response success/error.
      });