Java Tips : Azure Web Appの環境変数を取得して出力する

AppService Plan

前回まででJava プログラムをAzure Web Appにデプロイして実行できるようになったので、デプロイしたAzure Web Appの環境情報(=環境変数)にはどのようなものがあるのか確認してみる。

環境変数を用いて、今後Slotの動作確認やスケールアウト・インの動作確認で利用できると思うのでそのための前準備とする。

Javaプログラム

環境変数のKeyとValueの組み合わせを1つの文字列とし、それをさらにHTMLのbrタグをセパレーターとして連結して出力するプログラム。

package com.example.azureappservice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class AzureAppServiceApplication {

	@GetMapping("/")
	String printEnvs() {
		List<String> workList = new ArrayList<String>();
		Map<String, String> envMap = System.getenv();
		for (Map.Entry<String, String> entry : envMap.entrySet()) {
			workList.add(entry.getKey() + "[" + entry.getValue() + "]");
		}
		return String.join("<br>", workList);
	}
	
	public static void main(String[] args) {
		SpringApplication.run(AzureAppServiceApplication.class, args);
	}

}

ビルド&デプロイ

上記のプログラムをmavenプラグインでコンパイル&デプロイする。

% mvn clean package com.microsoft.azure:azure-webapp-maven-plugin:2.9.0:deploy
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.example:AzureAppService >---------------------
[INFO] Building AzureAppService 0.0.1-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- clean:3.3.2:clean (default-clean) @ AzureAppService ---
[INFO] Deleting /Users/sato/proj/learn/java/SpringTips/AzureAppService/target
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ AzureAppService ---
[INFO] Copying 1 resource from src/main/resources to target/classes
[INFO] Copying 0 resource from src/main/resources to target/classes
[INFO] 
[INFO] --- compiler:3.11.0:compile (default-compile) @ AzureAppService ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 1 source file with javac [debug release 17] to target/classes
[INFO] 
[INFO] --- resources:3.3.1:testResources (default-testResources) @ AzureAppService ---
[INFO] skip non existing resourceDirectory /Users/sato/proj/learn/java/SpringTips/AzureAppService/src/test/resources
[INFO] 
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ AzureAppService ---
[INFO] Changes detected - recompiling the module! :dependency
[INFO] Compiling 1 source file with javac [debug release 17] to target/test-classes
[INFO] 
[INFO] --- surefire:3.1.2:test (default-test) @ AzureAppService ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.azureappservice.AzureAppServiceApplicationTests
00:08:52.112 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.azureappservice.AzureAppServiceApplicationTests]: AzureAppServiceApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
00:08:52.220 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.example.azureappservice.AzureAppServiceApplication for test class com.example.azureappservice.AzureAppServiceApplicationTests

  .   ____          _            __ _ _
 /\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\
( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\
 \\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.2.2)

