forked from Azure-Samples/azure-spring-boot-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update redis kafka sample (Azure-Samples#165)
- Loading branch information
Showing
9 changed files
with
202 additions
and
65 deletions.
There are no files selected for viewing
160 changes: 104 additions & 56 deletions
160
cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 3 additions & 7 deletions
10
...g-cloud-azure-starter/spring-cloud-azure-sample-cache/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
58 changes: 58 additions & 0 deletions
58
cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/outputs.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
3 changes: 3 additions & 0 deletions
3
cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/setup_env.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
17 changes: 17 additions & 0 deletions
17
cache/spring-cloud-azure-starter/spring-cloud-azure-sample-cache/terraform/variables.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
2 changes: 1 addition & 1 deletion
2
.../spring-cloud-azure-starter/spring-cloud-azure-sample-eventhubs-kafka/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters