page_type | languages | products | description | urlFragment | ||
---|---|---|---|---|---|---|
sample |
|
|
Azure Spring Cloud Stream Binder Sample project for Event Hub client library |
azure-spring-cloud-sample-eventhubs-kafka |
This code sample demonstrates how to use the Spring Cloud Stream Kafka
binder for Azure Event Hub. The sample app exposes a RESTful API to receive
string message. Then message is sent through Azure Event Hub to a bean consumer
which simply logs the message.
Running this sample will be charged by Azure. You can check the usage and bill at this link.
-
Create a service principal for use in by your app. Please follow create service principal from Azure CLI.
-
Create Azure Event Hubs.
-
Update application.yaml file
spring: cloud: azure: client-id: [service-principal-id] client-secret: [service-principal-secret] tenant-id: [tenant-id] resource-group: [resource-group] eventhub: namespace: [eventhub-namespace] stream: function: definition: consume;supply bindings: consume-in-0: destination: [eventhub-name] group: [consumer-group] supply-out-0: destination: [the-same-eventhub-name-as-above]
-
Run the
mvn spring-boot:run
in the root of the code sample to get the app running. -
Send a POST request
$ curl -X POST http://localhost:8080/messages?message=hello
-
Verify in your app’s logs that a similar message was posted:
New message received: hello
-
Delete the resources on Azure Portal to avoid unexpected charges.
-
Meet with
Creating topics with default partitions/replication factor are only supported in CreateTopicRequest version 4+
error.o.s.c.s.b.k.p.KafkaTopicProvisioner : Failed to create topics org.apache.kafka.common.errors.UnsupportedVersionException: Creating topics with default partitions/replication factor are only supported in CreateTopicRequest version 4+. The following topics need values for partitions and replicas
When this error is found, add this configuration item
spring.cloud.stream.kafka.binder.replicationFactor
, with the value set to at least 1. For more information, see Spring Cloud Stream Kafka Binder Reference Guide.