Skip to content

Commit 595dd5e

Browse files
Extend Spring Boot app + Service Bus sample (#55)
1 parent 9eecad9 commit 595dd5e

16 files changed

Lines changed: 1382 additions & 56 deletions

File tree

samples/servicebus/README.md

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,80 @@
1-
# Azure ServiceBus (Java SDK)
1+
# Azure Service Bus with Spring Boot
22

3-
This sample creates a minimal Spring Boot application that sends and receive messages via Azure ServiceBus.
3+
This sample demonstrates a Java Spring Boot application that sends and receives messages via [Azure Service Bus](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview). The application uses the [Spring Cloud Azure Service Bus Stream Binder](https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-cloud-stream-binder-java-app-with-service-bus) to connect to a Service Bus queue, send a `Hello, World!` message, and receive it back, and then exits.
44

5-
## Overview
5+
> [!NOTE]
6+
> At this time, the Azure Web Apps and Azure Function Apps emulators in LocalStack for Azure do not support Java applications. The Spring Boot sample application must be executed directly on the host machine and cannot be deployed to the emulator.
67
7-
- **`app`**: A folder that contains the Java application
8-
- **`scripts/deploy.sh`**: One script that provisions the required resources for this sample and runs the application
9-
10-
## Quick Start
8+
## Architecture
9+
10+
The solution is composed of the following Azure resources:
11+
12+
1. [Azure Resource Group](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-cli): A logical container scoping all resources in this sample.
13+
2. [Azure Service Bus Namespace](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview): The messaging namespace that hosts the queue used by the application.
14+
3. [Azure Service Bus Queue](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions#queues): The `myqueue` queue used to send and receive messages.
15+
16+
> **Note**
17+
> The Java application currently runs on the host machine. In a future iteration, it will be deployed to an emulator-hosted web app.
18+
19+
## Prerequisites
20+
21+
- [Azure Subscription](https://azure.microsoft.com/free/)
22+
- [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli)
23+
- [Azlocal CLI](https://azure.localstack.cloud/user-guides/sdks/az/): LocalStack Azure CLI wrapper
24+
- [Java 21+](https://learn.microsoft.com/en-us/java/openjdk/download)
25+
- [Maven 3.8+](https://maven.apache.org/download.cgi)
26+
- [Terraform](https://developer.hashicorp.com/terraform/downloads), if you plan to deploy the sample via Terraform.
27+
- [Bicep extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep), if you plan to deploy the sample via Bicep.
28+
29+
## Deployment
30+
31+
Set up the Azure emulator using the LocalStack for Azure Docker image. Before starting, ensure you have a valid `LOCALSTACK_AUTH_TOKEN` to access the Azure emulator. Refer to the [Auth Token guide](https://docs.localstack.cloud/getting-started/auth-token/) to obtain your Auth Token and set it in the `LOCALSTACK_AUTH_TOKEN` environment variable. The Azure Docker image is available on the [LocalStack Docker Hub](https://hub.docker.com/r/localstack/localstack-azure-alpha). To pull the image, execute:
1132

12-
To deploy the scenario against a LocalStack Emulator:
1333
```bash
14-
bash ./scripts/deploy.sh
34+
docker pull localstack/localstack-azure-alpha
1535
```
1636

17-
The script will:
18-
1. Creates a ResourceGroup
19-
2. Creates a Servicebus Namespace
20-
3. Creates a Servicebus Queue
21-
4. Starts the Java app
22-
23-
The app then:
24-
1. Connects to the configured ServiceBus
25-
2. Sends a message to a queue
26-
3. Receives the message, and shuts down the application
27-
28-
After the application has shutdown, the script will then:
29-
5. Deletes the resource group with all of it's resources
37+
Start the LocalStack Azure emulator by running:
38+
39+
```bash
40+
export LOCALSTACK_AUTH_TOKEN=<your_auth_token>
41+
IMAGE_NAME=localstack/localstack-azure-alpha localstack start
42+
```
43+
44+
Deploy the application to LocalStack for Azure using one of these methods:
45+
46+
- [Azure CLI Deployment](./java/scripts/deploy.sh)
47+
- [Bicep Deployment](./java/bicep/deploy.sh)
48+
- [Terraform Deployment](./java/terraform/deploy.sh)
49+
50+
All deployment methods have been fully tested against Azure and the LocalStack for Azure local emulator.
51+
52+
> **Note**
53+
> When you deploy the application to LocalStack for Azure for the first time, the initialization process involves downloading and building Docker images. This is a one-time operation—subsequent deployments will be significantly faster. Depending on your internet connection and system resources, this initial setup may take several minutes.
54+
55+
## How It Works
56+
57+
The deploy script performs the following steps:
58+
59+
1. Creates a resource group.
60+
2. Creates a Service Bus namespace.
61+
3. Creates a Service Bus queue (`myqueue`).
62+
4. Retrieves the namespace connection string and exports it as `AZURE_SERVICEBUS_CONNECTION_STRING`.
63+
5. Starts the Spring Boot application via `mvn clean spring-boot:run`.
64+
65+
The application then:
66+
67+
1. Connects to the configured Service Bus namespace using the connection string.
68+
2. Sends a `Hello, World!` message to the `myqueue` queue.
69+
3. Receives the message via a `@ServiceBusListener` consumer.
70+
4. Shuts down after receiving the message.
71+
72+
## References
73+
74+
- [Azure Service Bus Documentation](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview)
75+
- [Spring Cloud Azure Service Bus](https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-cloud-stream-binder-java-app-with-service-bus)
76+
- [Azure Service Bus Queues](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions)
77+
- [Spring Boot Starter for Azure Service Bus](https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/spring-cloud-azure)
78+
- [LocalStack for Azure](https://azure.localstack.cloud/)
3079

3180

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# Bicep Deployment
2+
3+
This directory contains a Bicep template and a deployment script for provisioning Azure services in LocalStack for Azure. For further details about the sample application, refer to the [Azure Service Bus with Spring Boot](../../README.md).
4+
5+
## Prerequisites
6+
7+
Before deploying this solution, ensure you have the following tools installed:
8+
9+
- [LocalStack for Azure](https://azure.localstack.cloud/): Local Azure cloud emulator for development and testing
10+
- [Visual Studio Code](https://code.visualstudio.com/): Code editor installed on one of the [supported platforms](https://code.visualstudio.com/docs/supporting/requirements#_platforms)
11+
- [Bicep extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep): VS Code extension for Bicep language support and IntelliSense
12+
- [Docker](https://docs.docker.com/get-docker/): Container runtime required for LocalStack
13+
- [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli): Azure command-line interface
14+
- [Azlocal CLI](https://azure.localstack.cloud/user-guides/sdks/az/): LocalStack Azure CLI wrapper
15+
- [Java 21+](https://learn.microsoft.com/en-us/java/openjdk/download): Java runtime for compiling and running the sample application
16+
- [Maven 3.8+](https://maven.apache.org/download.cgi): Build tool for managing Java project dependencies and compilation
17+
- [jq](https://jqlang.org/): JSON processor for scripting and parsing command outputs
18+
19+
### Installing azlocal CLI
20+
21+
The [deploy.sh](deploy.sh) Bash script uses the `azlocal` CLI instead of the standard Azure CLI to work with LocalStack. Install it using:
22+
23+
```bash
24+
pip install azlocal
25+
```
26+
27+
For more information, see [Get started with the az tool on LocalStack](https://azure.localstack.cloud/user-guides/sdks/az/).
28+
29+
## Architecture Overview
30+
31+
The [deploy.sh](deploy.sh) script creates the [Azure Resource Group](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-cli), while the Bicep template creates the following Azure resources:
32+
33+
1. [Azure Resource Group](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-cli): A logical container scoping all resources in this sample.
34+
2. [Azure Service Bus Namespace](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview): The messaging namespace that hosts the queue used by the application.
35+
3. [Azure Service Bus Queue](https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions#queues): The `myqueue` queue used to send and receive messages.
36+
37+
The Spring Boot sample application connects to the Service Bus namespace, sends a test message to the sample queue, receives it back, and exits. For more information on the sample application, see [Azure Service Bus with Spring Boot](../../README.md).
38+
39+
## Configuration
40+
41+
Before deploying the `main.bicep` template, update the [main.bicepparam](main.bicepparam) file with your specific values:
42+
43+
```bicep
44+
using 'main.bicep'
45+
46+
param queueName = 'myqueue'
47+
param zoneRedundant = false
48+
param tags = {
49+
environment: 'test'
50+
iac: 'bicep'
51+
}
52+
```
53+
54+
## Provisioning Scripts
55+
56+
You can use the [deploy.sh](deploy.sh) script to automate the deployment of all Azure resources and the sample application in a single step, streamlining setup and reducing manual configuration. The script executes the following steps:
57+
58+
- Detects environment (LocalStack vs Azure Cloud) and uses the appropriate CLI.
59+
- Creates the resource group if it doesn't exist.
60+
- Optionally validates the Bicep template.
61+
- Optionally runs a what-if deployment for preview.
62+
- Deploys the `main.bicep` template with parameters from [main.bicepparam](main.bicepparam).
63+
- Extracts the Service Bus namespace name from deployment outputs.
64+
- Retrieves the namespace connection string and exports it as `AZURE_SERVICEBUS_CONNECTION_STRING`.
65+
- Compiles the Spring Boot project and runs the app on the host machine.
66+
67+
## Deployment
68+
69+
You can set up the Azure emulator by utilizing LocalStack for Azure Docker image. Before starting, ensure you have a valid `LOCALSTACK_AUTH_TOKEN` to access the Azure emulator. Refer to the [Auth Token guide](https://docs.localstack.cloud/getting-started/auth-token/) to obtain your Auth Token and specify it in the `LOCALSTACK_AUTH_TOKEN` environment variable. The Azure Docker image is available on the [LocalStack Docker Hub](https://hub.docker.com/r/localstack/localstack-azure-alpha). To pull the Azure Docker image, execute the following command:
70+
71+
```bash
72+
docker pull localstack/localstack-azure-alpha
73+
```
74+
75+
Start the LocalStack Azure emulator using the localstack CLI, execute the following command:
76+
77+
```bash
78+
export LOCALSTACK_AUTH_TOKEN=<your_auth_token>
79+
IMAGE_NAME=localstack/localstack-azure-alpha localstack start
80+
```
81+
82+
Navigate to the `bicep` folder:
83+
84+
```bash
85+
cd samples/servicebus/java/bicep
86+
```
87+
88+
Make the script executable:
89+
90+
```bash
91+
chmod +x deploy.sh
92+
```
93+
94+
Run the deployment script:
95+
96+
```bash
97+
./deploy.sh
98+
```
99+
100+
## Validation
101+
102+
Once the deployment completes, run the [validate.sh](../scripts/validate.sh) script to confirm that all resources were provisioned and configured as expected:
103+
104+
```bash
105+
#!/bin/bash
106+
107+
# Variables
108+
PREFIX='local'
109+
SUFFIX='test'
110+
RESOURCE_GROUP_NAME="${PREFIX}-rg"
111+
SERVICEBUS_NAMESPACE_NAME="${PREFIX}-sb-ns-${SUFFIX}"
112+
SERVICEBUS_QUEUE_NAME="myqueue"
113+
ENVIRONMENT=$(az account show --query environmentName --output tsv)
114+
115+
# Choose the appropriate CLI based on the environment
116+
if [[ $ENVIRONMENT == "LocalStack" ]]; then
117+
echo "Using azlocal for LocalStack emulator environment."
118+
AZ="azlocal"
119+
else
120+
echo "Using standard az for AzureCloud environment."
121+
AZ="az"
122+
fi
123+
124+
# Check resource group
125+
echo -e "[$RESOURCE_GROUP_NAME] resource group:\n"
126+
$AZ group show \
127+
--name "$RESOURCE_GROUP_NAME" \
128+
--output table \
129+
--only-show-errors
130+
131+
# Check Service Bus namespace
132+
echo -e "\n[$SERVICEBUS_NAMESPACE_NAME] Service Bus namespace:\n"
133+
$AZ servicebus namespace show \
134+
--resource-group "$RESOURCE_GROUP_NAME" \
135+
--name "$SERVICEBUS_NAMESPACE_NAME" \
136+
--query "{name:name, location:location, serviceBusEndpoint:serviceBusEndpoint, status:provisioningState}" \
137+
--output table \
138+
--only-show-errors
139+
140+
# Check Service Bus queue
141+
echo -e "\n[$SERVICEBUS_QUEUE_NAME] Service Bus queue:\n"
142+
$AZ servicebus queue show \
143+
--resource-group "$RESOURCE_GROUP_NAME" \
144+
--namespace-name "$SERVICEBUS_NAMESPACE_NAME" \
145+
--name "$SERVICEBUS_QUEUE_NAME" \
146+
--query "{name:name, messageCount:messageCount, sizeInBytes:sizeInBytes}" \
147+
--output table \
148+
--only-show-errors
149+
```
150+
151+
## Cleanup
152+
153+
To destroy all created resources:
154+
155+
```bash
156+
#!/bin/bash
157+
158+
# Variables
159+
PREFIX='local'
160+
SUFFIX='test'
161+
RESOURCE_GROUP_NAME="${PREFIX}-rg"
162+
163+
# Delete resource group and all contained resources
164+
az group delete --name $RESOURCE_GROUP_NAME --yes --no-wait
165+
166+
# Verify deletion
167+
az group list --output table
168+
```
169+
170+
This will remove all Azure resources created by the Bicep deployment script.
171+
172+
## Related Documentation
173+
174+
- [Azure Bicep Documentation](https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/)
175+
- [Bicep Language Reference](https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions)
176+
- [LocalStack for Azure Documentation](https://azure.localstack.cloud/)

0 commit comments

Comments
 (0)