Files
spring-cloud-function/spring-cloud-function-samples/function-sample-azure-kafka-trigger
Christian Tzolov 6299a5366b Streamline and refactor the azure and the azure-web adapters.
- Add SCF/Azure Gradle sample and docs.
- Move the function-azure-di-samples into standalone projects.
 - Apply the name convetion and project structure for the SCF adaptes.
   E.g.  function-sample-azure-XXX projects under the spring-cloud-function-samples root.
 - Remove the redudant samples.
 - Improve the samples docs and the Adapter generic docs.
- Streamline docs.
- Add azure web adapter sample and README.
- Add Spring Azure Functions banner for azure and azure web adapters.
- azure-web adapter fixes:
  - Fix issues in serverles-web ProxyHttpServletResponse implementation.
  - Remove the custom FunctionClassUtils utils in favor of scf-context/util/FunctionClassUtils.
- Remove redundant files.
- Add FunctionInvoker deprecation annotations.
- Extend the time trigger sample with Retry policies example.
2023-07-19 14:54:05 +02:00
..
2022-10-21 15:41:28 +02:00
2022-10-21 15:41:28 +02:00
2022-10-21 15:41:28 +02:00
2022-10-21 15:41:28 +02:00
2022-10-21 15:41:28 +02:00
2022-10-21 15:41:28 +02:00

Azure Function with Kafka Trigger & Output Binding

Spring Cloud Function example for implementing an Azure functions with KafkaTrigger and Kafka Binding support.

The Azure function is triggered by messages sent on the trigger topic and in turn calls the uppercase SCF with the trigger payload. The SCF capitalizes the input json value fields and sends the result to an output Kafka topic called: output.

Running Locally

First start a Kafka server locally. The ./src/main/resources/docker-compose-demo.yaml helps to start locally Zookeeper, Kafka and Kafka UI.

docker-compose -f ./src/main/resources/docker-compose-demo.yaml up

You can reach the Kafka UI (Redpanda) dashboard on http://localhost:8080/topics

The docker-compose pre-creates the trigger and output topics used by the function.

Next build and run the Azure function:

./mvnw clean package
./mvnw azure-functions:run

From the Kafka UI, got to the trigger topic view (http://localhost:8080/topics/trigger), select Actions/Publish Message and submit a new JSON message:

{ "foo" : "bar"}

Push the Publish button and let the function do its job and check the output topic (http://localhost:8080/topics/output) :

e.g.the bar is in uppercase:

{ "foo" : "BAR"}

Running on Azure (TODO: WIP)

Make sure you are logged in your Azure account.

az login

Build and deploy

./mvnw clean package
./mvnw azure-functions:deploy

Implementation

Configure the Kafka extension in the host.json settings:

{
    "functionTimeout": "00:05:00",
    "version": "2.0",
    "extensions": {
        "kafka": {
            "maxBatchSize": 64,
            "SubscriberIntervalInSeconds": 1,
            "ExecutorChannelCapacity": 1,
            "ChannelFullRetryIntervalInMs": 50
        }
    },
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[3.3.0, 4.0.0)"
    }
}

Also to allow your functions to scale properly on the Premium plan when using Kafka triggers and bindings, you need to enable runtime scale monitoring.

Notes

  • Disable the spring-boot-maven-plugin in favor of the azure-functions-maven-plugin.
  • Exclude the org.springframework.boot:spring-boot-starter-logging dependency from the org.springframework.cloud:spring-cloud-function-adapter-azure.
  • In local.settings.json set the local values for the %BrokerList, %ConfluentCloudUsername% and the %TriggerKafkaTopic% trigger and binding variables:
{
	"IsEncrypted": false,
	"Values": {
...
		"BrokerList": "localhost:9092",
		"ConfluentCloudUsername": "test",
		"TriggerKafkaTopic": "trigger"
	}
}

References