From fa7f0ffea19fd39e85830434272ee0a1ab460063 Mon Sep 17 00:00:00 2001 From: Dingmeng Xue Date: Fri, 9 Aug 2024 10:54:55 +0800 Subject: [PATCH 1/4] Add java 17 sample --- java-17-sample/.gitignore | 38 +++++++++++ java-17-sample/README.md | 25 +++++++ java-17-sample/pom.xml | 68 +++++++++++++++++++ .../com/azure/asa/sample/HelloController.java | 30 ++++++++ .../asa/sample/Java17SampleApplication.java | 13 ++++ .../src/main/resources/application.properties | 2 + 6 files changed, 176 insertions(+) create mode 100644 java-17-sample/.gitignore create mode 100644 java-17-sample/README.md create mode 100644 java-17-sample/pom.xml create mode 100644 java-17-sample/src/main/java/com/azure/asa/sample/HelloController.java create mode 100644 java-17-sample/src/main/java/com/azure/asa/sample/Java17SampleApplication.java create mode 100644 java-17-sample/src/main/resources/application.properties diff --git a/java-17-sample/.gitignore b/java-17-sample/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/java-17-sample/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/java-17-sample/README.md b/java-17-sample/README.md new file mode 100644 index 0000000..521c169 --- /dev/null +++ b/java-17-sample/README.md @@ -0,0 +1,25 @@ +# Prepare Variables +```shell +SUBSCRIPTION=6c933f90-8115-4392-90f2-7077c9fa5dbd +LOCATION=westus2 +RESOURCE_GROUP=dixue-asas-vnet-prod +SERVICE_NAME=dixue-asas-vnet-prod + +az account set --subscription $SUBSCRIPTION +``` + +# Standard Plan +## Provision ASA Service Instance +```shell + +``` +## Deploy Application +```shell +mvn clean package +az spring app create -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --runtime-version Java_17 --assign-endpoint true +az spring app deploy -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --artifact-path ./target/java-17-sample-1.0-SNAPSHOT.jar +``` +# Enterprise Plan +## Provision ASA Service Instance + +## Deploy Application diff --git a/java-17-sample/pom.xml b/java-17-sample/pom.xml new file mode 100644 index 0000000..a652304 --- /dev/null +++ b/java-17-sample/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.azure.asa.sample + java-17-sample + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 3.3.2 + + + + 17 + 17 + UTF-8 + 2023.0.3 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/java-17-sample/src/main/java/com/azure/asa/sample/HelloController.java b/java-17-sample/src/main/java/com/azure/asa/sample/HelloController.java new file mode 100644 index 0000000..19fa35a --- /dev/null +++ b/java-17-sample/src/main/java/com/azure/asa/sample/HelloController.java @@ -0,0 +1,30 @@ +package com.azure.asa.sample; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.ArrayList; +import java.util.List; + +@RestController +public class HelloController { + + @GetMapping("/") + public String index() { + StringBuffer sb = new StringBuffer(); + sb.append("

Greetings from Spring Boot!

