This page shows supported authentication methods and clients, and shows sample code you can use to connect Azure Event Hubs to other cloud services using Service Connector. You might still be able to connect to Event Hubs in other programming languages without using Service Connector. This page also shows default environment variable names and values (or Spring Boot configuration) you get when you create service connections.
Supported compute services
Service Connector can be used to connect the following compute services to Azure Event Hubs:
- Azure App Service
- Azure Container Apps
- Azure Functions
- Azure Kubernetes Service (AKS)
- Azure Spring Apps
Supported authentication types and client types
The table below shows which combinations of client types and authentication methods are supported for connecting your compute service to Azure Event Hubs using Service Connector. A “Yes” indicates that the combination is supported, while a “No” indicates that it is not supported.
Client type |
System-assigned managed identity |
User-assigned managed identity |
Secret / connection string |
Service principal |
.NET |
Yes |
Yes |
Yes |
Yes |
Go |
Yes |
Yes |
Yes |
Yes |
Java |
Yes |
Yes |
Yes |
Yes |
Java - Spring Boot |
Yes |
Yes |
Yes |
Yes |
Kafka - Spring Boot |
Yes |
Yes |
Yes |
Yes |
Node.js |
Yes |
Yes |
Yes |
Yes |
Python |
Yes |
Yes |
Yes |
Yes |
None |
Yes |
Yes |
Yes |
Yes |
This table indicates that all combinations of client types and authentication methods in the table are supported. All client types can use any of the authentication methods to connect to Azure Event Hubs using Service Connector.
Default environment variable names or application properties
Use the connection details below to connect compute services to Event Hubs. For each example below, replace the placeholder texts <Event-Hubs-namespace>
, <access-key-name>
, <access-key-value>
<client-ID>
, <client-secret>
, and <tenant-id>
with your Event Hubs namespace, shared access key name, shared access key value, client ID, client secret and tenant ID. For more information about naming conventions, check the Service Connector internals article.
System-assigned managed identity
SpringBoot client type
Default environment variable name |
Description |
Sample value |
spring.cloud.azure.eventhub.namespace |
Event Hubs namespace |
<Event-Hub-namespace>.servicebus.windows.net |
spring.cloud.azure.eventhubs.namespace |
Event Hubs namespace for Spring Cloud Azure version above 4.0 |
<Event-Hub-namespace>.servicebus.windows.net |
spring.cloud.azure.eventhubs.credential.managed-identity-enabled |
Whether to enable managed identity |
true |
Kafka-SpringBoot client type
Default environment variable name |
Description |
Sample value |
spring.kafka.bootstrap-servers |
Kafka bootstrap server |
<Event-Hub-namespace>.servicebus.windows.net |
Other client types
Default environment variable name |
Description |
Sample value |
AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE |
Event Hubs namespace |
<Event-Hubs-namespace>.servicebus.windows.net |
Sample code
Refer to the steps and code below to connect to Azure Event Hubs using a system-assigned managed identity.
Install dependencies.
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.EventHubs
Authenticate using Azure.Identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using System;
sing Azure.Identity;
using Azure.Messaging.EventHubs;
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var fullyQualifiedNamespace = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE");
var eventHubName = "<NAME OF THE EVENT HUB>";
// Example of sending events
var producer = new EventHubProducerClient(fullyQualifiedNamespace, eventHubName, credential);
Add the following dependencies in your pom.xml file:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventhubs</artifactId>
<version>5.15.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
Authenticate using azure-identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_EVENTHUB_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_EVENTHUB_CLIENTID"))
// .clientSecret(System.getenv("AZURE_EVENTHUB_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_EVENTHUB_TENANTID"))
// .build();
String namespace = System.getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE");
// Example of sending events
EventProcessorClientBuilder eventProcessorClientBuilder = new EventProcessorClientBuilder()
.consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME)
.credential(namespace, "<event-hub-name>", credential)
EventProcessorClient eventProcessorClient = eventProcessorClientBuilder.buildEventProcessorClient();
Install dependencies.
pip install azure-eventhub
pip install azure-identity
Authenticate using azure-identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import os
from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_EVENTHUB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_EVENTHUB_TENANTID')
# client_id = os.getenv('AZURE_EVENTHUB_CLIENTID')
# client_secret = os.getenv('AZURE_EVENTHUB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
namespace = os.getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE")
EVENT_HUB_NAME = "EVENT_HUB_NAME"
# Example of sending events
producer = EventHubProducerClient(
fully_qualified_namespace=namespace,
eventhub_name=EVENT_HUB_NAME,
credential=cred,
)
Install dependencies.
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
Authenticate using azidentity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs"
)
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_EVENTHUB_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_EVENTHUB_CLIENTID")
// tenantid := os.Getenv("AZURE_EVENTHUB_TENANTID")
// clientsecret := os.Getenv("AZURE_EVENTHUB_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
namespace := os.Getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE")
// Example of sending events
producerClient, err := azeventhubs.NewProducerClient(namespace, "<eventhub-name>", defaultAzureCred, nil)
if err != nil {
panic(err)
}
Install dependencies.
npm install @azure/event-hubs
npm install @azure/identity
Authenticate using @azure/identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
const { EventHubProducerClient } = require("@azure/event-hubs");
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_EVENTHUB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_EVENTHUB_TENANTID;
// const clientId = process.env.AZURE_EVENTHUB_CLIENTID;
// const clientSecret = process.env.AZURE_EVENTHUB_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const namespace = process.env.AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE;
const eventHubName = "EVENT HUB NAME";
// Example of sending events
const producer = new EventHubProducerClient(fullyQualifiedNamespace, eventHubName, credential);
For other languages, you can use the connection configuration properties that Service Connector sets to the environment variables to connect to Azure Event Hubs. For environment variable details, see Integrate Azure Event Hubs with Service Connector.
User-assigned managed identity
SpringBoot client type
Default environment variable name |
Description |
Sample value |
spring.cloud.azure.eventhub.namespace |
Event Hubs namespace |
<Event-Hub-namespace>.servicebus.windows.net |
spring.cloud.azure.client-id |
Your client ID |
<client-ID> |
spring.cloud.azure.eventhubs.namespace |
Event Hubs namespace for Spring Cloud Azure version above 4.0 |
<Event-Hub-namespace>.servicebus.windows.net |
spring.cloud.azure.eventhubs.credential.client-id |
Your client ID for Spring Cloud Azure version above 4.0 |
<client-ID> |
spring.cloud.azure.eventhubs.credential.managed-identity-enabled |
Whether to enable managed identity |
true |
Kafka-SpringBoot client type
Default environment variable name |
Description |
Sample value |
spring.kafka.bootstrap-servers |
Kafka bootstrap server |
<Event-Hub-namespace>.servicebus.windows.net |
spring.kafka.properties.azure.credential.managed-identity-enabled |
Whether to enable managed identity |
true |
spring.kafka.properties.azure.credential.client-id |
Your client ID |
<client-ID> |
Other client types
Default environment variable name |
Description |
Sample value |
AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE |
Event Hubs namespace |
<Event-Hubs-namespace>.servicebus.windows.net |
AZURE_EVENTHUB_CLIENTID |
Your client ID |
<client-ID> |
Sample code
Refer to the steps and code below to connect to Azure Event Hubs using a user-assigned managed identity.
Install dependencies.
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.EventHubs
Authenticate using Azure.Identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using System;
sing Azure.Identity;
using Azure.Messaging.EventHubs;
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var fullyQualifiedNamespace = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE");
var eventHubName = "<NAME OF THE EVENT HUB>";
// Example of sending events
var producer = new EventHubProducerClient(fullyQualifiedNamespace, eventHubName, credential);
Add the following dependencies in your pom.xml file:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventhubs</artifactId>
<version>5.15.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
Authenticate using azure-identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_EVENTHUB_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_EVENTHUB_CLIENTID"))
// .clientSecret(System.getenv("AZURE_EVENTHUB_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_EVENTHUB_TENANTID"))
// .build();
String namespace = System.getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE");
// Example of sending events
EventProcessorClientBuilder eventProcessorClientBuilder = new EventProcessorClientBuilder()
.consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME)
.credential(namespace, "<event-hub-name>", credential)
EventProcessorClient eventProcessorClient = eventProcessorClientBuilder.buildEventProcessorClient();
Install dependencies.
pip install azure-eventhub
pip install azure-identity
Authenticate using azure-identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import os
from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_EVENTHUB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_EVENTHUB_TENANTID')
# client_id = os.getenv('AZURE_EVENTHUB_CLIENTID')
# client_secret = os.getenv('AZURE_EVENTHUB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
namespace = os.getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE")
EVENT_HUB_NAME = "EVENT_HUB_NAME"
# Example of sending events
producer = EventHubProducerClient(
fully_qualified_namespace=namespace,
eventhub_name=EVENT_HUB_NAME,
credential=cred,
)
Install dependencies.
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
Authenticate using azidentity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs"
)
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_EVENTHUB_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_EVENTHUB_CLIENTID")
// tenantid := os.Getenv("AZURE_EVENTHUB_TENANTID")
// clientsecret := os.Getenv("AZURE_EVENTHUB_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
namespace := os.Getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE")
// Example of sending events
producerClient, err := azeventhubs.NewProducerClient(namespace, "<eventhub-name>", defaultAzureCred, nil)
if err != nil {
panic(err)
}
Install dependencies.
npm install @azure/event-hubs
npm install @azure/identity
Authenticate using @azure/identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
const { EventHubProducerClient } = require("@azure/event-hubs");
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_EVENTHUB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_EVENTHUB_TENANTID;
// const clientId = process.env.AZURE_EVENTHUB_CLIENTID;
// const clientSecret = process.env.AZURE_EVENTHUB_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const namespace = process.env.AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE;
const eventHubName = "EVENT HUB NAME";
// Example of sending events
const producer = new EventHubProducerClient(fullyQualifiedNamespace, eventHubName, credential);
For other languages, you can use the connection configuration properties that Service Connector sets to the environment variables to connect to Azure Event Hubs. For environment variable details, see Integrate Azure Event Hubs with Service Connector.
Connection string
Warning
Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this procedure requires a very high degree of trust in the application, and carries risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.
SpringBoot client type
Default environment variable name |
Description |
Sample value |
spring.cloud.azure.storage.connection-string |
Event Hubs connection string |
Endpoint=sb://servicelinkertesteventhub.servicebus.windows.net/;SharedAccessKeyName=<access-key-name>;SharedAccessKey=<access-key-value> |
spring.cloud.azure.eventhubs.connection-string |
Event Hubs connection string for Spring Cloud Azure version above 4.0 |
Endpoint=sb://servicelinkertesteventhub.servicebus.windows.net/;SharedAccessKeyName=<access-key-name>;SharedAccessKey=<access-key-value> |
Kafka-SpringBoot client type
Default environment variable name |
Description |
Sample value |
spring.cloud.azure.eventhubs.connection-string |
Event Hubs connection string |
Endpoint=sb://servicelinkertesteventhub.servicebus.windows.net/;SharedAccessKeyName=<access-key-name>;SharedAccessKey=<access-key-value> |
Other client types
Default environment variable name |
Description |
Sample value |
AZURE_EVENTHUB_CONNECTIONSTRING |
Event Hubs connection string |
Endpoint=sb://<Event-Hubs-namespace>.servicebus.windows.net/;SharedAccessKeyName=<access-key-name>;SharedAccessKey=<access-key-value> |
Sample code
Refer to the steps and code below to connect to Azure Event Hubs using a connection string.
Install dependency.
dotnet add package Azure.Messaging.EventHubs
Get the connection string from the environment variable added by Service Connector.
using System;
using Azure.Messaging.EventHubs;
string connectionString = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CONNECTIONSTRING");
var eventHubName = "<NAME OF THE EVENT HUB>";
var consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName;
var producer = new EventHubProducerClient(connectionString, eventHubName);
var consumer = new EventHubConsumerClient(consumerGroup, connectionString, eventHubName);
- Add the following dependency in your pom.xml file:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventhubs</artifactId>
<version>5.15.0</version>
</dependency>
- Get the connection string from the environment variable added by Service Connector.
String connectionStr = System.getenv("AZURE_EVENTHUB_CONNECTIONSTRING");
// Example of sending events
EventHubProducerAsyncClient producer = new EventHubClientBuilder()
.connectionString(
connStr,
"<event-hub-name>")
.buildAsyncProducerClient();
- Install dependency.
pip install azure-eventhub
- Get the connection string from the environment variable added by Service Connector.
import os
from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient
CONN_STR = os.environ["AZURE_EVENTHUB_CONNECTIONSTRING"]
EVENT_HUB_NAME = "EVENT_HUB_NAME"
# Example of sending events
producer = EventHubProducerClient.from_connection_string(
conn_str=EVENT_HUB_CONNECTION_STR, eventhub_name=EVENT_HUB_NAME
)
- Install dependency.
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs
- Get the connection string from the environment variable added by Service Connector.
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs"
)
connectionString := os.Getenv("AZURE_EVENTHUB_CONNECTIONSTRING")
// Example of sending events
producerClient, err := azeventhubs.NewProducerClientFromConnectionString(connectionString, "EVENT HUB NAME", nil)
- Install dependency.
npm install @azure/event-hubs
- Get the connection string from the environment variable added by Service Connector.
const { EventHubProducerClient } = require("@azure/event-hubs");
const eventHubName = "EVENT HUB NAME";
const connection_string = process.env.AZURE_EVENTHUB_CONNECTIONSTRING;
// Example of sending events
const producer = new EventHubProducerClient(connectionString, eventHubName);
For other languages, you can use the connection configuration properties that Service Connector sets to the environment variables to connect to Azure Event Hubs. For environment variable details, see Integrate Azure Event Hubs with Service Connector.
Service principal
SpringBoot client type
Default environment variable name |
Description |
Sample value |
spring.cloud.azure.eventhub.namespace |
Event Hubs namespace |
<Event-Hub-namespace>.servicebus.windows.net |
spring.cloud.azure.client-id |
Your client ID |
<client-ID> |
spring.cloud.azure.tenant-id |
Your client secret |
<client-secret> |
spring.cloud.azure.client-secret |
Your tenant ID |
<tenant-id> |
spring.cloud.azure.eventhubs.namespace |
Event Hubs namespace for Spring Cloud Azure version above 4.0 |
<Event-Hub-namespace>.servicebus.windows.net |
spring.cloud.azure.eventhubs.credential.client-id |
Your client ID for Spring Cloud Azure version above 4.0 |
<client-ID> |
spring.cloud.azure.eventhubs.credential.client-secret |
Your client secret for Spring Cloud Azure version above 4.0 |
<client-secret> |
spring.cloud.azure.eventhubs.profile.tenant-id |
Your tenant ID for Spring Cloud Azure version above 4.0 |
<tenant-id> |
Kafka-SpringBoot client type
Default environment variable name |
Description |
Sample value |
spring.kafka.bootstrap-servers |
Kafka bootstrap server |
<Event-Hub-namespace>.servicebus.windows.net |
spring.kafka.properties.azure.credential.client-id |
Your client ID |
<client-ID> |
spring.kafka.properties.azure.credential.client-secret |
Your client secret |
<client-secret> |
spring.kafka.properties.azure.profile.tenant-id |
Your tenant ID |
<tenant-id> |
Other client types
Default environment variable name |
Description |
Sample value |
AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE |
Event Hubs namespace |
<Event-Hubs-namespace>.servicebus.windows.net |
AZURE_EVENTHUB_CLIENTID |
Your client ID |
<client-ID> |
AZURE_EVENTHUB_CLIENTSECRET |
Your client secret |
<client-secret> |
AZURE_EVENTHUB_TENANTID |
Your tenant ID |
<tenant-id> |
Sample code
Refer to the steps and code below to connect to Azure Event Hubs using a service principal.
Install dependencies.
dotnet add package Azure.Identity
dotnet add package Azure.Messaging.EventHubs
Authenticate using Azure.Identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
using System;
sing Azure.Identity;
using Azure.Messaging.EventHubs;
// Uncomment the following lines corresponding to the authentication type you want to use.
// system-assigned managed identity
// var credential = new DefaultAzureCredential();
// user-assigned managed identity
// var credential = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTID");
// });
// service principal
// var tenantId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_CLIENTSECRET");
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var fullyQualifiedNamespace = Environment.GetEnvironmentVariable("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE");
var eventHubName = "<NAME OF THE EVENT HUB>";
// Example of sending events
var producer = new EventHubProducerClient(fullyQualifiedNamespace, eventHubName, credential);
Add the following dependencies in your pom.xml file:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventhubs</artifactId>
<version>5.15.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
Authenticate using azure-identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
// for user-assigned managed identity
// DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_EVENTHUB_CLIENTID"))
// .build();
// for service principal
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_EVENTHUB_CLIENTID"))
// .clientSecret(System.getenv("AZURE_EVENTHUB_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_EVENTHUB_TENANTID"))
// .build();
String namespace = System.getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE");
// Example of sending events
EventProcessorClientBuilder eventProcessorClientBuilder = new EventProcessorClientBuilder()
.consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME)
.credential(namespace, "<event-hub-name>", credential)
EventProcessorClient eventProcessorClient = eventProcessorClientBuilder.buildEventProcessorClient();
Install dependencies.
pip install azure-eventhub
pip install azure-identity
Authenticate using azure-identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import os
from azure.eventhub import EventData
from azure.eventhub.aio import EventHubProducerClient
# Uncomment the following lines corresponding to the authentication type you want to use.
# system-assigned managed identity
# cred = ManagedIdentityCredential()
# user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_EVENTHUB_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# service principal
# tenant_id = os.getenv('AZURE_EVENTHUB_TENANTID')
# client_id = os.getenv('AZURE_EVENTHUB_CLIENTID')
# client_secret = os.getenv('AZURE_EVENTHUB_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
namespace = os.getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE")
EVENT_HUB_NAME = "EVENT_HUB_NAME"
# Example of sending events
producer = EventHubProducerClient(
fully_qualified_namespace=namespace,
eventhub_name=EVENT_HUB_NAME,
credential=cred,
)
Install dependencies.
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
Authenticate using azidentity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventhubs"
)
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// for user-assigned managed identity
// clientid := os.Getenv("AZURE_EVENTHUB_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// for service principal
// clientid := os.Getenv("AZURE_EVENTHUB_CLIENTID")
// tenantid := os.Getenv("AZURE_EVENTHUB_TENANTID")
// clientsecret := os.Getenv("AZURE_EVENTHUB_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
namespace := os.Getenv("AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE")
// Example of sending events
producerClient, err := azeventhubs.NewProducerClient(namespace, "<eventhub-name>", defaultAzureCred, nil)
if err != nil {
panic(err)
}
Install dependencies.
npm install @azure/event-hubs
npm install @azure/identity
Authenticate using @azure/identity
and get the Azure Event Hubs namespace from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use.
const { EventHubProducerClient } = require("@azure/event-hubs");
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
// Uncomment the following lines corresponding to the authentication type you want to use.
// for system-assigned managed identity
// const credential = new DefaultAzureCredential();
// for user-assigned managed identity
// const clientId = process.env.AZURE_EVENTHUB_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// for service principal
// const tenantId = process.env.AZURE_EVENTHUB_TENANTID;
// const clientId = process.env.AZURE_EVENTHUB_CLIENTID;
// const clientSecret = process.env.AZURE_EVENTHUB_CLIENTSECRET;
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const namespace = process.env.AZURE_EVENTHUB_FULLYQUALIFIEDNAMESPACE;
const eventHubName = "EVENT HUB NAME";
// Example of sending events
const producer = new EventHubProducerClient(fullyQualifiedNamespace, eventHubName, credential);
For other languages, you can use the connection configuration properties that Service Connector sets to the environment variables to connect to Azure Event Hubs. For environment variable details, see Integrate Azure Event Hubs with Service Connector.
Next steps
Follow the tutorial listed below to learn more about Service Connector.