Skip to content

Latest commit

 

History

History
102 lines (69 loc) · 3.94 KB

File metadata and controls

102 lines (69 loc) · 3.94 KB

Camel Kafka - Spring Boot example

Abstract

An example which shows how to use the Apache Avro dataformat with Camel with the use of Apicurio Registry

Introduction

The application is configured to create the schema in apicurio registry if it doesn’t exist. This schema is then used to serialize and deserialize the Employee instance from/to a kafka topic. A new Employee instance is periodically serialized into a configured topic by one camel route and consumed, deserialized and printed to the log by the second route.

See the configuration file and the schema definition for more info.

Trying out the example on your local machine

Start the apicurio registry in a docker container:

podman run -d --rm --name apicurio -p 8080:8080 quay.io/apicurio/apicurio-registry-mem:2.6.13.Final

Start the kafka broker:

podman run --rm -d \
--name kafka \
-p 9092:9092 \
-e CLUSTER_ID=my-cluster \
-e KAFKA_NODE_ID=1 \
-e KAFKA_PROCESS_ROLES=controller,broker \
-e KAFKA_KRAFT_MODE=true \
-e KAFKA_LISTENERS='PLAINTEXT://0.0.0.0:9092,CONTROLLER://localhost:9093' \
-e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,CONTROLLER:PLAINTEXT \
-e KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT \
-e KAFKA_CONTROLLER_LISTENER_NAMES=CONTROLLER \
-e KAFKA_ADVERTISED_LISTENERS='PLAINTEXT://localhost:9092' \
-e KAFKA_CONTROLLER_QUORUM_VOTERS='1@localhost:9093' \
-e KAFKA_AUTO_CREATE_TOPICS_ENABLE=true \
-e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
confluentinc/cp-kafka:7.9.4-1-ubi8

How to run the example

You can run this example using

mvn spring-boot:run

You will see that each 5 seconds a new employee is created and received and printed to the application log:

New employee: {"firstName": "Buster", "lastName": "Upton", "hireDate": 1669040550701, "personal": {"phone": "1-628-816-1593", "address": "642 Crist Knolls"}}
New employee: {"firstName": "Genevie", "lastName": "Connelly", "hireDate": 1676979164176, "personal": {"phone": "111-242-0401", "address": "0423 Jared Junctions"}}
New employee: {"firstName": "Jefferson", "lastName": "Harber", "hireDate": 1659042443546, "personal": {"phone": "121.056.5072", "address": "8466 Tamisha Meadows"}}
New employee: {"firstName": "Wilburn", "lastName": "Lakin", "hireDate": 1666498121570, "personal": {"phone": "762.365.2778", "address": "9207 Schumm Point"}}
...

Trying out the example on OpenShift

First, start with creating a new OpenShift project:

oc new-project csb-example-avro

Start a new deployment for the apicurio registry:

oc new-app quay.io/apicurio/apicurio-registry-mem:2.6.13.Final

Install operator Streams for Apache Kafka in the created namespace using OperatorHub Documentation then create managed resources

oc apply -f ./src/main/resources/openshift/node-pool.yml

oc apply -f ./src/main/resources/openshift/kafka.yml

oc apply -f ./src/main/resources/openshift/topic.yml

How to run

The application is deployed using the openshift-maven-plugin that takes care of creating all the necessary OpenShift resources.

Simply use the following command to deploy the application:

mvn clean package -Popenshift

After the application pod reaches the Ready state you will see the expected output in the application’s pod log.

Help and contributions

If you hit any problem using Camel or have some feedback, then please let us know.

We also love contributors, so get involved :-)

The Camel riders!