"); + + return sb.toString(); + } + + private List memory = new ArrayList<>(); + + @PostMapping("/memory/add") + public String increaseMemory() { + byte[] bytes = new byte[32 * 1024 * 1024]; + memory.add(bytes); + return "Current length is " + memory.size(); + } +} \ No newline at end of file diff --git a/java-17-sample/src/main/java/com/azure/asa/sample/Java17SampleApplication.java b/java-17-sample/src/main/java/com/azure/asa/sample/Java17SampleApplication.java new file mode 100644 index 0000000..ef566bf --- /dev/null +++ b/java-17-sample/src/main/java/com/azure/asa/sample/Java17SampleApplication.java @@ -0,0 +1,13 @@ +package com.azure.asa.sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Java17SampleApplication { + + public static void main(String[] args) { + SpringApplication.run(Java17SampleApplication.class, args); + } + +} diff --git a/java-17-sample/src/main/resources/application.properties b/java-17-sample/src/main/resources/application.properties new file mode 100644 index 0000000..293cf65 --- /dev/null +++ b/java-17-sample/src/main/resources/application.properties @@ -0,0 +1,2 @@ +management.endpoints.web.exposure.include=* +management.endpoint.env.show-values=ALWAYS \ No newline at end of file From 9338ef5688589b121530faba5ea600729a2f3ca8 Mon Sep 17 00:00:00 2001 From: Dingmeng Xue Date: Wed, 18 Sep 2024 22:24:41 +0800 Subject: [PATCH 2/4] Add java 17 sample --- config-server-yaml-samples/README.md | 5 +- java-17-sample/.gitignore | 38 ------------- java-17-sample/README.md | 20 +++---- java-17-sample/pom.xml | 34 +++++++++--- .../azure/asa/sample/EurekaController.java | 54 +++++++++++++++++++ .../com/azure/asa/sample/HelloController.java | 3 +- .../src/main/resources/application.properties | 6 ++- 7 files changed, 97 insertions(+), 63 deletions(-) delete mode 100644 java-17-sample/.gitignore create mode 100644 java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java diff --git a/config-server-yaml-samples/README.md b/config-server-yaml-samples/README.md index 6ed90bd..253f06b 100644 --- a/config-server-yaml-samples/README.md +++ b/config-server-yaml-samples/README.md @@ -20,7 +20,4 @@ The more complex like pattern matching on the application and profile name is al ### Reference -1. https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.1.4.RELEASE/single/spring-cloud-config.html#_git_backend -2. https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.1.4.RELEASE/single/spring-cloud-config.html#_authentication -3. https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.1.4.RELEASE/single/spring-cloud-config.html#_git_ssh_configuration_using_properties -4. https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.1.4.RELEASE/single/spring-cloud-config.html#_pattern_matching_and_multiple_repositories +1. https://docs.spring.io/spring-cloud-config/reference/server/environment-repository/git-backend.html \ No newline at end of file diff --git a/java-17-sample/.gitignore b/java-17-sample/.gitignore deleted file mode 100644 index 5ff6309..0000000 --- a/java-17-sample/.gitignore +++ /dev/null @@ -1,38 +0,0 @@ -target/ -!.mvn/wrapper/maven-wrapper.jar -!**/src/main/**/target/ -!**/src/test/**/target/ - -### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ -*.iws -*.iml -*.ipr - -### Eclipse ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ -build/ -!**/src/main/**/build/ -!**/src/test/**/build/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store \ No newline at end of file diff --git a/java-17-sample/README.md b/java-17-sample/README.md index 521c169..17a9898 100644 --- a/java-17-sample/README.md +++ b/java-17-sample/README.md @@ -1,25 +1,25 @@ +# Local Development + +```shell +docker run --rm -p 8761:8761 springcloud/demo-eureka-server +``` + # Prepare Variables ```shell -SUBSCRIPTION=6c933f90-8115-4392-90f2-7077c9fa5dbd -LOCATION=westus2 -RESOURCE_GROUP=dixue-asas-vnet-prod -SERVICE_NAME=dixue-asas-vnet-prod +SUBSCRIPTION= +LOCATION= +RESOURCE_GROUP= +SERVICE_NAME= az account set --subscription $SUBSCRIPTION ``` # Standard Plan -## Provision ASA Service Instance -```shell -``` ## Deploy Application ```shell mvn clean package az spring app create -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --runtime-version Java_17 --assign-endpoint true az spring app deploy -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --artifact-path ./target/java-17-sample-1.0-SNAPSHOT.jar ``` -# Enterprise Plan -## Provision ASA Service Instance -## Deploy Application diff --git a/java-17-sample/pom.xml b/java-17-sample/pom.xml index a652304..91d74ee 100644 --- a/java-17-sample/pom.xml +++ b/java-17-sample/pom.xml @@ -8,17 +8,10 @@ java-17-sample 1.0-SNAPSHOT - - org.springframework.boot - spring-boot-starter-parent - 3.3.2 - - 17 17 UTF-8 - 2023.0.3 @@ -42,14 +35,31 @@ org.springframework.cloud spring-cloud-starter-netflix-eureka-client + + org.apache.httpcomponents + httpclient + 4.5.14 + + + com.fasterxml.woodstox + woodstox-core + 6.4.0 + + + org.springframework.boot + spring-boot-dependencies + 3.3.3 + pom + import + org.springframework.cloud spring-cloud-dependencies - ${spring-cloud.version} + 2023.0.3 pom import @@ -61,6 +71,14 @@ org.springframework.boot spring-boot-maven-plugin + 3.3.3 + + + + repackage + + + diff --git a/java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java b/java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java new file mode 100644 index 0000000..00f7610 --- /dev/null +++ b/java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java @@ -0,0 +1,54 @@ +package com.azure.asa.sample; + +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; +import com.netflix.discovery.shared.Application; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.Set; + +@RestController +public class EurekaController { + + @Autowired + private EurekaClient discoveryClient; + + @GetMapping("/eureka") + public String getEurekaState() { + StringBuilder sb = new StringBuilder(); + + sb.append("

