What are Connection Strings?
Connection strings are the credentials your applications use to authenticate with Azure Event Hubs. They contain essential information like the endpoint, shared access key name, and the primary or secondary key. Think of them as the "keys to the kingdom" for your Event Hubs namespace.
Where to Find Your Connection String
You can find your Event Hubs connection string in the Azure portal:
- Navigate to your Event Hubs namespace.
- In the left-hand menu, under Settings, select Shared access policies.
- Click on an existing policy (e.g., RootManageSharedAccessKey) or create a new one with appropriate permissions.
- The connection string will be displayed on the policy details page. You can copy the Primary Connection String or Secondary Connection String.
Connection String Components
A typical Event Hubs connection string looks like this:
Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=your-key-name;SharedAccessKey=your-secret-key
Let's break down the components:
- Endpoint: The fully qualified domain name (FQDN) of your Event Hubs namespace.
- SharedAccessKeyName: The name of the shared access policy that grants the necessary permissions.
- SharedAccessKey: The secret key associated with the specified policy name.
Using Connection Strings in Your Code
You'll typically configure your application to read the connection string from environment variables or a configuration file. Here's a conceptual example of how you might use it with an Event Hubs client library (syntax may vary by language):
// Example in C# using Azure.Messaging.EventHubs
using Azure.Messaging.EventHubs;
string connectionString = Environment.GetEnvironmentVariable("EVENTHUB_CONNECTION_STRING");
string eventHubName = "your-eventhub-name";
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
// Send events...
}
Primary vs. Secondary Keys
Your shared access policies come with both a primary and a secondary key. It's a good security practice to use the secondary key for your application and keep the primary key as a backup. If you ever need to rotate your keys, you can update your application to use the primary key while you regenerate the secondary key, minimizing downtime.
Next Steps
Now that you understand connection strings, you're ready to start sending and receiving events. Explore the API Reference for detailed information on client libraries and explore how to send your first events.