Skip to content

jpontdia/azure-eventhubs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

azure-eventhubs

Microsoft Azure Event Hubs demo with Spring Boot 2.6.3. Implemented features: Logback Appender & Event Producer

Table of contents

  1. Objectives
  2. Prerequisites
  3. Environment variables
  4. Build and test the application
  5. Reading messages from Event Hubs
  6. Recommended content

Objectives

  1. Send application logs to Azure Event Hubs implementing a Logback appender.
  2. Producing events with Spring Cloud Stream to Azure Event Hubs.

The next image depicts the architecture of the application: architecture

Prerequisites

  • An Azure subscription
  • Java Development Kit (JDK) 11
  • Apache Maven, version 3.8 or later.
  • An Event Hubs standard instance (for the use case of logging with the Kafka API)
  • 2 Event Hub's (topics), one for storing the log messages and other to store the message payloads received in the web service

Environment variables

The application requires the next environment variables:

  • EVENTHUBS_NAMESPACE. The management container for event hubs (or topics, in Kafka parlance).
  • EVENTHUBS_CONNECTION. The connection string for Event Hubs
  • EVENTHUBS_LOGGING_EVENTHUB. The name of the Event Hub used to store the logging messages

Example:

set EVENTHUBS_CONNECTION=Endpoint=sb://my-eventhubs.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=aabbccddeeffgghhhiii= 
set EVENTHUBS_NAMESPACE=my-eventhubs
set EVENTHUBS_LOGGING_EVENTHUB=logs

Getting the namespace

  1. Browse to the Azure portal at https://portal.azure.com/ and #.

  2. Select All resources, then search for Event Hubs Namespace

    namespace

Getting the connection string

  1. Browse to the Azure portal at https://portal.azure.com/ and #.
  2. Select All resources, then search for Event Hubs Namespace
  3. On the Event Hubs Namespace page, select on the left sidebar menu the option Shared access policies
  4. Select RootManageSharedAccessKey and copy the Connection string-primary key connection-string

Build and test the application

Build your Spring Boot application with Maven and run it; for example:

mvn clean package -Dmaven.test.skip=true
mvn spring-boot:run

Once your application is running, you can use CURL to test the application, for example (using line continuation character for windows):

curl --location --request POST 'http://localhost:8080/creditlimit' ^
--header 'Content-Type: application/json' ^
--header 'charset: ISO-8859-1' ^
--data-raw '{
    "accountId": "4127778000004797",
    "amount": "1000000",
    "sourceSystem": "WebCustomer",
    "messageIdentifier": "137"
}'

You should see a similar json response in the client:

{
   "updateRequest": {
      "accountId": "4127778000004797",
      "amount": "1000000",
      "sourceSystem": "WebCustomer",
      "messageIdentifier": "136"
   },
   "changeDate": "2022-01-27T10:15:43.0286947-06:00"
}

Reading messages from Event Hubs

Using VisualStudio Code We can get the messages sent to the logging Event Hub. Install the Azure Event Hub Explorer: extension

Configure the Event Hub connection. From the command palette (Ctrl-Shift-P) select the operation:

EventHub: Select Event Hub

Then command will ask for:

  • The Azure subscription
  • The resource group
  • The namespace
  • The event hub

Monitor the incoming message sent to Event Hub. From the command palette (Ctrl-Shift-P) select:

EventHub: Start Monitoring Event Hub Message

Event Hubs explorer

Test the application, and you should see the messages in VisualStudio Code: Event Hubs explorer results

The logging messages must be the same as the spring console: Spring boot console

Recommended content