From 703a1c0f7c2a6a3cebdb8d748a01e34fc91d2a5f Mon Sep 17 00:00:00 2001 From: Muyao Feng <92105726+Netyyyy@users.noreply.github.com> Date: Fri, 12 Jul 2024 16:38:24 +0800 Subject: [PATCH] Add samples for Spring messaging (#750) * add spring messaging azure storage queue sample * add spring messaging azure servicebus sample * add spring messaging azure eh sample --- .../eventhubs-spring-messaging/README.md | 203 ++++++++++++++++++ .../eventhubs-spring-messaging/pom.xml | 35 +++ .../eventhubs/messaging/ConsumerService.java | 20 ++ .../EventHubMessagingApplication.java | 34 +++ .../src/main/resources/application.yaml | 9 + .../terraform/main.tf | 114 ++++++++++ .../terraform/outputs.tf | 15 ++ .../terraform/setup_env.ps1 | 7 + .../terraform/setup_env.sh | 7 + .../terraform/variables.tf | 17 ++ pom.xml | 6 + .../servicebus-spring-messaging/README.md | 194 +++++++++++++++++ .../servicebus-spring-messaging/pom.xml | 31 +++ .../servicebus/messaging/ConsumerService.java | 20 ++ .../ServiceBusMessagingApplication.java | 26 +++ .../src/main/resources/application.yaml | 6 + .../terraform/main.tf | 77 +++++++ .../terraform/outputs.tf | 4 + .../terraform/setup_env.ps1 | 3 + .../terraform/setup_env.sh | 3 + .../terraform/variables.tf | 23 ++ .../storage-queue-spring-messaging/README.md | 192 +++++++++++++++++ .../storage-queue-spring-messaging/pom.xml | 31 +++ .../StorageQueueMessagingApplication.java | 41 ++++ .../src/main/resources/application.yml | 6 + .../terraform/main.tf | 71 ++++++ .../terraform/outputs.tf | 4 + .../terraform/setup_env.ps1 | 3 + .../terraform/setup_env.sh | 3 + .../terraform/variables.tf | 17 ++ 30 files changed, 1222 insertions(+) create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/README.md create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/pom.xml create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/ConsumerService.java create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/EventHubMessagingApplication.java create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/resources/application.yaml create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/main.tf create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/outputs.tf create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/setup_env.ps1 create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/setup_env.sh create mode 100644 eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/variables.tf create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/README.md create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/pom.xml create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/java/com/azure/spring/sample/servicebus/messaging/ConsumerService.java create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/java/com/azure/spring/sample/servicebus/messaging/ServiceBusMessagingApplication.java create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/resources/application.yaml create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/main.tf create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/outputs.tf create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/setup_env.ps1 create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/setup_env.sh create mode 100644 servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/variables.tf create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/README.md create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/pom.xml create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/src/main/java/com/azure/spring/sample/storage/queue/messaging/StorageQueueMessagingApplication.java create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/src/main/resources/application.yml create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/main.tf create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/outputs.tf create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/setup_env.ps1 create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/setup_env.sh create mode 100644 storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/variables.tf diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/README.md b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/README.md new file mode 100644 index 000000000..e02a58f98 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/README.md @@ -0,0 +1,203 @@ +--- +page_type: sample +languages: +- java +products: +- azure-service-bus +name: Sending and Receiving Message by Spring Messaging Azure Event Hubs in Spring Boot Application +description: This sample demonstrates how to send and receive message by Spring Messaging Event Hubs in Spring Boot application. +--- + +# Sending and Receiving Message by Spring Messaging Azure Event Hubs in Spring Boot Application + +This code sample demonstrates how to use Spring Messaging Azure Event Hubs sending and receiving Message. + +## What You Will Build +You will build an application using `EventHubsTemplate` send messages and `@EventHubsListener` receive messages for Azure Event Hubs. + +## What You Need + +- [An Azure subscription](https://azure.microsoft.com/free/) +- [Terraform](https://www.terraform.io/) +- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) +- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later +- Maven +- You can also import the code straight into your IDE: + - [IntelliJ IDEA](https://www.jetbrains.com/idea/download) + +## Provision Azure Resources Required to Run This Sample +This sample will create Azure resources using Terraform. If you choose to run it without using Terraform to provision resources, please pay attention to: +> [!IMPORTANT] +> If you choose to use a security principal to authenticate and authorize with Microsoft Entra ID for accessing an Azure resource +> please refer to [Authorize access with Microsoft Entra ID](https://learn.microsoft.com/azure/developer/java/spring-framework/authentication#authorize-access-with-microsoft-entra-id) to make sure the security principal has been granted the sufficient permission to access the Azure resource. + +### Authenticate Using the Azure CLI +Terraform must authenticate to Azure to create infrastructure. + +In your terminal, use the Azure CLI tool to setup your account permissions locally. + +```shell +az login +``` + +Your browser window will open and you will be prompted to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. You do not need to save this output as it is saved in your system for Terraform to use. + +```shell +You have logged in. Now let us find all the subscriptions to which you have access... + +[ + { + "cloudName": "AzureCloud", + "homeTenantId": "home-Tenant-Id", + "id": "subscription-id", + "isDefault": true, + "managedByTenants": [], + "name": "Subscription-Name", + "state": "Enabled", + "tenantId": "0envbwi39-TenantId", + "user": { + "name": "your-username@domain.com", + "type": "user" + } + } +] +``` + +If you have more than one subscription, specify the subscription-id you want to use with command below: +```shell +az account set --subscription +``` + +### Provision the Resources + +After login Azure CLI with your account, now you can use the terraform script to create Azure Resources. + +#### Run with Bash + +```shell +# In the root directory of the sample +# Initialize your Terraform configuration +terraform -chdir=./terraform init + +# Apply your Terraform Configuration +terraform -chdir=./terraform apply -auto-approve + +``` + +#### Run with Powershell + +```shell +# In the root directory of the sample +# Initialize your Terraform configuration +terraform -chdir=terraform init + +# Apply your Terraform Configuration +terraform -chdir=terraform apply -auto-approve + +``` + +It may take a few minutes to run the script. After successful running, you will see prompt information like below: + +```shell +azurerm_resource_group.main: Creating... +azurerm_resource_group.main: Creation complete after 3s ... +azurerm_storage_account.storage_account: Creating... +azurerm_eventhub_namespace.eventhubs_namespace: Still creating... [10s elapsed] +... +azurerm_storage_account.storage_account: Creation complete after 38s ... +azurerm_storage_container.storage_container: Creating... +azurerm_role_assignment.role_storage_account_contributor: Creating... +azurerm_storage_container.storage_container: Creation complete after 1s ... +azurerm_role_assignment.role_storage_blob_data_owner: Creating... +... +azurerm_role_assignment.role_storage_blob_data_owner: Creation complete after 25s ... +azurerm_role_assignment.role_storage_account_contributor: Creation complete after 29s ... +... +azurerm_eventhub_namespace.eventhubs_namespace: Creation complete after 1m23s ... +azurerm_eventhub.eventhubs: Creating... +azurerm_eventhub.eventhubs: Creation complete after 7s ... +azurerm_role_assignment.role_eventhubs_data_owner: Creating... +... +azurerm_role_assignment.role_eventhubs_data_owner: Creation complete after 24s ... + +Apply complete! Resources: 11 added, 0 changed, 0 destroyed. + +Outputs: +... + +``` + +You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created. + +### Export Output to Your Local Environment +Running the command below to export environment values: + +#### Run with Bash + +```shell +source ./terraform/setup_env.sh +``` + +#### Run with Powershell + +```shell +terraform\setup_env.ps1 +``` + +If you want to run the sample in debug mode, you can save the output value. + +```shell +AZURE_EVENTHUBS_NAMESPACE=... +AZURE_STORAGE_CONTAINER_NAME=... +AZURE_STORAGE_ACCOUNT_NAME=... +``` + +## Run Locally + +### Run the sample with Maven + +In your terminal, run `mvn clean spring-boot:run`. + +```shell +mvn clean spring-boot:run +``` + +### Run the sample in IDEs + +You can debug your sample by adding the saved output values to the tool's environment variables or the sample's `application.yaml` file. + +* If your tool is `IDEA`, please refer to [Debug your first Java application](https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html) and [add environment variables](https://www.jetbrains.com/help/objc/add-environment-variables-and-program-arguments.html#add-environment-variables). + +* If your tool is `ECLIPSE`, please refer to [Debugging the Eclipse IDE for Java Developers](https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php) and [Eclipse Environment Variable Setup](https://examples.javacodegeeks.com/desktop-java/ide/eclipse/eclipse-environment-variable-setup-example/). + +## Verify This Sample + +1. Verify in your app's logs that a similar message was posted: + ``` + [ main] c.a.s.s.e.m.EventHubMessagingApplication : Sending a message to the Event Hubs. + ... + New message received: Hello world + ``` + + +## Clean Up Resources +After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing. + +The terraform destroy command terminates resources managed by your Terraform project. +To destroy the resources you created. + +#### Run with Bash + +```shell +terraform -chdir=./terraform destroy -auto-approve +``` + +#### Run with Powershell + +```shell +terraform -chdir=terraform destroy -auto-approve +``` + +## Deploy to Azure Spring Apps + +Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/pom.xml b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/pom.xml new file mode 100644 index 000000000..70bb23e94 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/pom.xml @@ -0,0 +1,35 @@ + + + + 4.0.0 + + + com.azure.spring + azure-spring-boot-samples + 1.0.0 + ../../../pom.xml + + + eventhubs-spring-messaging + 1.0.0 + jar + + Communicate to Azure Event Hubs via Spring messaging + + + + com.azure.spring + spring-cloud-azure-starter + + + com.azure.spring + spring-messaging-azure-eventhubs + + + com.azure + azure-messaging-eventhubs-checkpointstore-blob + + + diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/ConsumerService.java b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/ConsumerService.java new file mode 100644 index 000000000..f62bc28a6 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/ConsumerService.java @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.eventhubs.messaging; + +import com.azure.spring.messaging.eventhubs.implementation.core.annotation.EventHubsListener; +import org.springframework.stereotype.Service; + +@Service +public class ConsumerService { + + private static final String EVENT_HUB_NAME = "eh1"; + private static final String CONSUMER_GROUP = "$Default"; + + @EventHubsListener(destination = EVENT_HUB_NAME, group = CONSUMER_GROUP) + public void handleMessageFromEventHub(String message) { + System.out.printf("New message received: %s%n", message); + } + +} diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/EventHubMessagingApplication.java b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/EventHubMessagingApplication.java new file mode 100644 index 000000000..08fc66e15 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/java/com/azure/spring/sample/eventhubs/messaging/EventHubMessagingApplication.java @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.eventhubs.messaging; + +import com.azure.spring.messaging.eventhubs.core.EventHubsTemplate; +import com.azure.spring.messaging.implementation.annotation.EnableAzureMessaging; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.messaging.support.MessageBuilder; + +import java.util.concurrent.TimeUnit; + +@SpringBootApplication +@EnableAzureMessaging +public class EventHubMessagingApplication { + private static final String EVENT_HUB_NAME = "eh1"; + private static final Logger LOGGER = LoggerFactory.getLogger(EventHubMessagingApplication.class); + + + public static void main(String[] args) throws InterruptedException { + ConfigurableApplicationContext applicationContext = SpringApplication.run(EventHubMessagingApplication.class); + EventHubsTemplate eventHubsTemplate = applicationContext.getBean(EventHubsTemplate.class); + TimeUnit.SECONDS.sleep(10); + LOGGER.info("Sending a message to the Event Hubs."); + eventHubsTemplate.sendAsync(EVENT_HUB_NAME, MessageBuilder.withPayload("Hello world").build()).subscribe(); + + } + +} diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/resources/application.yaml b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/resources/application.yaml new file mode 100644 index 000000000..5ea21b8e2 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/src/main/resources/application.yaml @@ -0,0 +1,9 @@ +spring: + cloud: + azure: + eventhubs: + namespace: ${AZURE_EVENTHUBS_NAMESPACE} + processor: + checkpoint-store: + container-name: ${AZURE_STORAGE_CONTAINER_NAME} + account-name: ${AZURE_STORAGE_ACCOUNT_NAME} diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/main.tf b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/main.tf new file mode 100644 index 000000000..9ea85a693 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/main.tf @@ -0,0 +1,114 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "3.9.0" + } + azurecaf = { + source = "aztfmod/azurecaf" + version = "1.2.16" + } + } +} + +provider "azurerm" { + features {} +} + +resource "azurecaf_name" "resource_group" { + name = var.application_name + resource_type = "azurerm_resource_group" + random_length = 5 + clean_input = true +} + +resource "azurerm_resource_group" "main" { + name = azurecaf_name.resource_group.result + location = var.location + + tags = { + terraform = "true" + application-name = var.application_name + spring-cloud-azure-sample = var.sample_tag_value + } +} + +data "azurerm_client_config" "current" { +} + +# =================== eventhubs ================ +resource "azurecaf_name" "azurecaf_name_eventhubs" { + name = var.application_name + resource_type = "azurerm_eventhub_namespace" + random_length = 5 + clean_input = true +} + +resource "azurerm_eventhub_namespace" "eventhubs_namespace" { + name = azurecaf_name.azurecaf_name_eventhubs.result + location = var.location + resource_group_name = azurerm_resource_group.main.name + sku = "Standard" + capacity = 1 + + tags = { + terraform = "true" + application-name = var.application_name + spring-cloud-azure-sample = var.sample_tag_value + } +} + +resource "azurerm_eventhub" "eventhubs" { + name = "eh1" + namespace_name = azurerm_eventhub_namespace.eventhubs_namespace.name + resource_group_name = azurerm_resource_group.main.name + partition_count = 2 + message_retention = 1 +} + +resource "azurerm_role_assignment" "role_eventhubs_data_owner" { + scope = azurerm_eventhub.eventhubs.id + role_definition_name = "Azure Event Hubs Data Owner" + principal_id = data.azurerm_client_config.current.object_id +} + + +# =================== storage ================ +resource "azurecaf_name" "azurecaf_name_storage_account" { + name = var.application_name + resource_type = "azurerm_storage_account" + random_length = 5 + clean_input = true +} + +resource "azurerm_storage_account" "storage_account" { + name = azurecaf_name.azurecaf_name_storage_account.result + resource_group_name = azurerm_resource_group.main.name + location = var.location + account_tier = "Standard" + account_replication_type = "LRS" + + tags = { + "spring-cloud-azure-sample" = var.sample_tag_value + "terraform" = "true" + "application-name" = var.application_name + } +} + +resource "azurerm_storage_container" "storage_container" { + name = "eventhubs-integration-sample" + storage_account_name = azurerm_storage_account.storage_account.name + container_access_type = "container" +} + +resource "azurerm_role_assignment" "role_storage_account_contributor" { + scope = azurerm_storage_account.storage_account.id + role_definition_name = "Storage Account Contributor" + principal_id = data.azurerm_client_config.current.object_id +} + +resource "azurerm_role_assignment" "role_storage_blob_data_owner" { + scope = azurerm_storage_container.storage_container.resource_manager_id + role_definition_name = "Storage Blob Data Owner" + principal_id = data.azurerm_client_config.current.object_id +} diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/outputs.tf b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/outputs.tf new file mode 100644 index 000000000..544cc58b5 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/outputs.tf @@ -0,0 +1,15 @@ +output "AZURE_EVENTHUBS_NAMESPACE" { + value = azurerm_eventhub_namespace.eventhubs_namespace.name + description = "The event hubs namespace." +} + +output "AZURE_STORAGE_ACCOUNT_NAME" { + value = azurerm_storage_account.storage_account.name + description = "The storage account name." +} + +output "AZURE_STORAGE_CONTAINER_NAME" { + value = azurerm_storage_container.storage_container.name + description = "The container name created in storage account." +} + diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/setup_env.ps1 b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/setup_env.ps1 new file mode 100644 index 000000000..10c7e8fc3 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/setup_env.ps1 @@ -0,0 +1,7 @@ +$env:AZURE_EVENTHUBS_NAMESPACE=$(terraform -chdir=terraform output -raw AZURE_EVENTHUBS_NAMESPACE) +$env:AZURE_STORAGE_CONTAINER_NAME=$(terraform -chdir=terraform output -raw AZURE_STORAGE_CONTAINER_NAME) +$env:AZURE_STORAGE_ACCOUNT_NAME=$(terraform -chdir=terraform output -raw AZURE_STORAGE_ACCOUNT_NAME) + +echo AZURE_EVENTHUBS_NAMESPACE=$env:AZURE_EVENTHUBS_NAMESPACE +echo AZURE_STORAGE_CONTAINER_NAME=$env:AZURE_STORAGE_CONTAINER_NAME +echo AZURE_STORAGE_ACCOUNT_NAME=$env:AZURE_STORAGE_ACCOUNT_NAME diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/setup_env.sh b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/setup_env.sh new file mode 100644 index 000000000..9298977ed --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/setup_env.sh @@ -0,0 +1,7 @@ +export AZURE_EVENTHUBS_NAMESPACE=$(terraform -chdir=./terraform output -raw AZURE_EVENTHUBS_NAMESPACE) +export AZURE_STORAGE_CONTAINER_NAME=$(terraform -chdir=./terraform output -raw AZURE_STORAGE_CONTAINER_NAME) +export AZURE_STORAGE_ACCOUNT_NAME=$(terraform -chdir=./terraform output -raw AZURE_STORAGE_ACCOUNT_NAME) + +echo AZURE_EVENTHUBS_NAMESPACE=$AZURE_EVENTHUBS_NAMESPACE +echo AZURE_STORAGE_CONTAINER_NAME=$AZURE_STORAGE_CONTAINER_NAME +echo AZURE_STORAGE_ACCOUNT_NAME=$AZURE_STORAGE_ACCOUNT_NAME diff --git a/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/variables.tf b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/variables.tf new file mode 100644 index 000000000..4f3843219 --- /dev/null +++ b/eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging/terraform/variables.tf @@ -0,0 +1,17 @@ +variable "application_name" { + type = string + description = "The name of your application." + default = "eventhubs" +} + +variable "location" { + type = string + description = "The Azure region where all resources in this example should be created." + default = "eastus" +} + +variable "sample_tag_value" { + type = string + description = "The value of spring-cloud-azure-sample tag." + default = "true" +} diff --git a/pom.xml b/pom.xml index 40884e0d1..95b9b02bb 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,7 @@ eventhubs/spring-cloud-azure-starter-eventhubs/eventhubs-client eventhubs/spring-cloud-azure-stream-binder-eventhubs/eventhubs-binder eventhubs/spring-cloud-azure-stream-binder-eventhubs/eventhubs-multibinders + eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging keyvault/azure-securtiy-keyvault-jca/run-with-command-line-server-side keyvault/azure-securtiy-keyvault-jca/run-with-command-line-client-side keyvault/azure-spring-boot-starter-keyvault-certificates/keyvault-certificates-client-side @@ -108,11 +109,13 @@ servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder-arm servicebus/spring-cloud-azure-starter-integration-servicebus/multiple-namespaces servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace + servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample storage/spring-cloud-azure-starter-storage-file-share/storage-file-sample storage/spring-cloud-azure-starter-integration-storage-queue/storage-queue-operation storage/spring-cloud-azure-starter-integration-storage-queue/storage-queue-integration storage/spring-cloud-azure-starter-storage-queue/storage-queue-client + storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging spring-petclinic-microservices @@ -166,6 +169,7 @@ eventhubs/spring-cloud-azure-starter-eventhubs/eventhubs-client eventhubs/spring-cloud-azure-stream-binder-eventhubs/eventhubs-binder eventhubs/spring-cloud-azure-stream-binder-eventhubs/eventhubs-multibinders + eventhubs/spring-messaging-azure-eventhubs/eventhubs-spring-messaging keyvault/spring-cloud-azure-starter-keyvault-secrets/property-source keyvault/spring-cloud-azure-starter-keyvault-secrets/secret-client servicebus/spring-cloud-azure-starter-servicebus/servicebus-queue @@ -178,11 +182,13 @@ servicebus/spring-cloud-azure-stream-binder-servicebus/servicebus-queue-binder-arm servicebus/spring-cloud-azure-starter-integration-servicebus/multiple-namespaces servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace + servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample storage/spring-cloud-azure-starter-storage-file-share/storage-file-sample storage/spring-cloud-azure-starter-integration-storage-queue/storage-queue-operation storage/spring-cloud-azure-starter-integration-storage-queue/storage-queue-integration storage/spring-cloud-azure-starter-storage-queue/storage-queue-client + storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging 17 diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/README.md b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/README.md new file mode 100644 index 000000000..f76ef181f --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/README.md @@ -0,0 +1,194 @@ +--- +page_type: sample +languages: +- java +products: +- azure-service-bus +name: Sending and Receiving Message by Spring Messaging Azure Service Bus (Queue) in Spring Boot Application +description: This sample demonstrates how to send and receive message by Spring Messaging Azure Service Bus (queue) in Spring Boot application. +--- + +# Sending and Receiving Message by Spring Messaging Azure Service Bus (Queue) in Spring Boot Application + +This code sample demonstrates how to use Spring Messaging Azure Service Bus (queue) sending and receiving Message. + +## What You Will Build +You will build an application using `ServiceBusTemplate` send messages and `@ServiceBusListener` receive messages for Azure Service Bus Queue. + +## What You Need + +- [An Azure subscription](https://azure.microsoft.com/free/) +- [Terraform](https://www.terraform.io/) +- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) +- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later +- Maven +- You can also import the code straight into your IDE: + - [IntelliJ IDEA](https://www.jetbrains.com/idea/download) + +## Provision Azure Resources Required to Run This Sample +This sample will create Azure resources using Terraform. If you choose to run it without using Terraform to provision resources, please pay attention to: +> [!IMPORTANT] +> If you choose to use a security principal to authenticate and authorize with Microsoft Entra ID for accessing an Azure resource +> please refer to [Authorize access with Microsoft Entra ID](https://learn.microsoft.com/azure/developer/java/spring-framework/authentication#authorize-access-with-microsoft-entra-id) to make sure the security principal has been granted the sufficient permission to access the Azure resource. + +### Authenticate Using the Azure CLI +Terraform must authenticate to Azure to create infrastructure. + +In your terminal, use the Azure CLI tool to setup your account permissions locally. + +```shell +az login +``` + +Your browser window will open and you will be prompted to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. You do not need to save this output as it is saved in your system for Terraform to use. + +```shell +You have logged in. Now let us find all the subscriptions to which you have access... + +[ + { + "cloudName": "AzureCloud", + "homeTenantId": "home-Tenant-Id", + "id": "subscription-id", + "isDefault": true, + "managedByTenants": [], + "name": "Subscription-Name", + "state": "Enabled", + "tenantId": "0envbwi39-TenantId", + "user": { + "name": "your-username@domain.com", + "type": "user" + } + } +] +``` + +If you have more than one subscription, specify the subscription-id you want to use with command below: +```shell +az account set --subscription +``` + +### Provision the Resources + +After login Azure CLI with your account, now you can use the terraform script to create Azure Resources. + +#### Run with Bash + +```shell +# In the root directory of the sample +# Initialize your Terraform configuration +terraform -chdir=./terraform init + +# Apply your Terraform Configuration +terraform -chdir=./terraform apply -auto-approve + +``` + +#### Run with Powershell + +```shell +# In the root directory of the sample +# Initialize your Terraform configuration +terraform -chdir=terraform init + +# Apply your Terraform Configuration +terraform -chdir=terraform apply -auto-approve + +``` + +It may take a few minutes to run the script. After successful running, you will see prompt information like below: + +```shell +azurecaf_name.azurecaf_name_servicebus: Creating... +azurecaf_name.resource_group: Creating... +azurecaf_name.resource_group: Creation complete ... +azurecaf_name.azurecaf_name_servicebus: Creation complete ... +azurerm_resource_group.main: Creating... +azurerm_resource_group.main: Creation complete ... +azurerm_servicebus_namespace.servicebus_namespace: Creation complete ... +... +azurerm_role_assignment.role_servicebus_data_owner: Creating... +azurerm_servicebus_queue.queue: Creating... +azurerm_servicebus_queue.queue: Creation complete ... +azurerm_role_assignment.role_servicebus_data_owner: Creation complete ... + +Apply complete! Resources: 6 added, 0 changed, 0 destroyed. + +Outputs: + +... + +``` + +You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created. + +### Export Output to Your Local Environment +Running the command below to export environment values: + +#### Run with Bash + +```shell +source ./terraform/setup_env.sh +``` + +#### Run with Powershell + +```shell +terraform\setup_env.ps1 +``` + +If you want to run the sample in debug mode, you can save the output value. + +```shell +AZURE_SERVICEBUS_NAMESPACE=... +``` + +## Run Locally + +### Run the sample with Maven + +In your terminal, run `mvn clean spring-boot:run`. + +```shell +mvn clean spring-boot:run +``` + +### Run the sample in IDEs + +You can debug your sample by adding the saved output values to the tool's environment variables or the sample's `application.yaml` file. + +* If your tool is `IDEA`, please refer to [Debug your first Java application](https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html) and [add environment variables](https://www.jetbrains.com/help/objc/add-environment-variables-and-program-arguments.html#add-environment-variables). + +* If your tool is `ECLIPSE`, please refer to [Debugging the Eclipse IDE for Java Developers](https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php) and [Eclipse Environment Variable Setup](https://examples.javacodegeeks.com/desktop-java/ide/eclipse/eclipse-environment-variable-setup-example/). + +## Verify This Sample + +1. Verify in your app's logs that a similar message was posted: + ``` + Sending a message to the queue. + ... + Consume message: Hello world + ``` + + +## Clean Up Resources +After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing. + +The terraform destroy command terminates resources managed by your Terraform project. +To destroy the resources you created. + +#### Run with Bash + +```shell +terraform -chdir=./terraform destroy -auto-approve +``` + +#### Run with Powershell + +```shell +terraform -chdir=terraform destroy -auto-approve +``` + +## Deploy to Azure Spring Apps + +Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/pom.xml b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/pom.xml new file mode 100644 index 000000000..24d40bf34 --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/pom.xml @@ -0,0 +1,31 @@ + + + + 4.0.0 + + + com.azure.spring + azure-spring-boot-samples + 1.0.0 + ../../../pom.xml + + + servicebus-spring-messaging + 1.0.0 + jar + + Communicate to Azure Service Bus via Spring messaging + + + + com.azure.spring + spring-messaging-azure-servicebus + + + com.azure.spring + spring-cloud-azure-starter + + + diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/java/com/azure/spring/sample/servicebus/messaging/ConsumerService.java b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/java/com/azure/spring/sample/servicebus/messaging/ConsumerService.java new file mode 100644 index 000000000..d2d818667 --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/java/com/azure/spring/sample/servicebus/messaging/ConsumerService.java @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.servicebus.messaging; + + +import com.azure.spring.messaging.servicebus.implementation.core.annotation.ServiceBusListener; +import org.springframework.stereotype.Service; + +@Service +public class ConsumerService { + + private static final String QUEUE_NAME = "que001"; + + @ServiceBusListener(destination = QUEUE_NAME) + public void handleMessageFromServiceBus(String message) { + System.out.printf("Consume message: %s%n", message); + } + +} diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/java/com/azure/spring/sample/servicebus/messaging/ServiceBusMessagingApplication.java b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/java/com/azure/spring/sample/servicebus/messaging/ServiceBusMessagingApplication.java new file mode 100644 index 000000000..fac86fac7 --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/java/com/azure/spring/sample/servicebus/messaging/ServiceBusMessagingApplication.java @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.servicebus.messaging; + + +import com.azure.spring.messaging.implementation.annotation.EnableAzureMessaging; +import com.azure.spring.messaging.servicebus.core.ServiceBusTemplate; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.messaging.support.MessageBuilder; + +@SpringBootApplication +@EnableAzureMessaging +public class ServiceBusMessagingApplication { + private static final String QUEUE_NAME = "que001"; + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ServiceBusMessagingApplication.class); + ServiceBusTemplate serviceBusTemplate = applicationContext.getBean(ServiceBusTemplate.class); + System.out.println("Sending a message to the queue."); + serviceBusTemplate.sendAsync(QUEUE_NAME, MessageBuilder.withPayload("Hello world").build()).subscribe(); + } + +} diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/resources/application.yaml b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/resources/application.yaml new file mode 100644 index 000000000..1d7d3fcb5 --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/src/main/resources/application.yaml @@ -0,0 +1,6 @@ +spring: + cloud: + azure: + servicebus: + namespace: ${AZURE_SERVICEBUS_NAMESPACE} + entity-type: queue diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/main.tf b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/main.tf new file mode 100644 index 000000000..8b86c1288 --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/main.tf @@ -0,0 +1,77 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "3.9.0" + } + azurecaf = { + source = "aztfmod/azurecaf" + version = "1.2.16" + } + } +} + +provider "azurerm" { + features {} +} + +resource "azurecaf_name" "resource_group" { + name = var.application_name + resource_type = "azurerm_resource_group" + random_length = 5 + clean_input = true +} + +resource "azurerm_resource_group" "main" { + name = azurecaf_name.resource_group.result + location = var.location + + tags = { + "terraform" = "true" + "application-name" = var.application_name + "spring-cloud-azure-sample" = var.sample_tag_value + } +} + +resource "azurecaf_name" "azurecaf_name_servicebus" { + name = var.application_name + resource_type = "azurerm_servicebus_namespace" + random_length = 5 + clean_input = true +} + +resource "azurerm_servicebus_namespace" "servicebus_namespace" { + name = azurecaf_name.azurecaf_name_servicebus.result + location = var.location + resource_group_name = azurerm_resource_group.main.name + + sku = var.pricing_tier + zone_redundant = false + + tags = { + terraform = "true" + application-name = var.application_name + spring-cloud-azure-sample = var.sample_tag_value + } +} + +resource "azurerm_servicebus_queue" "queue" { + name = "que001" + namespace_id = azurerm_servicebus_namespace.servicebus_namespace.id + + enable_partitioning = false + max_delivery_count = 10 + lock_duration = "PT30S" + max_size_in_megabytes = 1024 + requires_session = false + default_message_ttl = "P14D" +} + +data "azurerm_client_config" "current" { +} + +resource "azurerm_role_assignment" "role_servicebus_data_owner" { + scope = azurerm_servicebus_namespace.servicebus_namespace.id + role_definition_name = "Azure Service Bus Data Owner" + principal_id = data.azurerm_client_config.current.object_id +} diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/outputs.tf b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/outputs.tf new file mode 100644 index 000000000..2735b8573 --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/outputs.tf @@ -0,0 +1,4 @@ +output "AZURE_SERVICEBUS_NAMESPACE" { + value = azurerm_servicebus_namespace.servicebus_namespace.name + description = "The servicebus namespace." +} diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/setup_env.ps1 b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/setup_env.ps1 new file mode 100644 index 000000000..fb06a3ced --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/setup_env.ps1 @@ -0,0 +1,3 @@ +$env:AZURE_SERVICEBUS_NAMESPACE=$(terraform -chdir=terraform output -raw AZURE_SERVICEBUS_NAMESPACE) + +echo AZURE_SERVICEBUS_NAMESPACE=$env:AZURE_SERVICEBUS_NAMESPACE diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/setup_env.sh b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/setup_env.sh new file mode 100644 index 000000000..68fd03675 --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/setup_env.sh @@ -0,0 +1,3 @@ +export AZURE_SERVICEBUS_NAMESPACE=$(terraform -chdir=./terraform output -raw AZURE_SERVICEBUS_NAMESPACE) + +echo AZURE_SERVICEBUS_NAMESPACE=$AZURE_SERVICEBUS_NAMESPACE diff --git a/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/variables.tf b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/variables.tf new file mode 100644 index 000000000..9bf9e2774 --- /dev/null +++ b/servicebus/spring-messaging-azure-servicebus/servicebus-spring-messaging/terraform/variables.tf @@ -0,0 +1,23 @@ +variable "application_name" { + type = string + description = "The name of your application." + default = "servicebus-queue" +} + +variable "location" { + type = string + description = "The Azure region where all resources in this example should be created." + default = "eastus" +} + +variable "sample_tag_value" { + type = string + description = "The value of spring-cloud-azure-sample tag." + default = "true" +} + +variable "pricing_tier" { + type = string + description = "The pricing tier of Service Bus." + default = "Standard" +} diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/README.md b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/README.md new file mode 100644 index 000000000..b962ac0af --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/README.md @@ -0,0 +1,192 @@ +--- +page_type: sample +languages: +- java +products: +- azure-queue-storage +name: Sending and Receiving Message by Spring Messaging Azure Storage Queue in Spring Boot Application +description: This sample demonstrates how to send and receive message by Spring Messaging Azure Storage Queue in Spring Boot application. +--- + +# Sending and Receiving Message by Spring Messaging Azure Storage Queue in Spring Boot Application + +This code sample demonstrates how to use Spring Messaging Azure Storage Queue sending and receiving Message. + +## What You Will Build +You will build an application using `StorageQueueTemplate` to send and receive messages. + +## What You Need + +- [An Azure subscription](https://azure.microsoft.com/free/) +- [Terraform](https://www.terraform.io/) +- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli) +- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later +- Maven +- You can also import the code straight into your IDE: + - [IntelliJ IDEA](https://www.jetbrains.com/idea/download) + +## Provision Azure Resources Required to Run This Sample +This sample will create Azure resources using Terraform. If you choose to run it without using Terraform to provision resources, please pay attention to: +> [!IMPORTANT] +> If you choose to use a security principal to authenticate and authorize with Microsoft Entra ID for accessing an Azure resource +> please refer to [Authorize access with Microsoft Entra ID](https://learn.microsoft.com/azure/developer/java/spring-framework/authentication#authorize-access-with-microsoft-entra-id) to make sure the security principal has been granted the sufficient permission to access the Azure resource. + +### Authenticate Using the Azure CLI +Terraform must authenticate to Azure to create infrastructure. + +In your terminal, use the Azure CLI tool to setup your account permissions locally. + +```shell +az login +``` + +Your browser window will open and you will be prompted to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. You do not need to save this output as it is saved in your system for Terraform to use. + +```shell +You have logged in. Now let us find all the subscriptions to which you have access... + +[ + { + "cloudName": "AzureCloud", + "homeTenantId": "home-Tenant-Id", + "id": "subscription-id", + "isDefault": true, + "managedByTenants": [], + "name": "Subscription-Name", + "state": "Enabled", + "tenantId": "0envbwi39-TenantId", + "user": { + "name": "your-username@domain.com", + "type": "user" + } + } +] +``` + +If you have more than one subscription, specify the subscription-id you want to use with command below: +```shell +az account set --subscription +``` + +### Provision the Resources + +After login Azure CLI with your account, now you can use the terraform script to create Azure Resources. + +#### Run with Bash + +```shell +# In the root directory of the sample +# Initialize your Terraform configuration +terraform -chdir=./terraform init + +# Apply your Terraform Configuration +terraform -chdir=./terraform apply -auto-approve + +``` + +#### Run with Powershell + +```shell +# In the root directory of the sample +# Initialize your Terraform configuration +terraform -chdir=terraform init + +# Apply your Terraform Configuration +terraform -chdir=terraform apply -auto-approve + +``` + +It may take a few minutes to run the script. After successful running, you will see prompt information like below: + +```shell + +azurecaf_name.resource_group: Creating... +azurecaf_name.azurecaf_name_storage_account: Creating... +azurecaf_name.resource_group: Creation complete after 0s +azurecaf_name.azurecaf_name_storage_account: Creation complete after 0s +azurerm_resource_group.main: Creating... +... +azurerm_storage_account.storage_account: Creating... +azurerm_storage_account.storage_account: Still creating... [10s elapsed] +azurerm_storage_account.storage_account: Still creating... [20s elapsed] +azurerm_storage_account.storage_account: Still creating... [30s elapsed] +azurerm_storage_account.storage_account: Creation complete after 34s +azurerm_role_assignment.role_storage_queue_data_contributor: Creating... +azurerm_role_assignment.role_storage_queue_data_contributor: Still creating... [10s elapsed] +azurerm_role_assignment.role_storage_queue_data_contributor: Still creating... [20s elapsed] +azurerm_role_assignment.role_storage_queue_data_contributor: Creation complete after 30s + +Apply complete! Resources: 5 added, 0 changed, 0 destroyed. + +Outputs: + +... + +``` + +You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created. + +### Export Output to Your Local Environment +Running the command below to export environment values: + +#### Run with Bash + +```shell +source ./terraform/setup_env.sh +``` + +#### Run with Powershell + +```shell +terraform\setup_env.ps1 +``` + +## Run Locally + +### Run the sample with Maven + +In your terminal, run `mvn clean spring-boot:run`. + +```shell +mvn clean spring-boot:run +``` + +### Run the sample in IDEs + +You can debug your sample by adding the saved output values to the tool's environment variables or the sample's `application.yaml` file. + +* If your tool is `IDEA`, please refer to [Debug your first Java application](https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html) and [add environment variables](https://www.jetbrains.com/help/objc/add-environment-variables-and-program-arguments.html#add-environment-variables). + +* If your tool is `ECLIPSE`, please refer to [Debugging the Eclipse IDE for Java Developers](https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php) and [Eclipse Environment Variable Setup](https://examples.javacodegeeks.com/desktop-java/ide/eclipse/eclipse-environment-variable-setup-example/). + +## Verify This Sample +View the below results in the console. + +``` +INFO 17464 --- [ main] s.s.q.m.StorageQueueMessagingApplication : Message was sent successfully. +... +INFO 17464 --- [ main] s.s.q.m.StorageQueueMessagingApplication : Received message: Hello world +``` + + +## Clean Up Resources +After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing. + +The terraform destroy command terminates resources managed by your Terraform project. +To destroy the resources you created. + +#### Run with Bash + +```shell +terraform -chdir=./terraform destroy -auto-approve +``` + +#### Run with Powershell + +```shell +terraform -chdir=terraform destroy -auto-approve +``` + +## Deploy to Azure Spring Apps + +Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI). diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/pom.xml b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/pom.xml new file mode 100644 index 000000000..71e068f3f --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/pom.xml @@ -0,0 +1,31 @@ + + + + 4.0.0 + + + com.azure.spring + azure-spring-boot-samples + 1.0.0 + ../../../pom.xml + + + storage-queue-spring-messaging + 1.0.0 + jar + + Communicate to a Azure Storage Queue via Spring messaging + + + + com.azure.spring + spring-messaging-azure-storage-queue + + + com.azure.spring + spring-cloud-azure-starter + + + diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/src/main/java/com/azure/spring/sample/storage/queue/messaging/StorageQueueMessagingApplication.java b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/src/main/java/com/azure/spring/sample/storage/queue/messaging/StorageQueueMessagingApplication.java new file mode 100644 index 000000000..9264067d4 --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/src/main/java/com/azure/spring/sample/storage/queue/messaging/StorageQueueMessagingApplication.java @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.sample.storage.queue.messaging; + +import com.azure.spring.messaging.storage.queue.core.StorageQueueTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.messaging.Message; +import org.springframework.messaging.support.MessageBuilder; +import java.time.Duration; + +@SpringBootApplication +public class StorageQueueMessagingApplication implements CommandLineRunner { + + private static final Logger LOGGER = LoggerFactory.getLogger(StorageQueueMessagingApplication.class); + private static final String STORAGE_QUEUE_NAME = "test"; + + @Autowired + StorageQueueTemplate storageQueueTemplate; + + public static void main(String[] args) { + SpringApplication.run(StorageQueueMessagingApplication.class, args); + } + + @Override + public void run(String... args) { + storageQueueTemplate + .sendAsync(STORAGE_QUEUE_NAME, MessageBuilder.withPayload("Hello world").build()) + .subscribe(); + LOGGER.info("Message was sent successfully."); + + Message message = storageQueueTemplate.receiveAsync(STORAGE_QUEUE_NAME, Duration.ofSeconds(50)).block(); + LOGGER.info("Received message: {}", new String((byte[]) message.getPayload())); + } + +} diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/src/main/resources/application.yml b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/src/main/resources/application.yml new file mode 100644 index 000000000..d05acf41b --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + cloud: + azure: + storage: + queue: + account-name: ${STORAGE_QUEUE_ACCOUNT_NAME} diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/main.tf b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/main.tf new file mode 100644 index 000000000..2a4c9e535 --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/main.tf @@ -0,0 +1,71 @@ +terraform { + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "3.9.0" + } + azurecaf = { + source = "aztfmod/azurecaf" + version = "1.2.16" + } + } +} + +provider "azurerm" { + features {} +} + +resource "azurecaf_name" "resource_group" { + name = var.application_name + resource_type = "azurerm_resource_group" + random_length = 5 + clean_input = true +} + +resource "azurerm_resource_group" "main" { + name = azurecaf_name.resource_group.result + location = var.location + + tags = { + terraform = "true" + application-name = var.application_name + spring-cloud-azure-sample = var.sample_tag_value + } +} + +resource "azurecaf_name" "azurecaf_name_storage_account" { + name = var.application_name + resource_type = "azurerm_storage_account" + random_length = 5 + clean_input = true +} + +# storage +resource "azurerm_storage_account" "storage_account" { + name = azurecaf_name.azurecaf_name_storage_account.result + resource_group_name = azurerm_resource_group.main.name + location = var.location + account_tier = "Standard" + account_replication_type = "LRS" + + tags = { + "spring-cloud-azure-sample" = var.sample_tag_value + "terraform" = "true" + "application-name" = var.application_name + } +} + +resource "azurerm_storage_queue" "queue" { + name = "test" + storage_account_name = azurerm_storage_account.storage_account.name +} + +# assign roles +data "azurerm_client_config" "current" { +} + +resource "azurerm_role_assignment" "role_storage_queue_data_contributor" { + scope = azurerm_storage_account.storage_account.id + role_definition_name = "Storage Queue Data Contributor" + principal_id = data.azurerm_client_config.current.object_id +} diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/outputs.tf b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/outputs.tf new file mode 100644 index 000000000..268be02f6 --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/outputs.tf @@ -0,0 +1,4 @@ +output "STORAGE_QUEUE_ACCOUNT_NAME" { + value = azurerm_storage_account.storage_account.name + description = "The name of storage account." +} diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/setup_env.ps1 b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/setup_env.ps1 new file mode 100644 index 000000000..d6324cd96 --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/setup_env.ps1 @@ -0,0 +1,3 @@ +$env:STORAGE_QUEUE_ACCOUNT_NAME=$(terraform -chdir=terraform output -raw STORAGE_QUEUE_ACCOUNT_NAME) + +echo STORAGE_QUEUE_ACCOUNT_NAME=$env:STORAGE_QUEUE_ACCOUNT_NAME diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/setup_env.sh b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/setup_env.sh new file mode 100644 index 000000000..162162ec6 --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/setup_env.sh @@ -0,0 +1,3 @@ +export STORAGE_QUEUE_ACCOUNT_NAME=$(terraform -chdir=./terraform output -raw STORAGE_QUEUE_ACCOUNT_NAME) + +echo STORAGE_QUEUE_ACCOUNT_NAME=$STORAGE_QUEUE_ACCOUNT_NAME diff --git a/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/variables.tf b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/variables.tf new file mode 100644 index 000000000..cca2f32ff --- /dev/null +++ b/storage/spring-messaging-azure-storage-queue/storage-queue-spring-messaging/terraform/variables.tf @@ -0,0 +1,17 @@ +variable "application_name" { + type = string + description = "The name of your application." + default = "storagequeue" +} + +variable "location" { + type = string + description = "The Azure region where all resources in this example should be created." + default = "eastus" +} + +variable "sample_tag_value" { + type = string + description = "The value of spring-cloud-azure-sample tag." + default = "true" +}