Calling other services

Kalix services

Calling other Kalix services in the same project from an Action is done by invoking them using a Spring WebFlux WebClient. The service is identified by the name it has been deployed. Kalix takes care of routing requests to the service and keeping the data safe by encrypting the connection for you.

In this sample we will make an action that does a call to the Value Entity Counter service, deployed with the service name "counter."

The Kalix Spring SDK provides a utility class WebClientProvider that can provide previously configured `WebClient`s to reach other Kalix services deployed on the same Kalix project.

In our delegating service implementation:

src/main/java/com/example/DelegatingServiceAction.java
public class DelegatingServiceAction extends Action {

  final private WebClient webClient;

  public DelegatingServiceAction(WebClientProvider webClientProvider) { (1)
    this.webClient = webClientProvider.webClientFor("counter"); (2)
  }

  @PostMapping("/delegate/counter/{counter_id}/increase")
  public Effect<Number> addAndReturn(@PathVariable String counterId, @RequestBody Number increaseBy) {
    var result =
        webClient
            .post().uri("/counter/" + counterId + "/increase") (3)
            .bodyValue(increaseBy)
            .retrieve()
            .bodyToMono(Number.class).toFuture();

    return effects().asyncReply(result);  (4)
  }
}
1 Let the WebClientProvider be injected into the Action with constructor injection.
2 Use the WebClientProvider to build a WebClient for the counter service.
3 Use the WebClient to make a REST call to the counter service.
4 Use the remote call result to create a reply.

External services

Calling Kalix services deployed on different projects or any other external service, is done by configuring specific WebClients.

See the Spring WebFlux WebClient documentation for details on configuring the WebClients.