Collaborate Kafka and Azure Functions securely within closed network

Published 06-08-2021 09:19 PM 1,128 Views
Microsoft

1. Introduction

Azure has offered options for Apache Kafka as PaaS - "Event Hubs for Kafka" or Kafka clusters on HDInsight. Event Hub option is more cloud managed than HDInsight one, so HDInsight option is user managed than Event Hub one. It's required to create "Producer" sending data to Kafka and "Consumer" retrieving data from Kafka for both options.
In this post, you can acquire knowledge how to develop scalable Kafka Producer by using Azure Functions.

 

2. Architecture 

Here is a sample architecture that Kafka Producer on Azure Functions sends data to Kafka clusters on HDInsight. HDInsight offers REST Proxy as option, and you can access Kafka endpoints on Azure VNETs by using the REST Proxy. Your application can securely access Kafka on HDInsight via subnets on Azure VNETs by setup VNET Integration on Azure Functions and connection with VNETs.

daisami_0-1623208728321.png

 

3. Setup environment

Create a VNET and subnets for this sample architecture. You should adjust resource names and IP address spaces for your environments.

 

 

export MyResourceGroup="myRG"
export vnetName="myVnet"
export VnetPrefix="10.10.0.0/16"
export FunctionsSubnet="Functions-Subnet"
export FunctionsSubnetPrefix="10.10.10.0/24"
export KafkaSubnet="Kafka-Subnet"
export KafkaSubnetPrefix="10.10.20.0/24"
export location="japaneast"

# Create Reource Group
az group create --name $MyResourceGroup --location $location

# Create VNET with a Subnet for Functions. More than /24 address range is required for VNet Integration
az network vnet create -g $MyResourceGroup -n $vnetName --address-prefix $VnetPrefix \
    --subnet-name $FunctionsSubnet --subnet-prefix $FunctionsSubnetPrefix

# Create Subnet for Kafka on HDInsight
az network vnet subnet create -g $MyResourceGroup --vnet-name $vnetName -n $KafkaSubnet \
    --address-prefixes $KafkaSubnetPrefix

 

 

Create Azure Functions with Standard or Premium plan to utilize VNET Integration. We have chosen Premium (EP1) in this sample.

 

 

# Function app and storage account names must be unique.
export storageName=functionstor$RANDOM
export functionAppName=functionkafkaproducer
export premiumplanName=myappspremplan

# Create an azure storage account
az storage account create \
  --name $storageName \
  --location $location \
  --resource-group $MyResourceGroup \
  --sku Standard_LRS

# Create a Premium plan
az functionapp plan create \
  --name $premiumplanName \
  --resource-group $MyResourceGroup \
  --location $location \
  --sku EP1

# Create a Function App
az functionapp create \
  --name $functionAppName \
  --storage-account $storageName \
  --plan $premiumplanName \
  --resource-group $MyResourceGroup \
  --runtime java \
  --functions-version 3

 

 

Create HDInsight Kafka cluster on a subnet which you have already created by using --subnet option. This option works well if you will deploy Kafka cluster on a subnet within same resource group, but you have to specify subnet ID to deploy on a subnet in different resource group.

 

 

export clusterName="mykafkacluster"
export storageAccount="myhdikafkastor1"
export httpPassword='<Your Cluster Admin Password>'
export sshPassword='<Your SSH Password>'

export storageContainer=$(echo $clusterName | tr "[:upper:]" "[:lower:]")
export workernodeCount=3
export clusterType=kafka
export clusterVersion=4.0
export componentVersion=kafka=2.1
export subnet="H"

# Create storage account
az storage account create \
    --name $storageAccount \
    --resource-group $resourceGroupName \
    --https-only true \
    --kind StorageV2 \
    --location $location \
    --sku Standard_LRS

# Export primary key of Storage Account
export storageAccountKey=$(az storage account keys list \
    --account-name $storageAccount \
    --resource-group $resourceGroupName \
    --query [0].value -o tsv)

# Create blob container
az storage container create \
    --name $storageContainer \
    --account-key $storageAccountKey \
    --account-name $storageAccount

