Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: how to inject component client #1953

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions docs/src/modules/java/pages/component-and-service-calls.adoc
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Number> nextNumberPath(@PathVariable Long number) {
Expand All @@ -34,7 +31,7 @@ public Effect<Number> nextNumberPath(@PathVariable Long number) {
// tag::component-client[]
DeferredCall<Any, Number> deferredCall = componentClient.forAction() // <1>
.call(FibonacciAction::getNumber) // <2>
.params(number);// <3>
.params(number); // <3>

return effects().forward(deferredCall);
// end::component-client[]
Expand Down