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. When 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

Calling the Greeting Service (Request / Response):

  ServiceMessage greetingRequest = ServiceMessage.builder()
          .qualifier("my.greeting.service", "greeting")
          .data("joe")
          .build();

  Publisher<ServiceMessage> publisher = microservices.call()
		.create()
		.requestOne(greetingRequest, String.class);
  
  final long TIMEOUT = 3;
  ServiceMessage result = Mono.from(publisher).block(
      Duration.ofSeconds(TIMEOUT)).data();
  System.out.println(result.data());

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

Listen and subscribe the Greeting Service (Reactive Stream):


  ServiceMessage serviceMessage = ServiceMessage.builder()
          .qualifier("my.greeting.service", "greeting")
          .data("joe")
          .build();
   
  Flux.from(service.requestMany(serviceMessage))
        .subscribe(onNext -> /* service stream responses */);
}