# Create HDInsight Kafka clusterName
az hdinsight create \
    --name $clusterName \
    --resource-group $resourceGroupName \
    --type $clusterType \
    --component-version $componentVersion \
    --http-password $httpPassword \
    --http-user admin \
    --location $location \
    --ssh-password $sshPassword \
    --ssh-user sshuser \
    --storage-account $storageAccount \
    --storage-account-key $storageAccountKey \
    --storage-container $storageContainer \
    --version $clusterVersion \
    --workernode-count $workernodeCount \
    --workernode-data-disks-per-node 2 \
    --vnet $vnetName \
    --subnet $KafkaSubnet

 

 

Setup VNET Integration on Azure Functions. Open your Azure Functions resource, Choose "Networking" and click "Click here to confiture" under VNet Integration.

daisami_1-1623210306195.png

 

Choose VNET and subnet to connect by following portal.

daisami_2-1623210325411.png

 

As next, here is important tip. It's not possible to access Kafka Brokers from Azure Functions within VNETs as default, so we have to setup environment variables like below.

Name Value  
WEBSITE_DNS_SERVER 168.63.129.16 work with Azure DNS private zones
WEBSITE_VNET_ROUTE_ALL 1 route all of your outbound traffic into VNETs

 

Refer screenshot below for this setup.

 

daisami_2-1623210865442.png

 

Now, we can develop Kafka Producer for Azure Functions. Add Maven dependency to use Kafka client like below. We have setup 2.1.0 version as same with deployed HDInsight Kafka version.

Add texts below to properties in pom.xml.

 

 

<properties>
    <kafka.version>2.1.0</kafka.version>
</properties>

 

 

Add texts below to dependency in pom.xml. 

 

 

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>${kafka.version}</version>
</dependency>

 

 

Here is code sample for Producer. This is a simple sample which sends a message to a Kafka cluster per request. You should adjust this code for your environment to improve performance or others. Note that build environment have to resolve Kafka Broker name space. 

 

 

package com.function;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import java.util.Optional;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.DescribeTopicsResult;
import org.apache.kafka.clients.admin.KafkaAdminClient;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.admin.TopicDescription;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import java.util.Properties;
import java.util.Random;
import java.io.IOException;

/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {
    /**
     * This function listens at endpoint "/api/FunctionKafkaProducerJava?". Two ways to invoke it using "curl" command in bash:
     * $ curl -XPOST {your host}/api/FunctionKafkaProducerJava?name={topicName} -d 'JSONBODY'
     */

    @FunctionName("FunctionKafkaProducerJava")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET, HttpMethod.POST},
                authLevel = AuthorizationLevel.FUNCTION)
                HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        // Parse query parameter
        final String topicName = request.getQueryParameters().get("name");
        final String reqBody = request.getBody().orElse("");

        // Set properties used to configure the producer
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "<KAFKABROKERS>");
        // Set how to serialize key/value pairs
        properties.setProperty("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        properties.setProperty("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

        // Create Kafka Producer
        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

        // Send message to Kafka
        try
        {
            producer.send(new ProducerRecord<String, String>(topicName, reqBody)).get();            
        }
        catch (Exception e)
        {
            System.out.print(e.getMessage());
        }

        // Close Kafka producer
        producer.flush();
        producer.close();
        return request.createResponseBuilder(HttpStatus.OK).body(reqBody).build();
    }
}

 

 

As an example to improve performance, you can change sample codes above by sending messages as a list at once. This sample can send multiple messages with Kafka Producer connection as just one post data.

 

 

        // Create Kafka Producer
        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

        // Send message to Kafka
        String jsonLine;
        for (int i=0; i < reqBodyArray.length; i++) {
            try {
                producer.send(new ProducerRecord<String, String>(topicName, reqBodyArray[i])).get();
            }
            catch (Exception e) {
                System.out.print(e.getMessage());
            }
        }
        // Close Kafka producer
        producer.flush();
        producer.close();

 

 

Azure Functions in this sample uses HTTP Trigger, so you can send sample data as HTTP post requests. 

 

 

