Quarkus - Dapr
Introduction
What is Dapr?
Dapr is a portable, event-driven runtime that makes it easy for any developer to build resilient, stateless and stateful applications that run on the cloud and edge and embraces the diversity of languages and developer frameworks.
Leveraging the benefits of a sidecar architecture, Dapr helps you tackle the challenges that come with building microservices and keeps your code platform agnostic.
For more information about Dapr, please go https://dapr.io/.
What is Quarkus-Dapr?
Quarkus Dapr is a Quarkus extension to integrate with Dapr.
Quarkus Dapr Extension enables Java developers to create ultra lightweight Java native applications for Function Computing and FaaS scenes, which is also particularly suitable for running as serverless.
With the help of Dapr, these ultra lightweight Java native applications can easily interact with external application and resources. Dapr provides many useful building blocks to build modern distributed application: service invocation, state management, input/output bindings, publish & subscribe, secret management……
Because of the advantages of sidecar model, the native applications can benefit from Dapr’s distributed capabilities while remain lightweight without introducing too many dependencies. This is not only helping to keep the size of java native applications, but also making the native applications easy to build as native images.
Installation
If you want to use this extension, you need to add the io.quarkiverse.dapr:quarkus-dapr extension first.
In your pom.xml file, add:
<dependency>
<groupId>io.quarkiverse.dapr</groupId>
<artifactId>quarkus-dapr</artifactId>
</dependency>
Examples
With Quarkus Dapr Extension, it’s pretty easy for java developers.
publish & subscribe
To publish events to your message broker, just inject a dapr client to your bean and call it’s publishEvent() method:
@Inject
SyncDaprClient dapr;
dapr.publishEvent("messagebus", "topic1", content.getBytes(StandardCharsets.UTF_8), new HashMap<>());
To subscribe events for your message broker, adding some annotations on your method is enough:
@POST
@Path("/topic1")
@Topic(name = "topic1", pubsubName = "messagebus")
public String eventOnTopic2(String content) {......}
In the attributes name, pubsubName, and match of the @Topic annotation, it is possible to set a config property to be loaded at runtime.
@POST
@Path("/topic6")
@Topic(name = "${topic6}", pubsubName = "${pubsub.topic6}", rule = @Rule(match = "${match.rule6}", priority = 1))
public String eventOnTopic6(String content) {......}
For more details and hands-on experiences, please reference to our Demo.
Quarkus Dapr – Dev Services
The Quarkus Dapr Extension provides Dev Services to simplify local development when using Quarkus with Dapr. It automatically provisions the required infrastructure and configures the Dapr sidecar so you can focus on application development.
Setting the Dapr Image
Dev Services for Dapr uses container images that are compatible with the version defined by the dapr/java-sdk dependency.
By default, the image version is aligned with the constant declared in the dapr/java-sdk library. This guarantees runtime compatibility between the Java SDK and the Dapr sidecar.
If necessary, you can override the default image by configuring:
quarkus.dapr.devservices.image-name=your-custom-image
Dapr Dev Services – Provisioned Containers
By default, Quarkus Dapr Dev Services starts the following containers:
docker.io/library/postgres
ghcr.io/diagridio/diagrid-dashboard
docker.io/daprio/placement
docker.io/daprio/scheduler
docker.io/daprio/daprd
The first two containers (postgres and diagrid-dashboard) are required to enable Dapr Dashboard integration in the Dev UI.
In addition, Quarkus Dapr automatically provisions a State Store component named kvstore, configured to use the PostgreSQL instance.
You can disable the Dapr Dashboard with:
quarkus.dapr.devservices.dashboard.enabled=false
|
If the Dapr Dashboard is disabled, Quarkus Dapr automatically provisions an in-memory State Store component named |
Provided Dapr Components
As described above, Quarkus Dapr automatically configures the Dapr sidecar with a State Store component.
It also registers a Pub/Sub component. When running with Dev Services, this Pub/Sub component uses an in-memory implementation suitable for development and testing.
|
The component name for Pub/Sub (in-memory) is The component name for the State Store (in-memory or PostgreSQL) is |
Using Dev Services
The following example demonstrates how to use the automatically provisioned Pub/Sub and State Store components.
package io.dapr.docs;
import io.dapr.Topic;
import io.dapr.client.domain.CloudEvent;
import io.dapr.client.domain.State;
import io.quarkiverse.dapr.core.SyncDaprClient;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;
import java.util.Map;
import java.util.UUID;
@Path("/dapr")
public class DaprResource {
@Inject
SyncDaprClient client;
@POST
@Path("/state")
public Response saveState() {
client.saveState("kvstore", "identity", UUID.randomUUID().toString()); (1)
return Response.ok().build();
}
@GET
@Path("/state")
public Response getState() {
State<String> state = client.getState("kvstore", "identity", String.class);
return Response.ok(Map.of("identity", state.getValue())).build();
}
@POST
@Path("/pub")
public void pub() {
client.publishEvent("pubsub", "topicName", "Hello from Quarkus!"); (2)
}
@POST
@Topic(name = "topicName") (3)
@Path("/sub") (4)
public void sub(CloudEvent<String> event) {
System.out.println("Received event: " + event.getData());
}
}
| 1 | kvstore is the name of the State Store component created by Dev Services. identity is the key used to store the value. |
| 2 | pubsub is the name of the Pub/Sub component created by Dev Services. topicName is the topic used to publish the message. |
| 3 | topicName must match the topic used in publishEvent. The @io.dapr.Topic annotation maps the endpoint to the topic. |
| 4 | The value sub can be any path. It is used internally to expose the subscription endpoint. |
|
The default value of
|
Adding Custom Dapr Components
To use non in-memory Dapr components, add them to the following directory:
src/main/resources/components
Example: Pub/Sub with Redis
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: pubsub
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
Example: State Store with Redis
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
metadata:
- name: keyPrefix
value: name
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
Quarkus Dapr automatically detects these files and configures the Dapr sidecar with the provided components during application startup.
Configuration property fixed at build time - All other configuration properties are overridable at runtime
Configuration property |
Type |
Default |
|---|---|---|
Whether this Dev Service should start with the application in dev mode or tests. Dapr Dev Services are enabled by default. Environment variable: |
boolean |
|
The Dapr container image to use. Environment variable: |
string |
|
Whether this Dev Service should start the Dapr Workflow Dashboard. Environment variable: |
boolean |
|
default pub sub config Environment variable: |
string |
|
Indicates whether Dapr Workflow should be enabled. Environment variable: |
boolean |
|
pub sub type Environment variable: |
string |
|
publish pub sub default metadata Environment variable: |
Map<String,String> |
|
consume pub sub default metadata Environment variable: |
Map<String,String> |