Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Spring Native configuration for samples #283

Merged
merged 41 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6192ad2
Cherry-pick the pr from 4.0 branch
moarychan Apr 4, 2022
a7987e4
Update sample for native image build configuration
moarychan Apr 7, 2022
8bc8692
Merge branch 'main' of github.com:Azure-Samples/azure-spring-boot-sam…
moarychan Apr 7, 2022
62ca14e
Use spring-cloud-azure-actuator instead of spring-boot-starter-actuator
moarychan Apr 8, 2022
5f9eab1
Remove non-existent proxy configuration
moarychan Apr 11, 2022
a0d8396
Revert "Remove non-existent proxy configuration"
moarychan Apr 11, 2022
580cf92
Add a section for readme for running the sample based on Spring Native;
moarychan Apr 11, 2022
6b69812
Move the aot plugin of root plugins into the special profile plugins
moarychan Apr 11, 2022
0a5948a
Add description for the command execution within the command prompt a…
moarychan Apr 11, 2022
e450284
Add system requirement for native build tools
moarychan Apr 11, 2022
b1ce537
Update docker image name;Update the change to storage queue sample
moarychan Apr 11, 2022
72610bd
Update destroy command section title
moarychan Apr 11, 2022
047d2e8
Add native usage section for other samples
moarychan Apr 12, 2022
4ce60a9
Update terraform provider version
moarychan Apr 12, 2022
d4838da
Fix the problems encountered in the sample test
moarychan Apr 13, 2022
0d364ee
Update build args
moarychan Apr 14, 2022
9d80e66
Update commands to build native executable when using Buildpack
moarychan Apr 15, 2022
30159b3
Remove testing dependencies
moarychan Apr 18, 2022
ebc65a6
Use the same spring native version
moarychan Apr 19, 2022
6dc78cc
Fix conflicts
moarychan Apr 19, 2022
06960e0
Merge branch 'main' of github.com:Azure-Samples/azure-spring-boot-sam…
moarychan Apr 20, 2022
8216d24
Fix comments
moarychan Apr 20, 2022
8f8fb72
Make the profile name same with shade plugin
moarychan Apr 20, 2022
c4a9360
Make the plugin spring-boot-maven-plugin as the default for each sample
moarychan Apr 21, 2022
2745c3d
No need shade plugin to fix the 'commands to long exception' due to t…
moarychan Apr 21, 2022
d6e3a22
Remove the native usage section in each supported sample and add a ne…
moarychan Apr 24, 2022
8852697
Add README.md for spring-native folder.
moarychan Apr 24, 2022
3eecb17
Remove image name properties
moarychan Apr 24, 2022
2c3132e
Fix comments
moarychan Apr 25, 2022
6b68bf8
Merge branch 'main' of github.com:Azure-Samples/azure-spring-boot-sam…
moarychan Apr 25, 2022
4212a98
Remove unnecessary dependencies
moarychan Apr 25, 2022
7692e91
Fix comments
moarychan Apr 25, 2022
16a21e6
Update README.md
saragluna Apr 25, 2022
683f4d5
Update README.md
saragluna Apr 25, 2022
612d2b5
Update README.md
saragluna Apr 25, 2022
108dfa3
Update README.md
saragluna Apr 25, 2022
5e20e8b
Update README.md
saragluna Apr 25, 2022
fb88a17
Fix the forgotten comments
moarychan Apr 25, 2022
e4b6d35
Merge remote-tracking branch 'origin/moary/support-spring-native' int…
moarychan Apr 25, 2022
a3375a5
Revert https://github.com/Azure-Samples/azure-spring-boot-samples/pul…
moarychan Apr 26, 2022
423f8cb
Fix comments
moarychan Apr 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ mvn clean spring-boot:run
It supports both Bash environment and [PowerShell](https://docs.microsoft.com/en-us/powershell/) environment.
Please refer to [README.md](servicebus/spring-cloud-azure-starter-integration-servicebus/single-namespace/README.md) under each sample for detailed information.

### Run Samples Based On Spring Native
The parent project POM file has been configured with the `Spring Native` support to build a Spring Boot native application by default, which can be built based on [Buildpacks](https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/#getting-started-buildpacks) and [Native Build Tools](https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/#getting-started-native-build-tools), please follow the readme doc in each sample to build a specific native application.
saragluna marked this conversation as resolved.
Show resolved Hide resolved

###
[spring-cloud-azure-starter-keyvault-secrets:4.0.0]: https://search.maven.org/artifact/com.azure.spring/spring-cloud-azure-starter-keyvault-secrets/4.0.0/jar
[spring-cloud-azure-stream-binder-eventhubs:4.0.0]: https://search.maven.org/artifact/com.azure.spring/spring-cloud-azure-stream-binder-eventhubs/4.0.0/jar
[spring-cloud-azure-starter-integration-eventhubs:4.0.0]: https://search.maven.org/artifact/com.azure.spring/spring-cloud-azure-starter-integration-eventhubs/4.0.0/jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ source ./terraform/setup_env.sh
#### Run with Powershell

```shell
. terraform\setup_env.ps1
terraform\setup_env.ps1
```

You will see output like below, save this output to use later.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ source ./terraform/setup_env.sh
#### Run with Powershell

```shell
. terraform\setup_env.ps1
terraform\setup_env.ps1
```

You will see output like below, save this output of `created user` to login.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,4 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Using Spring Cloud Azure App Configuration Starter

This sample illustrates the simplest usage of `spring-cloud-azure-starter-appconfiguration`.

## What You Will Build
You will build an application that use Spring Resource abstraction to read and write data with [Azure Storage Blob](https://azure.microsoft.com/services/storage/blobs/).
saragluna marked this conversation as resolved.
Show resolved Hide resolved

## 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 Azure Active Directory for accessing an Azure resource
> please refer to [Authorize access with Azure AD](https://microsoft.github.io/spring-cloud-azure/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.

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 <your-subscription-id>
```

### 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.appconfig: Creating...
azurecaf_name.resource_group: Creating...
...
azurerm_resource_group.main: Creating...
...
azurerm_role_assignment.appconf_dataowner: Creating...
...
Apply complete! Resources: 6 added, 0 changed, 0 destroyed.

```

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
APPCONFIGURATION_ENDPOINT=...
```

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

```text
INFO 15116 --- [ main] .s.s.a.AppConfigurationSampleApplication : Returned the from Azure App Configuration: sample-key, hello from default application
saragluna marked this conversation as resolved.
Show resolved Hide resolved
```


## 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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-samples</artifactId>
<version>1.0.0</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<artifactId>appconfiguration-sample</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>Read and Write Data with Azure AppConfiguration Using ConfigurationClient</name>
<description>Sample project for Spring Cloud Azure Starter AppConfiguration</description>

<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-appconfiguration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.spring.sample.appconfiguration;

import com.azure.data.appconfiguration.ConfigurationClient;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
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;

@SpringBootApplication
public class AppConfigurationSampleApplication implements CommandLineRunner {

private static final Logger logger = LoggerFactory.getLogger(AppConfigurationSampleApplication.class);

@Autowired
private ConfigurationClient configurationClient;

public static void main(String[] args) {
SpringApplication.run(AppConfigurationSampleApplication.class, args);
}

public void run(String... var1) throws Exception {
String sampleKey = "sample-key";
ConfigurationSetting configurationSetting = configurationClient.getConfigurationSetting(sampleKey, "somelabel");
logger.info("Returned the from Azure App Configuration: {}, {}", sampleKey, configurationSetting.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring:
cloud:
azure:
appconfiguration:
endpoint: ${APPCONFIGURATION_ENDPOINT}

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.99"
}
azurecaf = {
source = "aztfmod/azurecaf"
version = "1.2.16"
}
}
}

provider "azurerm" {
features {}
}

// ===========resource_group===========
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" {
}

// ===========azurerm_app_configuation===========
resource "azurecaf_name" "appconfig" {
name = var.application_name
resource_type = "azurerm_app_configuration"
random_length = 5
clean_input = true
}

resource "azurerm_app_configuration" "appconfig_resource" {
name = azurecaf_name.appconfig.result
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
sku = "standard"
tags = {
"terraform" = "true"
"spring-cloud-azure-sample" = var.sample_tag_value
}
}

resource "azurerm_role_assignment" "appconf_dataowner" {
scope = azurerm_app_configuration.appconfig_resource.id
role_definition_name = "App Configuration Data Owner"
principal_id = data.azurerm_client_config.current.object_id
}

resource "azurerm_app_configuration_key" "test" {
configuration_store_id = azurerm_app_configuration.appconfig_resource.id
key = "sample-key"
label = "somelabel"
value = "hello from default application"
depends_on = [
azurerm_role_assignment.appconf_dataowner
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "APPCONFIGURATION_ENDPOINT" {
value = azurerm_app_configuration.appconfig_resource.endpoint
description = "The app configuration endpoint."
}

output "RESOURCE_GROUP_NAME" {
value = azurerm_resource_group.main.name
description = "The resource group name."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$env:APPCONFIGURATION_ENDPOINT=$(terraform -chdir=terraform output -raw APPCONFIGURATION_ENDPOINT)

echo APPCONFIGURATION_ENDPOINT=$env:APPCONFIGURATION_ENDPOINT
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export APPCONFIGURATION_ENDPOINT=$(terraform -chdir=./terraform output -raw APPCONFIGURATION_ENDPOINT)
echo APPCONFIGURATION_ENDPOINT=$APPCONFIGURATION_ENDPOINT
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 = "appconfiguration-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"
}
Loading