This example demonstrates a simple application with a Kafka publisher and a subscriber.
Log message
POST request > controller > logger > Kafka broker > log
Receiving logged messages
GET request > controller > log > controller > GET response
Start a Kafka cluster and configure its bootstrap URLs in
src/main/resources/application.yml. It is done separately for producer and
consumer. In this example application it is set to localhost:9092 by default.
Build the application.
mvn clean packageStart the application in one terminal window.
java -jar target/vertx-spring-boot-sample-kafka.jar And access it in another.
First submit a couple of messages for processing.
echo "Hello, World" | http POST :8080
echo "Hello again" | http POST :8080 Then check the result.
http :8080You should get something like the following.
HTTP/1.1 200 OK
Content-Type: text/event-stream;charset=UTF-8
transfer-encoding: chunked
data:Hello, World
data:Hello againKafka client configuration is very straightforward and is done via application.(yml|properties) file. Here's the
one used in this application.
vertx:
kafka:
producer:
bootstrap:
# Default bootstrap servers to be used by producers
servers: localhost:9092
key:
# Default key serializer to be used by producers
serializer: org.apache.kafka.common.serialization.StringSerializer
value:
# Default value serializer to be used by producers
serializer: org.apache.kafka.common.serialization.StringSerializer
consumer:
bootstrap:
# Default bootstrap servers to be used by consumers
servers: localhost:9092
group:
# Default consumer group id
id: log
key:
# Default key deserializer to be used by consumers
deserializer: org.apache.kafka.common.serialization.StringDeserializer
value:
# Default value deserializer to be used by consumers
deserializer: org.apache.kafka.common.serialization.StringDeserializer
You can set any standard Kafka property by using vertx.kafka.producer or vertx.kafka.consumer prefixes for producer
and consumer respectively.