curl -XPOST {your host}/api/FunctionKafkaProducerJava?name={topicName} -d '{<JSONBODY>}'

 

 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2429394%22%20slang%3D%22en-US%22%3ECollaborate%20Kafka%20and%20Azure%20Functions%20securely%20within%20closed%20network%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2429394%22%20slang%3D%22en-US%22%3E%3CH3%20id%3D%22toc-hId-2057059728%22%20id%3D%22toc-hId-2057059014%22%3E1.%20Introduction%3C%2FH3%3E%0A%3CP%3EAzure%20has%20offered%20options%20for%20Apache%20Kafka%20as%20PaaS%20-%20%22Event%20Hubs%20for%20Kafka%22%20or%20Kafka%20clusters%20on%20HDInsight.%20Event%20Hub%20option%20is%20more%20cloud%20managed%20than%20HDInsight%20one%2C%20so%20HDInsight%20option%20is%20user%20managed%20than%20Event%20Hub%20one.%20It's%20required%20to%20create%20%22Producer%22%20sending%20data%20to%20Kafka%20and%20%22Consumer%22%20retrieving%20data%20from%20Kafka%20for%20both%20options.%3CBR%20%2F%3EIn%20this%20post%2C%20you%20can%20acquire%20knowledge%20how%20to%20develop%20scalable%20Kafka%20Producer%20by%20using%20Azure%20Functions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-249605265%22%20id%3D%22toc-hId-249604551%22%3E2.%20Architecture%26nbsp%3B%3C%2FH3%3E%0A%3CP%3EHere%20is%20a%20sample%20architecture%20that%20Kafka%20Producer%20on%20Azure%20Functions%20sends%20data%20to%20Kafka%20clusters%20on%20HDInsight.%20HDInsight%20offers%20REST%20Proxy%20as%20option%2C%20and%20you%20can%26nbsp%3B%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3Eaccess%20Kafka%20endpoints%20on%20Azure%20VNETs%20by%20using%20the%20REST%20Proxy.%26nbsp%3B%3C%2FSPAN%3EYour%20application%20can%20securely%20access%20Kafka%20on%20HDInsight%20via%20subnets%20on%20Azure%20VNETs%20by%20setup%20VNET%20Integration%20on%20Azure%20Functions%20and%20connection%20with%20VNETs.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22daisami_0-1623208728321.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F287337i34225A34307EB497%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22daisami_0-1623208728321.png%22%20alt%3D%22daisami_0-1623208728321.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1557849198%22%20id%3D%22toc-hId--1557849912%22%3E3.%20Setup%20environment%3C%2FH3%3E%0A%3CP%3ECreate%20a%20VNET%20and%20subnets%20for%20this%20sample%20architecture.%20You%20should%20adjust%20resource%20names%20and%20IP%20address%20spaces%20for%20your%20environments.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eexport%20MyResourceGroup%3D%22myRG%22%0Aexport%20vnetName%3D%22myVnet%22%0Aexport%20VnetPrefix%3D%2210.10.0.0%2F16%22%0Aexport%20FunctionsSubnet%3D%22Functions-Subnet%22%0Aexport%20FunctionsSubnetPrefix%3D%2210.10.10.0%2F24%22%0Aexport%20KafkaSubnet%3D%22Kafka-Subnet%22%0Aexport%20KafkaSubnetPrefix%3D%2210.10.20.0%2F24%22%0Aexport%20location%3D%22japaneast%22%0A%0A%23%20Create%20Reource%20Group%0Aaz%20group%20create%20--name%20%24MyResourceGroup%20--location%20%24location%0A%0A%23%20Create%20VNET%20with%20a%20Subnet%20for%20Functions.%20More%20than%20%2F24%20address%20range%20is%20required%20for%20VNet%20Integration%0Aaz%20network%20vnet%20create%20-g%20%24MyResourceGroup%20-n%20%24vnetName%20--address-prefix%20%24VnetPrefix%20%5C%0A%20%20%20%20--subnet-name%20%24FunctionsSubnet%20--subnet-prefix%20%24FunctionsSubnetPrefix%0A%0A%23%20Create%20Subnet%20for%20Kafka%20on%20HDInsight%0Aaz%20network%20vnet%20subnet%20create%20-g%20%24MyResourceGroup%20--vnet-name%20%24vnetName%20-n%20%24KafkaSubnet%20%5C%0A%20%20%20%20--address-prefixes%20%24KafkaSubnetPrefix%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20Azure%20Functions%20with%20Standard%20or%20Premium%20plan%20to%20utilize%20VNET%20Integration.%20We%20have%20chosen%20Premium%20(EP1)%20in%20this%20sample.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%23%20Function%20app%20and%20storage%20account%20names%20must%20be%20unique.%0Aexport%20storageName%3Dfunctionstor%24RANDOM%0Aexport%20functionAppName%3Dfunctionkafkaproducer%0Aexport%20premiumplanName%3Dmyappspremplan%0A%0A%23%20Create%20an%20azure%20storage%20account%0Aaz%20storage%20account%20create%20%5C%0A%20%20--name%20%24storageName%20%5C%0A%20%20--location%20%24location%20%5C%0A%20%20--resource-group%20%24MyResourceGroup%20%5C%0A%20%20--sku%20Standard_LRS%0A%0A%23%20Create%20a%20Premium%20plan%0Aaz%20functionapp%20plan%20create%20%5C%0A%20%20--name%20%24premiumplanName%20%5C%0A%20%20--resource-group%20%24MyResourceGroup%20%5C%0A%20%20--location%20%24location%20%5C%0A%20%20--sku%20EP1%0A%0A%23%20Create%20a%20Function%20App%0Aaz%20functionapp%20create%20%5C%0A%20%20--name%20%24functionAppName%20%5C%0A%20%20--storage-account%20%24storageName%20%5C%0A%20%20--plan%20%24premiumplanName%20%5C%0A%20%20--resource-group%20%24MyResourceGroup%20%5C%0A%20%20--runtime%20java%20%5C%0A%20%20--functions-version%203%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20HDInsight%20Kafka%20cluster%20on%20a%20subnet%20which%20you%20have%20already%20created%20by%20using%20%3CSTRONG%3E--subnet%3C%2FSTRONG%3E%20option.%20This%20option%20works%20well%20if%20you%20will%20deploy%20Kafka%20cluster%20on%20a%20subnet%20within%20same%20resource%20group%2C%20but%20you%20have%20to%20specify%20subnet%20ID%20to%20deploy%20on%20a%20subnet%20in%20different%20resource%20group.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eexport%20clusterName%3D%22mykafkacluster%22%0Aexport%20storageAccount%3D%22myhdikafkastor1%22%0Aexport%20httpPassword%3D'%3CYOUR%20cluster%3D%22%22%20admin%3D%22%22%20password%3D%22%22%3E'%0Aexport%20sshPassword%3D'%3CYOUR%20ssh%3D%22%22%20password%3D%22%22%3E'%0A%0Aexport%20storageContainer%3D%24(echo%20%24clusterName%20%7C%20tr%20%22%5B%3Aupper%3A%5D%22%20%22%5B%3Alower%3A%5D%22)%0Aexport%20workernodeCount%3D3%0Aexport%20clusterType%3Dkafka%0Aexport%20clusterVersion%3D4.0%0Aexport%20componentVersion%3Dkafka%3D2.1%0Aexport%20subnet%3D%22H%22%0A%0A%23%20Create%20storage%20account%0Aaz%20storage%20account%20create%20%5C%0A%20%20%20%20--name%20%24storageAccount%20%5C%0A%20%20%20%20--resource-group%20%24resourceGroupName%20%5C%0A%20%20%20%20--https-only%20true%20%5C%0A%20%20%20%20--kind%20StorageV2%20%5C%0A%20%20%20%20--location%20%24location%20%5C%0A%20%20%20%20--sku%20Standard_LRS%0A%0A%23%20Export%20primary%20key%20of%20Storage%20Account%0Aexport%20storageAccountKey%3D%24(az%20storage%20account%20keys%20list%20%5C%0A%20%20%20%20--account-name%20%24storageAccount%20%5C%0A%20%20%20%20--resource-group%20%24resourceGroupName%20%5C%0A%20%20%20%20--query%20%5B0%5D.value%20-o%20tsv)%0A%0A%23%20Create%20blob%20container%0Aaz%20storage%20container%20create%20%5C%0A%20%20%20%20--name%20%24storageContainer%20%5C%0A%20%20%20%20--account-key%20%24storageAccountKey%20%5C%0A%20%20%20%20--account-name%20%24storageAccount%0A%0A%23%20Create%20HDInsight%20Kafka%20clusterName%0Aaz%20hdinsight%20create%20%5C%0A%20%20%20%20--name%20%24clusterName%20%5C%0A%20%20%20%20--resource-group%20%24resourceGroupName%20%5C%0A%20%20%20%20--type%20%24clusterType%20%5C%0A%20%20%20%20--component-version%20%24componentVersion%20%5C%0A%20%20%20%20--http-password%20%24httpPassword%20%5C%0A%20%20%20%20--http-user%20admin%20%5C%0A%20%20%20%20--location%20%24location%20%5C%0A%20%20%20%20--ssh-password%20%24sshPassword%20%5C%0A%20%20%20%20--ssh-user%20sshuser%20%5C%0A%20%20%20%20--storage-account%20%24storageAccount%20%5C%0A%20%20%20%20--storage-account-key%20%24storageAccountKey%20%5C%0A%20%20%20%20--storage-container%20%24storageContainer%20%5C%0A%20%20%20%20--version%20%24clusterVersion%20%5C%0A%20%20%20%20--workernode-count%20%24workernodeCount%20%5C%0A%20%20%20%20--workernode-data-disks-per-node%202%20%5C%0A%20%20%20%20--vnet%20%24vnetName%20%5C%0A%20%20%20%20--subnet%20%24KafkaSubnet%3C%2FYOUR%3E%3C%2FYOUR%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESetup%20VNET%20Integration%20on%20Azure%20Functions.%20Open%20your%20Azure%20Functions%20resource%2C%20Choose%20%22Networking%22%20and%20click%20%22Click%20here%20to%20confiture%22%20under%20VNet%20Integration.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22daisami_1-1623210306195.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F287360iB2CC6EA0E383563C%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22daisami_1-1623210306195.png%22%20alt%3D%22daisami_1-1623210306195.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EChoose%20VNET%20and%20subnet%20to%20connect%20by%20following%20portal.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22daisami_2-1623210325411.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F287361i8EECFEB2A162C0C6%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22daisami_2-1623210325411.png%22%20alt%3D%22daisami_2-1623210325411.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20next%2C%20here%20is%20important%20tip.%20It's%20not%20possible%20to%20access%20Kafka%20Brokers%20from%20Azure%20Functions%20within%20VNETs%20as%20default%2C%20so%20we%20have%20to%20setup%20environment%20variables%20like%20below.%3C%2FP%3E%0A%3CTABLE%20border%3D%221%22%20width%3D%22100%25%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2230px%22%3EName%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2230px%22%3EValue%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2230px%22%3E%26nbsp%3B%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2230px%22%3EWEBSITE_DNS_SERVER%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2230px%22%3E%3CSPAN%3E168.63.129.16%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2230px%22%3E%3CSPAN%3Ework%20with%20Azure%20DNS%20private%20zones%3C%2FSPAN%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2230px%22%3E%3CSPAN%3EWEBSITE_VNET_ROUTE_ALL%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%3E1%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2230px%22%3E%3CSPAN%3Eroute%20all%20of%20your%20outbound%20traffic%20into%20VNETs%3C%2FSPAN%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERefer%20screenshot%20below%20for%20this%20setup.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditordaisami_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22daisami_2-1623210865442.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F287362i8F3BF9AC0C7D1262%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22daisami_2-1623210865442.png%22%20alt%3D%22daisami_2-1623210865442.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%2C%20we%20can%20develop%20Kafka%20Producer%20for%20Azure%20Functions.%20Add%20Maven%20dependency%20to%20use%20Kafka%20client%20like%20below.%20We%20have%20setup%20%3CSTRONG%3E2.1.0%3C%2FSTRONG%3E%20version%20as%20same%20with%20deployed%20HDInsight%20Kafka%20version.%3C%2FP%3E%0A%3CP%3EAdd%20texts%20below%20to%20%3CSTRONG%3Eproperties%3C%2FSTRONG%3E%20in%20pom.xml.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-html%22%3E%3CCODE%3E%3CPROPERTIES%3E%0A%20%20%20%20%3CKAFKA.VERSION%3E2.1.0%3C%2FKAFKA.VERSION%3E%0A%3C%2FPROPERTIES%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAdd%20texts%20below%20to%20%3CSTRONG%3Edependency%26nbsp%3B%3C%2FSTRONG%3Ein%20pom.xml.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-html%22%3E%3CCODE%3E%3CDEPENDENCY%3E%0A%20%20%20%20%3CGROUPID%3Eorg.apache.kafka%3C%2FGROUPID%3E%0A%20%20%20%20%3CARTIFACTID%3Ekafka-clients%3C%2FARTIFACTID%3E%0A%20%20%20%20%3CVERSION%3E%24%7Bkafka.version%7D%3C%2FVERSION%3E%0A%3C%2FDEPENDENCY%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20code%20sample%20for%20Producer.%20This%20is%20a%20simple%20sample%20which%20sends%20a%20message%20to%20a%20Kafka%20cluster%20per%20request.%20You%20should%20adjust%20this%20code%20for%20your%20environment%20to%20improve%20performance%20or%20others.%20Note%20that%20build%20environment%20have%20to%20resolve%20Kafka%20Broker%20name%20space.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-java%22%3E%3CCODE%3Epackage%20com.function%3B%0A%0Aimport%20com.microsoft.azure.functions.ExecutionContext%3B%0Aimport%20com.microsoft.azure.functions.HttpMethod%3B%0Aimport%20com.microsoft.azure.functions.HttpRequestMessage%3B%0Aimport%20com.microsoft.azure.functions.HttpResponseMessage%3B%0Aimport%20com.microsoft.azure.functions.HttpStatus%3B%0Aimport%20com.microsoft.azure.functions.annotation.AuthorizationLevel%3B%0Aimport%20com.microsoft.azure.functions.annotation.FunctionName%3B%0Aimport%20com.microsoft.azure.functions.annotation.HttpTrigger%3B%0Aimport%20java.util.Optional%3B%0Aimport%20org.apache.kafka.clients.producer.KafkaProducer%3B%0Aimport%20org.apache.kafka.clients.producer.ProducerRecord%3B%0Aimport%20org.apache.kafka.clients.producer.ProducerConfig%3B%0Aimport%20org.apache.kafka.clients.admin.AdminClient%3B%0Aimport%20org.apache.kafka.clients.admin.DescribeTopicsResult%3B%0Aimport%20org.apache.kafka.clients.admin.KafkaAdminClient%3B%0Aimport%20org.apache.kafka.clients.CommonClientConfigs%3B%0Aimport%20org.apache.kafka.clients.admin.TopicDescription%3B%0Aimport%20java.util.Collection%3B%0Aimport%20java.util.Collections%3B%0Aimport%20java.util.concurrent.ExecutionException%3B%0Aimport%20java.util.Properties%3B%0Aimport%20java.util.Random%3B%0Aimport%20java.io.IOException%3B%0A%0A%2F**%0A%20*%20Azure%20Functions%20with%20HTTP%20Trigger.%0A%20*%2F%0Apublic%20class%20Function%20%7B%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20This%20function%20listens%20at%20endpoint%20%22%2Fapi%2FFunctionKafkaProducerJava%3F%22.%20Two%20ways%20to%20invoke%20it%20using%20%22curl%22%20command%20in%20bash%3A%0A%20%20%20%20%20*%20%24%20curl%20-XPOST%20%7Byour%20host%7D%2Fapi%2FFunctionKafkaProducerJava%3Fname%3D%7BtopicName%7D%20-d%20'JSONBODY'%0A%20%20%20%20%20*%2F%0A%0A%20%20%20%20%40FunctionName(%22FunctionKafkaProducerJava%22)%0A%20%20%20%20public%20HttpResponseMessage%20run(%0A%20%20%20%20%20%20%20%20%20%20%20%20%40HttpTrigger(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%20%3D%20%22req%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20methods%20%3D%20%7BHttpMethod.GET%2C%20HttpMethod.POST%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20authLevel%20%3D%20AuthorizationLevel.FUNCTION)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20HttpRequestMessage%3COPTIONAL%3E%3CSTRING%3E%26gt%3B%20request%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20final%20ExecutionContext%20context)%20%7B%0A%20%20%20%20%20%20%20%20context.getLogger().info(%22Java%20HTTP%20trigger%20processed%20a%20request.%22)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Parse%20query%20parameter%0A%20%20%20%20%20%20%20%20final%20String%20topicName%20%3D%20request.getQueryParameters().get(%22name%22)%3B%0A%20%20%20%20%20%20%20%20final%20String%20reqBody%20%3D%20request.getBody().orElse(%22%22)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Set%20properties%20used%20to%20configure%20the%20producer%0A%20%20%20%20%20%20%20%20Properties%20properties%20%3D%20new%20Properties()%3B%0A%20%20%20%20%20%20%20%20properties.setProperty(%22bootstrap.servers%22%2C%20%22%3CKAFKABROKERS%3E%22)%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20Set%20how%20to%20serialize%20key%2Fvalue%20pairs%0A%20%20%20%20%20%20%20%20properties.setProperty(%22key.serializer%22%2C%22org.apache.kafka.common.serialization.StringSerializer%22)%3B%0A%20%20%20%20%20%20%20%20properties.setProperty(%22value.serializer%22%2C%22org.apache.kafka.common.serialization.StringSerializer%22)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Create%20Kafka%20Producer%0A%20%20%20%20%20%20%20%20KafkaProducer%3CSTRING%3E%20producer%20%3D%20new%20KafkaProducer%26lt%3B%26gt%3B(properties)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Send%20message%20to%20Kafka%0A%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20producer.send(new%20ProducerRecord%3CSTRING%3E(topicName%2C%20reqBody)).get()%3B%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20catch%20(Exception%20e)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(e.getMessage())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Close%20Kafka%20producer%0A%20%20%20%20%20%20%20%20producer.flush()%3B%0A%20%20%20%20%20%20%20%20producer.close()%3B%0A%20%20%20%20%20%20%20%20return%20request.createResponseBuilder(HttpStatus.OK).body(reqBody).build()%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FKAFKABROKERS%3E%3C%2FSTRING%3E%3C%2FOPTIONAL%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20an%20example%20to%20improve%20performance%2C%20you%20can%20change%20sample%20codes%20above%20by%20sending%20messages%20as%20a%20list%20at%20once.%20This%20sample%20can%20send%20multiple%20messages%20with%20Kafka%20Producer%20connection%20as%20just%20one%20post%20data.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-java%22%3E%3CCODE%3E%20%20%20%20%20%20%20%20%2F%2F%20Create%20Kafka%20Producer%0A%20%20%20%20%20%20%20%20KafkaProducer%3CSTRING%3E%20producer%20%3D%20new%20KafkaProducer%26lt%3B%26gt%3B(properties)%3B%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Send%20message%20to%20Kafka%0A%20%20%20%20%20%20%20%20String%20jsonLine%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%20%26lt%3B%20reqBodyArray.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20try%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20producer.send(new%20ProducerRecord%3CSTRING%3E(topicName%2C%20reqBodyArray%5Bi%5D)).get()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20(Exception%20e)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(e.getMessage())%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%2F%2F%20Close%20Kafka%20producer%0A%20%20%20%20%20%20%20%20producer.flush()%3B%0A%20%20%20%20%20%20%20%20producer.close()%3B%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAzure%20Functions%20in%20this%20sample%20uses%20HTTP%20Trigger%2C%20so%20you%20can%20send%20sample%20data%20as%20HTTP%20post%20requests.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Ecurl%20-XPOST%20%7Byour%20host%7D%2Fapi%2FFunctionKafkaProducerJava%3Fname%3D%7BtopicName%7D%20-d%20'%7B%3CJSONBODY%3E%7D'%3C%2FJSONBODY%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2429394%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20has%20offered%20options%20for%20Apache%20Kafka%20as%20PaaS%20-%20%22Event%20Hubs%20for%20Kafka%22%20or%20Kafka%20cluster%20on%20HDInsight.%20Event%20Hub%20option%20is%20more%20cloud%20managed%20than%20HDInsight%20one%2C%20so%20HDInsight%20option%20is%20user%20managed%20than%20Event%20Hub%20one.%20It's%20required%20to%20create%20%22Producer%22%20sending%20data%20to%20Kafka%20and%20%22Consumer%22%20retriving%20data%20from%20Kafka%20for%20both%20options.%3CBR%20%2F%3EIn%20this%20post%2C%20you%20can%20acquire%20knowledge%20how%20to%20develop%20scalable%20Kafka%20Producer%20by%20using%20Azure%20Functions.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2429394%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20App%20Service%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EBest%20Practices%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EJava%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EServerless%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Jun 09 2021 08:39 AM
Updated by: