Skip to content

Commit

Permalink
Update for Spring Cloud Azure to 5.15 (#752)
Browse files Browse the repository at this point in the history
* update Spring Cloud Azure 5.15
update Spring Boot 3.3.2/Spring Cloud 2023.0.3
set 5.x as default version

* fix Build with maven

* fix CodeQL

* support spring3

* fix duplicate name
  • Loading branch information
Netyyyy authored Aug 15, 2024
1 parent a66e1ac commit 37cd4b0
Show file tree
Hide file tree
Showing 136 changed files with 3,532 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
distribution: 'adopt'
cache: maven
- name: Build azure-spring-boot-samples 4.x with Maven - Java ${{ matrix.java-version }}
run: mvn -f pom.xml --batch-mode --update-snapshots verify
run: mvn -f pom.xml -P spring-cloud-azure-4.x --batch-mode --update-snapshots verify
- if: ${{ (matrix.java-version == '17') }}
name: Build azure-spring-boot-samples 5.x with Maven - ${{ matrix.java-version }}
run: mvn -f pom.xml -P spring-cloud-azure-5.x --batch-mode --update-snapshots verify
run: mvn -f pom.xml --batch-mode --update-snapshots verify

10 changes: 7 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
repository: Azure/azure-sdk-for-java
ref: main
path: azure-sdk-for-java
- uses: actions/setup-java@v4
with:
distribution: 'oracle' # See 'Supported distributions' for available options
java-version: '17'
- name: Build latest azure-sdk-for-java repository
run: |
cd azure-sdk-for-java
Expand All @@ -70,7 +74,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -81,7 +85,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -95,4 +99,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v3
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
| [spring-cloud-azure-appconfiguration-config-sample](appconfiguration/spring-cloud-azure-appconfiguration-config/spring-cloud-azure-appconfiguration-config-sample) |||
| [spring-cloud-azure-appconfiguration-config-convert-sample-initial](appconfiguration/spring-cloud-azure-appconfiguration-config/spring-cloud-azure-appconfiguration-config-convert-sample/spring-cloud-azure-appconfiguration-config-convert-sample-initial) |||
| [spring-cloud-azure-feature-management-sample](appconfiguration/spring-cloud-azure-feature-management/spring-cloud-azure-feature-management-sample) |||
| [spring-cloud-azure-feature-management-web-sample](appconfiguration/spring-cloud-azure-feature-management-web/spring-cloud-azure-feature-management-web-sample) |||
| [spring-cloud-azure-feature-management-web-sample](appconfiguration/spring-cloud-azure-feature-management-web) |||
| [spring-cloud-azure-starter-appconfiguration-config-sample](appconfiguration/spring-cloud-azure-starter-appconfiguration-config/spring-cloud-azure-starter-appconfiguration-config-sample) |||
| [appconfiguration-client](appconfiguration/spring-cloud-azure-starter-appconfiguration/appconfiguration-client) |||

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ This sample describes how to use [spring-cloud-azure-feature-management](https:/


### How to run without App Configuration Service
Start the application and check the resulting console output to check the returned value.
Start the application and access http://localhost:8080 to check the resulting console output.

1. Load features from application.yml
```
mvn spring-boot:run
```

2. Check the returned value. The feature `Beta` has one filter `Random` which defines
2. Check the returned value, you will see the following information displayed: **Run the Beta filter**.

### How to run with App Configuration Service

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-samples</artifactId>
<version>1.0.0</version>
<relativePath>../../../pom.xml</relativePath>
<relativePath>../../../../pom.xml</relativePath>
</parent>

<artifactId>spring-cloud-azure-feature-management-web-sample</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spring:
config:
message: Hi
feature-management:
beta: true
Beta: true
dark-theme:
enabled-for:
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-samples</artifactId>
<version>1.0.0</version>
<relativePath>../../../pom.xml</relativePath>
<relativePath>../../../../pom.xml</relativePath>
</parent>

<artifactId>spring-cloud-azure-feature-management-web-sample</artifactId>
<artifactId>spring-cloud-azure-targeting-filter-web-sample</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.example.demo;

import org.springframework.boot.SpringApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.example.demo.configuration;
import com.azure.spring.cloud.feature.management.filters.TargetingFilter;
import com.azure.spring.cloud.feature.management.targeting.TargetingEvaluationOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.example.demo.controllers;

import org.springframework.stereotype.Controller;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.example.demo.impl;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
page_type: sample
languages:
- java
products:
- azure-app-configuration
name: Managing Features and Get Configurations From App Configuration in Spring Boot Web Application
description: This sample demonstrates how to manage features and how to get configurations from App Configuration to Spring Environment in Spring Boot web application.
---

# Managing Features and Get Configurations From App Configuration in Spring Boot Web Application

This sample describes how to use [spring-cloud-azure-feature-management](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/spring/spring-cloud-azure-feature-management/README.md) to manage features and how to get configurations from App Configuration Service to Spring Environment.

## Key concepts
## Getting started



### How to run without App Configuration Service
Start the application and access http://localhost:8080 to check the resulting console output.

1. Load features from application.yml
```
mvn spring-boot:run
```

2. Check the returned value, you will see the following information displayed: **Run the Beta filter**.

### How to run with App Configuration Service

#### Prepare data

1. Create a Configuration Store if not exist.

2. Import the data file src/main/resources/data/sample-data.yml into the Configuration Store created above. Under `For language` select `Other`. Under `File type` select `Yaml`.

#### Configure the bootstrap.yaml

Change the connection-string value with the Access Key value of the Configuration Store created above.

#### Run the application
Start the application and access http://localhost:8080 to check the returned value. Different commands for different scenarios are listed below.

1. Load properties similar with from application.properties, i.e., keys starting with /application/
```
mvn spring-boot:run
```

2. Load properties similar with from application_dev.properties, i.e., keys starting with /application_dev
```
mvn -Dspring.profiles.active=dev spring-boot:run
```

3. Load properties similar with from foo.properties, i.e., keys starting with /foo/
```
mvn -Dspring.application.name=foo spring-boot:run
```

4. Load properties similar with from foo_dev.properties, i.e., keys starting with /foo_dev/
```
mvn -Dspring.application.name=foo -Dspring.profiles.active=dev spring-boot:run
```

### More details

Please refer to this [README](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/spring/spring-cloud-azure-starter-appconfiguration-config/README.md) about more usage details.

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

## Examples
## Troubleshooting
## Next steps
## Contributing

<!-- LINKS -->

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>spring-cloud-azure-feature-management-web-sample</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>Manage Features and Get Configurations From App Configuration For Web Service</name>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-feature-management-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.example;

import java.io.IOException;



import com.azure.spring.cloud.feature.management.FeatureManager;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class FeatureFilter implements Filter {

private static final Logger LOGGER = LoggerFactory.getLogger(FeatureFilter.class);

@Autowired
private FeatureManager featureManager;

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (!featureManager.isEnabledAsync("Beta").block()) {
chain.doFilter(request, response);
return;
}
LOGGER.info("Run the Beta filter");
chain.doFilter(request, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.example;

import com.azure.spring.cloud.feature.management.FeatureManager;
import com.azure.spring.cloud.feature.management.web.FeatureGate;
import com.azure.spring.cloud.feature.management.web.FeatureManagerSnapshot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
@ConfigurationProperties("controller")
public class HelloController {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);

@Autowired
private FeatureManager featureManager;

@Autowired
private FeatureManagerSnapshot featureManagerSnapshot;

@GetMapping("/privacy")
public String getRequestBased(Model model) {
model.addAttribute("Beta", featureManager.isEnabledAsync("beta").block());
model.addAttribute("isDarkThemeS1", featureManagerSnapshot.isEnabledAsync("dark-theme").block());
model.addAttribute("isDarkThemeS2", featureManagerSnapshot.isEnabledAsync("dark-theme").block());
model.addAttribute("isDarkThemeS3", featureManagerSnapshot.isEnabledAsync("dark-theme").block());
return "privacy";
}

@GetMapping(value = {"/Beta", "/BetaA"})
@FeatureGate(feature = "beta-ab", fallback = "/BetaB")
public String getRedirect(Model model) {
return "BetaA";
}

@GetMapping("/BetaB")
public String getRedirected(Model model) {
return "BetaB";
}

@GetMapping(value = {"", "/", "/welcome"})
public String mainWithParam(Model model) {
model.addAttribute("Beta", featureManager.isEnabledAsync("beta").block());
return "welcome";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.example;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "config")
public class MessageProperties {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Loading

0 comments on commit 37cd4b0

Please sign in to comment.