From 5fc1065d8cb81fcd68a9ab95d6b0b5846bb25aaf Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 21 Feb 2024 11:36:22 +0800 Subject: [PATCH] Allows overriding the BACKEND_ENDPOINT via env When deployed in the same k8s pod, services communicate by localhost despite being in different docker containers. By adding `BACKEND_ENDPOINT`, we can set this to "http://localhost:9000/api" in a k8s "extra container', so that we can do "helm test" on it. See https://kubernetes.io/docs/concepts/workloads/pods/#pod-networking Signed-off-by: Adrian Cole --- .../src/main/java/brave/example/Frontend.java | 9 +++++++- docker/bin/install-example | 21 ++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/armeria/src/main/java/brave/example/Frontend.java b/armeria/src/main/java/brave/example/Frontend.java index d8dc57d..d3dd98d 100644 --- a/armeria/src/main/java/brave/example/Frontend.java +++ b/armeria/src/main/java/brave/example/Frontend.java @@ -8,14 +8,21 @@ import com.linecorp.armeria.server.healthcheck.HealthCheckService; import com.linecorp.armeria.server.logging.AccessLogWriter; import com.linecorp.armeria.server.logging.LoggingService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public final class Frontend { + static final Logger LOGGER = LoggerFactory.getLogger(HttpTracingFactory.class); public static void main(String[] args) { final HttpTracing httpTracing = HttpTracingFactory.create("frontend"); + final String backendEndpoint = + System.getProperty("backend.endpoint", "http://127.0.0.1:9000/api"); + LOGGER.info("Using backend endpoint: {}", backendEndpoint); + final WebClient backendClient = - WebClient.builder(System.getProperty("backend.endpoint", "http://127.0.0.1:9000/api")) + WebClient.builder(backendEndpoint) .decorator(BraveClient.newDecorator(httpTracing.clientOf("backend"))) .build(); diff --git a/docker/bin/install-example b/docker/bin/install-example index 79d6545..2167a53 100755 --- a/docker/bin/install-example +++ b/docker/bin/install-example @@ -28,19 +28,23 @@ tee -a start-frontend start-backend <<-'EOF' export IP="$(hostname -i || echo '127.0.0.1')" EOF +# Declare variables that expand variables set in this shell script/ +# These don't need to be exported as they are converted to java properties. cat >> start-frontend <<-EOF -export PORT=${frontend_port} -export EXAMPLE=${version} -export EXAMPLE_SERVICE=frontend +PORT=${frontend_port} +EXAMPLE=${version} +EXAMPLE_SERVICE=frontend +DEFAULT_BACKEND_ENDPOINT=http://backend:${backend_port}/api EOF cat >> start-backend <<-EOF -export PORT=${backend_port} -export EXAMPLE=${version} -export EXAMPLE_SERVICE=backend +PORT=${backend_port} +EXAMPLE=${version} +EXAMPLE_SERVICE=backend EOF -# Add common elements for Java applications +# Add common elements for Java applications. +# HEALTHCHECK variables need to be exported as they are read via proc info. tee -a start-frontend start-backend <<-'EOF' export HEALTHCHECK_IP=${IP} export HEALTHCHECK_PORT=${PORT} @@ -55,7 +59,8 @@ exec java ${JAVA_OPTS} -cp 'classes:lib/*' \ -Dzipkin.baseUrl=${ZIPKIN_BASEURL:-http://zipkin:9411/} \ EOF -echo "-Dbackend.endpoint=http://backend:${backend_port}/api \\" >> start-frontend +# Allow override of the backend endpoint, for a multi-container pod. +echo '-Dbackend.endpoint=${BACKEND_ENDPOINT:-${DEFAULT_BACKEND_ENDPOINT}} \' >> start-frontend # Add the main class echo 'brave.example.Frontend "$@"' >> start-frontend