2024-02-05T00:08:52.602+09:00  INFO 11504 --- [           main] c.e.a.AzureAppServiceApplicationTests    : Starting AzureAppServiceApplicationTests using Java 21.0.1 with PID 11504 (started by sato in /Users/sato/proj/learn/java/SpringTips/AzureAppService)
2024-02-05T00:08:52.604+09:00  INFO 11504 --- [           main] c.e.a.AzureAppServiceApplicationTests    : No active profile set, falling back to 1 default profile: "default"
2024-02-05T00:08:53.736+09:00  INFO 11504 --- [           main] c.e.a.AzureAppServiceApplicationTests    : Started AzureAppServiceApplicationTests in 1.388 seconds (process running for 2.318)
WARNING: A Java agent has been loaded dynamically (/Users/sato/.m2/repository/net/bytebuddy/byte-buddy-agent/1.14.11/byte-buddy-agent-1.14.11.jar)
WARNING: If a serviceability tool is in use, please run with -XX:+EnableDynamicAgentLoading to hide this warning
WARNING: If a serviceability tool is not in use, please run with -Djdk.instrument.traceUsage for more information
WARNING: Dynamic loading of agents will be disallowed by default in a future release
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.001 s -- in com.example.azureappservice.AzureAppServiceApplicationTests
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- jar:3.3.0:jar (default-jar) @ AzureAppService ---
[INFO] Building jar: /Users/sato/proj/learn/java/SpringTips/AzureAppService/target/AzureAppService-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot:3.2.2:repackage (repackage) @ AzureAppService ---
[INFO] Replacing main artifact /Users/sato/proj/learn/java/SpringTips/AzureAppService/target/AzureAppService-0.0.1-SNAPSHOT.jar with repackaged archive, adding nested dependencies in BOOT-INF/.
[INFO] The original artifact has been renamed to /Users/sato/proj/learn/java/SpringTips/AzureAppService/target/AzureAppService-0.0.1-SNAPSHOT.jar.original
[INFO] 
[INFO] --- azure-webapp:2.9.0:deploy (default-cli) @ AzureAppService ---
Downloading from azure-sdk-for-java: <https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-java/maven/v1/net/minidev/json-smart/maven-metadata.xml>
Downloading from shibboleth-repo: <https://build.shibboleth.net/nexus/content/repositories/releases/net/minidev/json-smart/maven-metadata.xml>
Downloading from ossrh: <https://oss.sonatype.org/content/repositories/snapshots/net/minidev/json-smart/maven-metadata.xml>
[INFO] Auth type: AZURE_CLI
[INFO] Default subscription: GBS Azure Sandbox(51cf59b2-3184-4d6c-abb6-6ab69af3415c)
[INFO] Username: IOCC_sato@gbssandbox.onmicrosoft.com
[INFO] Subscription: GBS Azure Sandbox(51cf59b2-3184-4d6c-abb6-6ab69af3415c)
[INFO] Trying to deploy external resources to azure-spring-workshop...
[INFO] Successfully deployed the resources to azure-spring-workshop
[INFO] Trying to deploy artifact to azure-spring-workshop...
[INFO] Deploying (/Users/sato/proj/learn/java/SpringTips/AzureAppService/target/AzureAppService-0.0.1-SNAPSHOT.jar)[jar]  ...
[INFO] Using service version null
[INFO] Using service version null
[INFO] Deployment Status: BuildSuccessful; Successful Instance Count: 0; In-progress Instance Count: 0; Failed Instance Count: 0
[INFO] Deployment Status: RuntimeStarting; Successful Instance Count: 0; In-progress Instance Count: 1; Failed Instance Count: 0
[INFO] Deployment Status: RuntimeStarting; Successful Instance Count: 0; In-progress Instance Count: 1; Failed Instance Count: 0
[INFO] Deployment Status: RuntimeStarting; Successful Instance Count: 0; In-progress Instance Count: 1; Failed Instance Count: 0
[INFO] Deployment Status: RuntimeStarting; Successful Instance Count: 0; In-progress Instance Count: 1; Failed Instance Count: 0
[INFO] Deployment Status: RuntimeStarting; Successful Instance Count: 0; In-progress Instance Count: 1; Failed Instance Count: 0
[INFO] Deployment Status: RuntimeStarting; Successful Instance Count: 0; In-progress Instance Count: 1; Failed Instance Count: 0
[INFO] Deployment Status: RuntimeSuccessful; Successful Instance Count: 1; In-progress Instance Count: 0; Failed Instance Count: 0
[INFO] Successfully deployed the artifact to <https://azure-spring-workshop.azurewebsites.net>
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:05 min
[INFO] Finished at: 2024-02-05T00:10:54+09:00
[INFO] ------------------------------------------------------------------------

動作確認

curlでリクエストを投げて、HTTPレスポンスボディーでAzure Web Appインスタンスの環境変数を確認する。1行が長く流石に分かりづらいのでbrタグで改行したものを掲載する。またセキュリティの観点から公開しない方が良い情報はマスクする。

