Quarkus - Dapr
# Quarkus - Dapr
(opens new window)
(opens new window)
(opens new window)
# Introduction
# What is Quarkus?
Traditional Java stacks were engineered for monolithic applications with long startup times and large memory requirements in a world where the cloud, containers, and Kubernetes did not exist. Java frameworks needed to evolve to meet the needs of this new world.
Quarkus was created to enable Java developers to create applications for a modern, cloud-native world. Quarkus is a Kubernetes-native Java framework tailored for GraalVM and HotSpot, crafted from best-of-breed Java libraries and standards. The goal is to make Java the leading platform in Kubernetes and serverless environments while offering developers a framework to address a wider range of distributed application architectures.
For more information about Quarkus, please go https://quarkus.io/.
# 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.
# What is NATS
NATS is a connective technology built for the ever increasingly hyper-connected world. It is a single technology that enables applications to securely communicate across any combination of cloud vendors, on-premise, edge, web and mobile, and devices. NATS consists of a family of open source products that are tightly integrated but can be deployed easily and independently. NATS is being used globally by thousands of companies, spanning use-cases including microservices, edge computing, mobile, IoT and can be used to augment or replace traditional messaging.
The NATS Server acts as a central nervous system for building distributed applications. Client APIs are provided in over 40 languages and frameworks including Go, Java, JavaScript/TypeScript, Python, Ruby, Rust, C#, C, and NGINX. Real time data streaming, highly resilient data storage and flexible data retrieval are supported through JetStream (opens new window) , the next generation streaming platform built into the NATS server. Check out the full list of NATS clients .
NATS was created by Derek Collison, in response to the market need for a simple, secure, and connective technology. NATS is currently deployed in some of the largest cloud platforms, including: VMware, CloudFoundry, Baidu, Siemens, and GE. NATS is 100% free to use under the Apache-2.0 Open Source License.
NATS is unique in its simplicity and performance, and as a result powers some of the largest production environments. You can learn more about NATS in extensive documentation (opens new window).
# What is Jetstream
NATS has a built-in distributed persistence system called JetStream (opens new window) which enables new functionalities and higher qualities of service on top of the base 'Core NATS' functionalities and qualities of service. JetStream is built-in to nats-server and you only need 1 (or 3 or 5 if you want fault-tolerance against 1 or 2 simultaneous NATS server failures) of your NATS server(s) to be JetStream enabled for it to be available to all the client applications. JetStream was created to solve the problems identified with streaming in technology today - complexity, fragility, and a lack of scalability. Some technologies address these better than others, but no current streaming technology is truly multi-tenant, horizontally scalable, and supports multiple deployment models. No other technology that we are aware of can scale from edge to cloud under the same security context while having complete deployment observability for operations.
# 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<>());
2
3
4
To subscribe events for your message broker, adding some annotations on your method is enough:
@POST
@Path("/topic1")
@Topic(name = "topic1", pubsubName = "jetstream-pubsub")
public String eventOnTopic2(String content) {......}
2
3
4
For more details and hands-on experiences, please reference to our Demo.
# Notice
In order to user quarkus-dapr
extension, especially in kts, it has dependency error. okhttp3
version is need to up-to-date.
It had issue (opens new window) already. But how to solve in gradle kotlin. I have a solution.
configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "com.squareup.okhttp3" && requested.name == "okhttp" && requested.version == "3.14.9") {
useVersion("4.10.0")
because("fixes critical bug in 1.2")
}
}
}
2
3
4
5
6
7
8
9
however, when we change the okhttp3
version, there was a okio
version problem.
modules {
module("com.squareup.okio:okio") {
replacedBy("com.squareup.okio:okio-jvm", "change it now")
}
}
2
3
4
5
I put my complete dependency here.
dependencies {
implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
implementation("io.quarkiverse.dapr:quarkus-dapr:1.0.4")
implementation("com.squareup.okio:okio-jvm:3.0.0")
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.6.21")
implementation("io.quarkus:quarkus-arc")
testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.rest-assured:rest-assured")
modules {
module("com.squareup.okio:okio") {
replacedBy("com.squareup.okio:okio-jvm", "change it now")
}
}
configurations.all {
resolutionStrategy.eachDependency {
if (requested.group == "com.squareup.okhttp3" && requested.name == "okhttp" && requested.version == "3.14.9") {
useVersion("4.10.0")
because("fixes critical bug in 1.2")
}
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
::: danager notice
- first need nats-tool. connect to server
nats --server=nats://localhost:4222
- create different jetstream related with topic. :::