Skip to content

Commit

Permalink
Update redis kafka sample (Azure-Samples#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
moarychan authored Jan 25, 2022
1 parent 8e2375f commit eb58e8c
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,86 +1,134 @@
# Spring Cloud Azure Sample redis cache

## Key concepts

Redis support is based on spring-boot-starter-data-redis, which obtains and
Automatic configuration of redis properties through Azure Redis Cache Management SDK.

## What You Will Build
You will build an application using the Spring Boot Starter Redis, Spring Cloud Azure Starter and Spring Cloud Azure Resource Manager to cache data to Azure Cache for Redis.

## Getting started
## What You Need

Running this sample will be charged by Azure. You can check the usage and bill at [this link][azure-account].
- [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 Azure Active Directory for accessing an Azure resource
> please refer to [Authorize access with Azure AD](https://microsoft.github.io/spring-cloud-azure/docs/current/reference/html/index.html#authorize-access-with-azure-active-directory) 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.

### Coordinates
In your terminal, use the Azure CLI tool to setup your account permissions locally.

Maven coordinates:
```shell
az login
```

```xml
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-resourcemanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
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": "[email protected]",
"type": "user"
}
}
]
```

### Create an Azure Cache for Redis instance
If you have more than one subscription, specify the subscription-id you want to use with command below:
```shell
az account set --subscription <your-subscription-id>
```

### Provision the Resources

After login Azure CLI with your account, now you can use the terraform script to create Azure Resources.

1. Create a service principal for use in by your app. Please follow [create service principal from Azure CLI][create-sp-using-azure-cli].
Now you can use the terraform script to create Azure Resources.

1. Create an Azure Cache for Redis instance. Please follow [create-azure-cache-for-redis].
```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
```

## Examples
It may take around 15 minutes to run the script. After successful running, you will see prompt information like below:

1. Update `src/main/resources/application.yaml` to specify
resource group, service principal, and cache instance name:
```shell

```yaml
spring:
cloud:
azure:
credential:
client-id: [service-principal-id]
client-secret: [service-principal-secret]
profile:
tenant-id: [tenant-id]
subscription-id: [subscription-id]
redis:
name: [azure-cache-for-redis-instance-name]
resource:
resource-group: [resource-group]
```
> :notes: Please note that currently we do not support the automatic creation of Azure Cache resources.
1. Run the application using the `$ mvn spring-boot:run` command.
azurecaf_name.resource_group: Creating...
azurecaf_name.azurecaf_name_redis: Creating...
azurecaf_name.azurecaf_name_redis: Creation complete after 0s ...
azurerm_redis_cache.redis: Still creating...
azurerm_redis_cache.redis: Still creating...
azurerm_redis_cache.redis: Creation complete after ...

1. Send a GET request to check, where `name` could be any string:
Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

$ curl -XGET http://localhost:8080/{name}
Outputs:
...

1. Confirm from Azure Redis Cache console in Azure Portal
```

$ keys *
You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created.

1. Delete the resources on [Azure Portal][azure-portal] to avoid unexpected charges.
### Export Output to Your Local Environment
Running the command below to export environment values:

```shell
source ./terraform/setup_env.sh
```

## Troubleshooting
## Run Locally

## Next steps
In your terminal, run `mvn clean spring-boot:run`.

```shell
mvn clean spring-boot:run
```

## Contributing
## Verify This Sample

<!-- LINKS -->
[azure-account]: https://azure.microsoft.com/account/
[azure-portal]: https://ms.portal.azure.com/
[create-azure-cache-for-redis]: https://docs.microsoft.com/azure/azure-cache-for-redis/quickstart-create-redis
[create-sp-using-azure-cli]: https://github.com/Azure-Samples/azure-spring-boot-samples/blob/main/create-sp-using-azure-cli.md
1. Send a GET request to check, where `name` could be any string:

```shell
$ curl -XGET http://localhost:8080/{name}
```

2. Confirm from Azure Redis Cache console in Azure Portal:

```shell
$ keys *
```

## 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.

```shell
terraform -chdir=./terraform destroy -auto-approve
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<relativePath>../../../pom.xml</relativePath>
</parent>

<artifactId>spring-cloud-azure-cache-sample</artifactId>
<artifactId>spring-cloud-azure-sample-cache</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
spring:
cloud:
azure:
credential:
client-id: [service-principal-id]
client-secret: [service-principal-secret]
profile:
tenant-id: [tenant-id]
subscription-id: [subscription-id]
subscription-id: ${AZURE_SUBSCRIPTION_ID}
redis:
name: [azure-cache-for-redis-instance-name]
name: ${AZURE_CACHE_REDIS_NAME}
resource:
resource-group: [resource-group]
resource-group: ${AZURE_RESOURCE_GROUP}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 2.75"
}
azurecaf = {
source = "aztfmod/azurecaf"
version = "1.2.10"
}
}
}

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
}
}

# =================== redis ================
data "azurerm_subscription" "current" { }

resource "azurecaf_name" "azurecaf_name_redis" {
name = var.application_name
resource_type = "azurerm_redis_cache"
random_length = 5
clean_input = true
}

resource "azurerm_redis_cache" "redis" {
name = azurecaf_name.azurecaf_name_redis.result
location = var.location
resource_group_name = azurerm_resource_group.main.name
capacity = 0
family = "C"
sku_name = "Basic"
enable_non_ssl_port = false
minimum_tls_version = "1.2"

redis_configuration {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "AZURE_SUBSCRIPTION_ID" {
value = data.azurerm_subscription.current.subscription_id
description = "The subscription ID of the resource."
}

output "AZURE_RESOURCE_GROUP" {
value = azurerm_redis_cache.redis.resource_group_name
description = "The resource group name of the resource."
}

output "AZURE_CACHE_REDIS_NAME" {
value = azurerm_redis_cache.redis.name
description = "The DNS name of the Redis instance."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export AZURE_SUBSCRIPTION_ID=$(terraform -chdir=./terraform output -raw AZURE_SUBSCRIPTION_ID)
export AZURE_RESOURCE_GROUP=$(terraform -chdir=./terraform output -raw AZURE_RESOURCE_GROUP)
export AZURE_CACHE_REDIS_NAME=$(terraform -chdir=./terraform output -raw AZURE_CACHE_REDIS_NAME)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
variable "application_name" {
type = string
description = "The name of your application."
default = "redis-sample"
}

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"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Spring Cloud Azure Sample Stream Event Hubs Kafka

This sample demonstrates how to use the Spring Cloud Azure Starter and Spring Cloud Starter Stream Kafka for Azure Event Hubs. The sample app exposes a RESTful API to receive
This sample demonstrates how to use the Spring Cloud Azure Starter and Spring Cloud Starter Stream Kafka for Azure Event Hubs ([Basic pricing tier is not supported](https://azure.microsoft.com/pricing/details/event-hubs/#explore-pricing-options)). The sample app exposes a RESTful API to receive
string message. Then message is sent through Azure Event Hubs to a bean `consumer`
which simply logs the message.

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>appconfiguration/azure-appconfiguration-sample</module>
<module>appconfiguration/feature-management-sample</module>
<module>appconfiguration/feature-management-web-sample</module>
<module>cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache</module>
<module>cloudfoundry/azure-cloud-foundry-service-sample</module>
<module>cosmos/spring-cloud-azure-starter-data-cosmos/spring-cloud-azure-data-cosmos-sample</module>
<module>cosmos/spring-cloud-azure-starter-cosmos/spring-cloud-azure-cosmos-sample</module>
Expand Down

0 comments on commit eb58e8c

Please sign in to comment.