Known Regions

"); + sb.append("
    "); + Set regions = discoveryClient.getAllKnownRegions(); + for(String region : regions) { + sb.append("
  • ").append(region).append("
  • "); + } + sb.append("
"); + + List apps = discoveryClient.getApplications().getRegisteredApplications(); + sb.append("

Registered Applications

"); + sb.append("
    "); + for(Application app : apps) { + sb.append("

    Application=").append(app.getName()).append("

    "); + sb.append("
      "); + List instances = app.getInstances(); + for(InstanceInfo instance : instances) { + sb.append("
        "); + sb.append("
      • InstanceId=").append(instance.getInstanceId()).append("
      • "); + sb.append("
      • Status=").append(instance.getStatus().toString()).append("
      • "); + sb.append("
      • IPAddr=").append(instance.getIPAddr()).append("
      • "); + sb.append("
      "); + } + sb.append("
    "); + } + sb.append("
"); + + sb.append("

Client Configuration

"); + sb.append("
    ").append(discoveryClient.getEurekaClientConfig().toString()).append("
"); + + return sb.toString(); + } +} diff --git a/java-17-sample/src/main/java/com/azure/asa/sample/HelloController.java b/java-17-sample/src/main/java/com/azure/asa/sample/HelloController.java index 19fa35a..a409f9e 100644 --- a/java-17-sample/src/main/java/com/azure/asa/sample/HelloController.java +++ b/java-17-sample/src/main/java/com/azure/asa/sample/HelloController.java @@ -1,6 +1,5 @@ package com.azure.asa.sample; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @@ -13,7 +12,7 @@ public class HelloController { @GetMapping("/") public String index() { - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append("

Greetings from Spring Boot!

