From d051f6526c78f96ba494ab393d8c770d3b8608c6 Mon Sep 17 00:00:00 2001 From: Eduardo Pinto Date: Tue, 9 Jan 2024 11:26:13 +0000 Subject: [PATCH] docs: how to inject component client --- .../java/pages/component-and-service-calls.adoc | 17 +++++++++++++++-- .../fibonacci/LimitedFibonacciAction.java | 15 ++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/docs/src/modules/java/pages/component-and-service-calls.adoc b/docs/src/modules/java/pages/component-and-service-calls.adoc index 33e2a40973..5d8d9c4391 100644 --- a/docs/src/modules/java/pages/component-and-service-calls.adoc +++ b/docs/src/modules/java/pages/component-and-service-calls.adoc @@ -1,13 +1,26 @@ = Component and Service Calls :page-aliases: spring:call-another-service.adoc, java:call-another-service.adoc +Typically, a Kalix service comprises many components. Such components might dependent on one another, on other Kalix services or even external services. This section describes how to call other components and services from within a Kalix service. + == Kalix components Since Kalix is an auto-scaling solution, components can be distributed across many nodes within the same service. Kalix doesn’t hide this fact and makes it explicit. That's why calling Kalix components is done via HTTP/gRPC `DeferredCall` calls. A `DeferredCall` is just an instruction on how to call a given component, which can be optimized by the Kalix engine (see xref:java:actions.adoc#_forwarding_commands[forwarding]). Sometimes it's necessary to transform the `DeferredCall` into the `CompletionStage` to combine many different component calls and build a single xref:java:actions.adoc#_composing_calls[asynchronous reply]. === Component Client -The Kalix `ComponentClient` is a utility for creating deferred calls in a type-safe way. You don't have to remember what the endpoint path is or which HTTP method should be selected for the call. Constructing the call is a matter of: +The Kalix `ComponentClient` is a utility for creating deferred calls in a type-safe way. You don't have to remember what the endpoint path is or which HTTP method should be selected for the call. To use the `ComponentClient` you need to inject it into your component (an Action in this example): + +[source,java,indent=0] +.src/main/java/com/example/fibonacci/LimitedFibonacciAction.java +---- +include::java:example$java-spring-fibonacci-action/src/main/java/com/example/fibonacci/LimitedFibonacciAction.java[tag=injecting-component-client] +---- +<1> Declare a field for the `ComponentClient`. +<2> Have a constructor that accepts the `ComponentClient` as an argument. +<3> Assign the `ComponentClient` to the field. + +With the `componentClient` available on your component, you can use it to create a `DeferredCall`. Constructing the call is a matter of: * selecting the component type, * choosing the endpoint, with a Java method reference, @@ -28,7 +41,7 @@ NOTE: Calling endpoints that return a stream response like `Flux` is not support Calling other Kalix services in the same project from an Action is done by invoking them using a https://docs.spring.io/spring-framework/docs/5.0.13.RELEASE/spring-framework-reference/web-reactive.html#webflux-client[`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 xref:value-entity.adoc[Value Entity Counter] service, deployed with the service name "counter." +In this sample we will make an action that does a call to the xref:value-entity.adoc[Value Entity Counter] service, deployed with the service name `counter`. The Kalix Java SDK provides a utility class `WebClientProvider` that can provide previously configured `WebClient`s to reach other Kalix services deployed on the same Kalix project. diff --git a/samples/java-spring-fibonacci-action/src/main/java/com/example/fibonacci/LimitedFibonacciAction.java b/samples/java-spring-fibonacci-action/src/main/java/com/example/fibonacci/LimitedFibonacciAction.java index cd14bc5d58..61e2f3d1aa 100644 --- a/samples/java-spring-fibonacci-action/src/main/java/com/example/fibonacci/LimitedFibonacciAction.java +++ b/samples/java-spring-fibonacci-action/src/main/java/com/example/fibonacci/LimitedFibonacciAction.java @@ -14,16 +14,13 @@ public class LimitedFibonacciAction extends Action { private static final Logger logger = LoggerFactory.getLogger(LimitedFibonacciAction.class); + // tag::injecting-component-client[] + private ComponentClient componentClient; // <1> - private ComponentClient componentClient; - - private ActionCreationContext ctx; - - - public LimitedFibonacciAction(ActionCreationContext ctx, ComponentClient componentClient) { - this.ctx = ctx; - this.componentClient = componentClient; + public LimitedFibonacciAction(ComponentClient componentClient) { // <2> + this.componentClient = componentClient; // <3> } + // end::injecting-component-client[] @GetMapping("/{number}/next") public Effect nextNumberPath(@PathVariable Long number) { @@ -34,7 +31,7 @@ public Effect nextNumberPath(@PathVariable Long number) { // tag::component-client[] DeferredCall deferredCall = componentClient.forAction() // <1> .call(FibonacciAction::getNumber) // <2> - .params(number);// <3> + .params(number); // <3> return effects().forward(deferredCall); // end::component-client[]