% curl --header "Content-Type: application/json" --request GET <https://azure-spring-workshop.azurewebsites.net/>
PATH[/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]
WEBSITE_OWNER_NAME[xxx+azure-spring-workshop-JapanEastwebspace-Linux]
WEBSITE_RESOURCE_GROUP[azure-spring-workshop]
JAVA_OPTS[-Djava.util.logging.config.file=/usr/local/appservice/logging.properties -Dfile.encoding=UTF-8  -Dserver.port=80 -XX:ErrorFile=/home/LogFiles/java_error_azure-spring-workshop_10-30-0-95_%p.log -XX:+CrashOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/LogFiles/java_memdump_azure-spring-workshop_10-30-0-95.log -Duser.dir=/home/site/wwwroot]
JRE_HOME[/usr/lib/jdk]
WEBSITE_SKU[LinuxFree]
SSH_PORT[2222]
PWD[/]
WEBSITE_SITE_NAME[azure-spring-workshop]
SERVER_PORT[80]
LANGUAGE[en_US:en]
APP_JAR_PATH[/home/site/wwwroot/app.jar:/usr/local/appservice/lib/azure.appservice.jar]
PORT[80]
WEBSITE_AUTH_ENABLED[False]
SERVER_MAXHTTPHEADERSIZE[16384]
WEBSITE_INSTANCE_ID[xxx]
LOGGING_FILE_NAME[/home/LogFiles/Application/spring.10-30-0-95.log]
NUM_CORES[2]
REGION_NAME[japaneast]
LC_ALL[en_US.UTF-8]
GLOBAL_PID_MAIN[0]
WEBJOB_HOME[/home]
WEBSITE_JAVA_KEYSTORE_PASSWORD[changeit]
APPSETTING_WEBSITE_AZMON_ENABLED[True]
SHLVL[0]
WEBSITE_HOME_STAMPNAME[waws-prod-ty1-085]
DIAGNOSTIC_LOGS_MOUNT_PATH[/var/log/diagnosticLogs]
LOGICAPPS_ACCESS_CONTROL_CONFIGURATION[null]
WEBSITE_AUTH_ENCRYPTION_KEY[xxx]
WEBSITE_OS[linux]
WEBSITE_JAVA_MAX_HEAP_MB[716]
WEBSITE_AUTH_SIGNING_KEY[xxx]
JAVA_HOME[/usr/lib/jdk]
LANG[en_US.UTF-8]
JAVA_TOOL_OPTIONS[-Xmx716M -Djava.net.preferIPv4Stack=true ]
WEBSITE_STACK[JAVA]
APPSVC_RUN_ZIP[FALSE]
_[/usr/bin/java]
APPSETTING_WEBSITE_AUTH_ENABLED[False]
APPSETTING_ScmType[None]
GIT_COMMIT[]
WEBSITE_ISOLATION[lxc]
WEBSITE_PHYSICAL_MEMORY_MB[1024]
COMPUTERNAME[10-30-0-95]
GLOBAL_EXIT_AFTER_CUSTOM_STARTUP[1]
WEBJOB_ENV[true]
WEBSITE_USE_DIAGNOSTIC_SERVER[false]
DOCKER_SERVER_VERSION[20.10.25]
ScmType[None]
WEBSITE_ROLE_INSTANCE_ID[0]
APPSETTING_WEBSITE_SITE_NAME[azure-spring-workshop]
LOGGING_FILE[/home/LogFiles/Application/spring.10-30-0-95.log]
HOSTNAME[8fcf022a7569]
PLATFORM_VERSION[101.0.7.490]
WEBSITE_HOSTNAME[azure-spring-workshop.azurewebsites.net]
HOME[/root]

感想

今回の実施内容は少し手応えがなく、調査する事もほとんどなかったが、今後、実行中のプログラムがどのインスタンスで実行されている結果が表示・出力されているのかを確認するための手段として、ホストの環境変数を使った識別ができれば良いと思う。

コメント

タイトルとURLをコピーしました