"); return sb.toString(); diff --git a/java-17-sample/src/main/resources/application.properties b/java-17-sample/src/main/resources/application.properties index 293cf65..cf07a6a 100644 --- a/java-17-sample/src/main/resources/application.properties +++ b/java-17-sample/src/main/resources/application.properties @@ -1,2 +1,6 @@ +spring.application.name=java-17-sample management.endpoints.web.exposure.include=* -management.endpoint.env.show-values=ALWAYS \ No newline at end of file +management.endpoint.env.show-values=ALWAYS +eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ +eureka.client.healthcheck.enabled=true +spring.cloud.config.enabled=false \ No newline at end of file From de14bb8dbc156b405faaee2a357049224b87c19c Mon Sep 17 00:00:00 2001 From: Dingmeng Xue Date: Thu, 26 Dec 2024 15:58:39 +0800 Subject: [PATCH 3/4] update code --- java-17-sample/README.md | 5 +++-- java-17-sample/src/main/resources/application.properties | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/java-17-sample/README.md b/java-17-sample/README.md index 521c169..bf58324 100644 --- a/java-17-sample/README.md +++ b/java-17-sample/README.md @@ -11,12 +11,13 @@ az account set --subscription $SUBSCRIPTION # Standard Plan ## Provision ASA Service Instance ```shell - +az spring create --resource-group $RESOURCE_GROUP --name $SERVICE_NAME --sku standard --location $LOCATION +az spring app create -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --runtime-version Java_17 --assign-endpoint true ``` + ## Deploy Application ```shell mvn clean package -az spring app create -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --runtime-version Java_17 --assign-endpoint true az spring app deploy -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --artifact-path ./target/java-17-sample-1.0-SNAPSHOT.jar ``` # Enterprise Plan diff --git a/java-17-sample/src/main/resources/application.properties b/java-17-sample/src/main/resources/application.properties index 293cf65..786e6b8 100644 --- a/java-17-sample/src/main/resources/application.properties +++ b/java-17-sample/src/main/resources/application.properties @@ -1,2 +1,4 @@ management.endpoints.web.exposure.include=* -management.endpoint.env.show-values=ALWAYS \ No newline at end of file +management.endpoint.env.show-values=ALWAYS +spring.cloud.config.enabled=false +eureka.client.enabled=false \ No newline at end of file From 57ebef21ed2d80311cb8813f4c6dc99f96b3589e Mon Sep 17 00:00:00 2001 From: Dingmeng Xue Date: Thu, 26 Dec 2024 17:50:32 +0800 Subject: [PATCH 4/4] Add managed identity --- java-17-sample/pom.xml | 17 +++++++++--- .../azure/asa/sample/EurekaController.java | 2 +- .../sample/ManagedIdentityTestController.java | 26 +++++++++++++++++++ .../RestResponseEntityExceptionHandler.java | 21 +++++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 java-17-sample/src/main/java/com/azure/asa/sample/ManagedIdentityTestController.java create mode 100644 java-17-sample/src/main/java/com/azure/asa/sample/RestResponseEntityExceptionHandler.java diff --git a/java-17-sample/pom.xml b/java-17-sample/pom.xml index 91d74ee..182da66 100644 --- a/java-17-sample/pom.xml +++ b/java-17-sample/pom.xml @@ -35,6 +35,10 @@ org.springframework.cloud spring-cloud-starter-netflix-eureka-client + + com.azure + azure-identity + org.apache.httpcomponents httpclient @@ -43,7 +47,7 @@ com.fasterxml.woodstox woodstox-core - 6.4.0 + 7.1.0
@@ -52,14 +56,21 @@ org.springframework.boot spring-boot-dependencies - 3.3.3 + 3.4.1 pom import org.springframework.cloud spring-cloud-dependencies - 2023.0.3 + 2024.0.0 + pom + import + + + com.azure + azure-sdk-bom + 1.2.30 pom import diff --git a/java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java b/java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java index 00f7610..e583bb8 100644 --- a/java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java +++ b/java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java @@ -13,7 +13,7 @@ @RestController public class EurekaController { - @Autowired + @Autowired(required = false) private EurekaClient discoveryClient; @GetMapping("/eureka") diff --git a/java-17-sample/src/main/java/com/azure/asa/sample/ManagedIdentityTestController.java b/java-17-sample/src/main/java/com/azure/asa/sample/ManagedIdentityTestController.java new file mode 100644 index 0000000..9fc0e23 --- /dev/null +++ b/java-17-sample/src/main/java/com/azure/asa/sample/ManagedIdentityTestController.java @@ -0,0 +1,26 @@ +package com.azure.asa.sample; + +import com.azure.core.credential.AccessToken; +import com.azure.core.credential.TokenRequestContext; +import com.azure.identity.ManagedIdentityCredential; +import com.azure.identity.ManagedIdentityCredentialBuilder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ManagedIdentityTestController { + + @GetMapping("/mi/token") + public String getAccessToken(@RequestParam(name = "client-id", required = false) String clientId) { + ManagedIdentityCredentialBuilder managedIdentityCredentialBuilder = new ManagedIdentityCredentialBuilder(); + if(clientId != null) { + managedIdentityCredentialBuilder.clientId(clientId); + } + ManagedIdentityCredential managedIdentityCredential = managedIdentityCredentialBuilder.build(); + AccessToken accessToken = managedIdentityCredential.getToken(new TokenRequestContext().addScopes("https://management.core.windows.net/")).block(); + + return accessToken.getToken(); + } + +} diff --git a/java-17-sample/src/main/java/com/azure/asa/sample/RestResponseEntityExceptionHandler.java b/java-17-sample/src/main/java/com/azure/asa/sample/RestResponseEntityExceptionHandler.java new file mode 100644 index 0000000..4320130 --- /dev/null +++ b/java-17-sample/src/main/java/com/azure/asa/sample/RestResponseEntityExceptionHandler.java @@ -0,0 +1,21 @@ +package com.azure.asa.sample; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@ControllerAdvice +public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { + + @ExceptionHandler(value = {RuntimeException.class}) + protected ResponseEntity handleConflict( + RuntimeException ex, WebRequest request) { + String bodyOfResponse = ex.getMessage(); + return handleExceptionInternal(ex, bodyOfResponse, + new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); + } +}