Skip to content

Commit ba92e31

Browse files
Examples for Micronaut CC Blog Post. (#8) (#283)
* Examples for Micronaut CC Blog Post. * Registered trademark on Kafka * Correction to dependencies in terraform SR provisioning. * Update per code review * Update per code review * Adding export statements to setup CC CLI. * Links to required software. * Documentation updates, per code review. --------- Co-authored-by: Dave Troiano <[email protected]>
1 parent 5455ea1 commit ba92e31

40 files changed

+1846
-0
lines changed

micronaut-cc/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Thumbs.db
2+
.DS_Store
3+
.gradle
4+
build/
5+
target/
6+
out/
7+
.micronaut/
8+
.idea
9+
*.iml
10+
*.ipr
11+
*.iws
12+
.project
13+
.settings
14+
.classpath
15+
.factorypath
16+
gradlew.bat
17+
gradle/wrapper/

micronaut-cc/README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Micronaut Framework, Apache Kafka&reg; and Confluent Cloud
2+
3+
## What is Micronaut Framework?
4+
5+
Micronaut is an open source JVM-based framework for building lightweight microservices. It is designed to provide features like
6+
dependency injection and inversion of control without Java reflection. Micronaut provides configuration-first bindings with sensible
7+
defaults to a number of external systems - databases, messaging systems, caches, and others.
8+
9+
Among those integrations is Apache Kafka. The code in this example should help you get started with concrete examples on integrating
10+
Micronaut with Apache Kafka, specifically on Confluent Cloud with Stream Governance.
11+
12+
## Using this Example
13+
14+
There are some prerequisites to using these examples:
15+
16+
* [Confluent Cloud Account](https://confluent.cloud)
17+
* [Confluent CLI](https://docs.confluent.io/confluent-cli/current/install.html)
18+
* [Terraform](https://www.terraform.io/)
19+
* Java 21 (I like using [sdkman](https://sdkman.io/) to manage multiple JDK installations.)
20+
* [Docker](https://www.docker.com/)
21+
* [jq](https://jqlang.github.io/jq/)
22+
* Editor/IDE of choice
23+
24+
Note that when installing and configuring the Confluent CLI, include the Confluent Cloud credentials as environment variables for future use. For instance with bash or zsh, include these in your profile as export statements:
25+
26+
```shell
27+
export CONFLUENT_CLOUD_API_KEY=<API KEY>
28+
export CONFLUENT_CLOUD_API_SECRET<API SECRET>
29+
```
30+
31+
### Provision Confluent Cloud Resources
32+
33+
Confluent Cloud resources are provisioned using terraform. Change to the `terraform` directory and issue the following commands:
34+
35+
```bash
36+
export TF_VAR_org_id=$(confluent organization list -o json | jq -c -r '.[] | select(.is_current)' | jq '.id')
37+
terraform init
38+
terraform plan -out "tfplan"
39+
terraform apply
40+
```
41+
42+
Export the environment variables needed to run the application using `terraform output` as follows:
43+
44+
```bash
45+
mkdir -p ~/tools
46+
terraform output -json | jq -r 'to_entries[] | .key + "=\"" + (.value.value | tostring) + "\""' | while read -r line ; do echo "$line"; done > ~/tools/micronaut-cc.properties
47+
```
48+
49+
These key-value pairs will need to be added as environment variables to the shell (or IDE run configuration) used to run the application.
50+
51+
### Run the Application
52+
53+
This microservice requires a MySQL database instance. Using Docker, start a MySQL container as follows:
54+
55+
```bash
56+
docker run --name micronaut-mysql -e MYSQL_ROOT_PASSWORD="micronaut" -p 3306:3306 -d mysql:8.2.0
57+
```
58+
59+
Next, use the bash shell of the running container to create a schema:
60+
61+
```bash
62+
docker exec -it micronaut-mysql bash
63+
bash-4.4# mysql -u root -p
64+
Enter password:
65+
Welcome to the MySQL monitor. Commands end with ; or \g.
66+
Your MySQL connection id is 31
67+
Server version: 8.2.0 MySQL Community Server - GPL
68+
69+
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
70+
71+
Oracle is a registered trademark of Oracle Corporation and/or its
72+
affiliates. Other names may be trademarks of their respective
73+
owners.
74+
75+
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
76+
77+
mysql> show schemas;
78+
+--------------------+
79+
| Database |
80+
+--------------------+
81+
| information_schema |
82+
| mysql |
83+
| performance_schema |
84+
| sys |
85+
+--------------------+
86+
5 rows in set (0.01 sec)
87+
88+
mysql> create schema micronaut;
89+
Query OK, 1 row affected (0.01 sec)
90+
91+
mysql> show schemas;
92+
+--------------------+
93+
| Database |
94+
+--------------------+
95+
| information_schema |
96+
| micronaut |
97+
| mysql |
98+
| performance_schema |
99+
| sys |
100+
+--------------------+
101+
5 rows in set (0.01 sec)
102+
```
103+
104+
As commited to this repo, the `application.yml` file for Micronaut is configured to use this MySQL database and schema, with the
105+
credentials in the command. If you configure MySQL differently, please make the appropriate changes to the `application.yml` before executing.
106+
107+
Export the key-value pairs to your shell (or IDE run configuration) then start the application. If using the command line, you can find information
108+
on that in the Micronaut User Guide linked below. IntelliJ IDEA provides a mechanism to import a key-value file as environment variables to a
109+
run configuration, as follows:
110+
111+
![Run Configuration Example](idea-run-config.png)
112+
113+
### Unit Testing
114+
115+
This repository includes unit tests to validate events are flowing to and from the Kafka topics configured. These unit tests make
116+
extensive use of Testcontainers, as does the test API for Micronaut by default. More can be found in the user guides.
117+
118+
To execute these tests using Gradle from the terminal window:
119+
```bash
120+
./gradlew test
121+
```
122+
123+
## Useful Micronaut Links
124+
- [User Guide](https://docs.micronaut.io/4.6.3/guide/index.html)
125+
- [API Reference](https://docs.micronaut.io/4.6.3/api/index.html)
126+
- [Configuration Reference](https://docs.micronaut.io/4.6.3/guide/configurationreference.html)
127+
- [Micronaut Guides](https://guides.micronaut.io/index.html)
128+
129+
### Feature kafka documentation
130+
131+
- [Micronaut Kafka Messaging documentation](https://micronaut-projects.github.io/micronaut-kafka/latest/guide/index.html)
132+
133+
### Feature serialization-jackson documentation
134+
135+
- [Micronaut Serialization Jackson Core documentation](https://micronaut-projects.github.io/micronaut-serialization/latest/guide/)
136+
137+
### Feature Testcontainers documentation
138+
139+
- [https://www.testcontainers.org/](https://www.testcontainers.org/)
140+
141+
## Confluent Cloud
142+
143+
- [https://confluent.cloud](Confluent Cloud Login/Signup)
144+
- [https://docs.confluent.io/cloud/current/overview.html](Confluent Cloud Docs)
145+
146+

micronaut-cc/build.gradle.kts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
plugins {
2+
id("com.github.johnrengelman.shadow") version "8.1.1"
3+
id("io.micronaut.application") version "4.4.2"
4+
id("io.micronaut.aot") version "4.4.2"
5+
id("com.bakdata.avro") version "1.0.0"
6+
}
7+
8+
version = "0.1"
9+
group = "example.micronaut"
10+
11+
repositories {
12+
mavenCentral()
13+
maven("https://packages.confluent.io/maven")
14+
}
15+
16+
val confluentVersion = project.properties.get("confluentVersion")
17+
18+
dependencies {
19+
annotationProcessor("io.micronaut.data:micronaut-data-processor")
20+
annotationProcessor("io.micronaut:micronaut-http-validation")
21+
annotationProcessor("io.micronaut.serde:micronaut-serde-processor")
22+
23+
implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
24+
implementation("io.micronaut.kafka:micronaut-kafka")
25+
implementation("io.micronaut.serde:micronaut-serde-jackson")
26+
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
27+
28+
implementation("org.apache.avro:avro:1.12.0")
29+
implementation("io.confluent:kafka-avro-serializer:${confluentVersion}")
30+
implementation("io.confluent:kafka-schema-rules:${confluentVersion}")
31+
32+
implementation("org.projectlombok:lombok:1.18.36")
33+
34+
implementation("net.datafaker:datafaker:2.4.1")
35+
36+
compileOnly("io.micronaut:micronaut-http-client")
37+
38+
runtimeOnly("ch.qos.logback:logback-classic")
39+
runtimeOnly("com.mysql:mysql-connector-j")
40+
runtimeOnly("org.yaml:snakeyaml")
41+
42+
testImplementation("io.micronaut:micronaut-http-client")
43+
testImplementation("io.micronaut.test:micronaut-test-rest-assured")
44+
testImplementation("com.fasterxml.jackson.core:jackson-databind:2.9.5")
45+
testImplementation("org.assertj:assertj-core")
46+
testImplementation("org.awaitility:awaitility:4.2.2")
47+
testImplementation("org.testcontainers:junit-jupiter:1.20.3")
48+
testImplementation("org.testcontainers:kafka:1.20.3")
49+
testImplementation("org.testcontainers:mysql:1.20.3")
50+
testImplementation("org.testcontainers:testcontainers:1.20.3")
51+
}
52+
53+
54+
application {
55+
mainClass = "io.confluent.devrel.Application"
56+
}
57+
java {
58+
sourceCompatibility = JavaVersion.toVersion("21")
59+
targetCompatibility = JavaVersion.toVersion("21")
60+
}
61+
62+
sourceSets {
63+
main {
64+
java.srcDirs("src/main/java", "build/generated-main-avro-java")
65+
}
66+
}
67+
68+
graalvmNative.toolchainDetection = false
69+
70+
micronaut {
71+
runtime("netty")
72+
testRuntime("junit5")
73+
processing {
74+
incremental(true)
75+
annotations("example.micronaut.*")
76+
}
77+
aot {
78+
// Please review carefully the optimizations enabled below
79+
// Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details
80+
optimizeServiceLoading = false
81+
convertYamlToJava = false
82+
precomputeOperations = true
83+
cacheEnvironment = true
84+
optimizeClassLoading = true
85+
deduceEnvironment = true
86+
optimizeNetty = true
87+
replaceLogbackXml = true
88+
}
89+
}
90+
91+
92+

micronaut-cc/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
micronautVersion=4.6.3
2+
confluentVersion=7.7.0

0 commit comments

Comments
 (0)