gpt4 book ai didi

java - AWS XRay SDK 无法读取 Docker 容器内的环境变量

转载 作者:行者123 更新时间:2023-12-01 18:01:11 27 4
gpt4 key购买 nike

AWS XRay 是一项跟踪服务,可让您跟踪分布式系统中的请求,甚至分析您的服务。无需过多了解 XRay 的工作原理,它基本上会监视您的服务,并通过 UDP 将有关每个服务请求的数据发送到守护程序,该守护程序收集这些数据并将其发送到 AWS。

当在本地或 EC2 中运行时,此守护进程位于运行服务的计算机的本地,并且可在端口 2000 上使用。这是守护进程主机位置的默认配置。

在 Kubernetes 中运行时,需要设置一个守护进程在每个节点上运行。根据documentation for setting up XRay with Kubernetes ,您可以通过使用所需主机设置环境变量 AWS_XRAY_DAEMON_ADDRESS 来覆盖默认值,也可以设置 JVM 系统变量 com.amazonaws.xray.emitters.daemonAddressSDK documentation 中也有对此的引用。 .

由于我的用例以及我们在组织中共享配置的方式,我想使用设置环境变量的方法。

根据文档,我们通过 helm 图表将其设置为部署:

env:
- name: AWS_XRAY_DAEMON_ADDRESS
value: aws-xray-daemon.default

通过执行运行服务的 pod,并运行 printenv,我们可以看到该值在部署时已成功设置。

<小时/>

问题:

当 XRay 尝试分析并向守护程序发送数据时,会引发 SdkClientException:

com.amazonaws.SdkClientException: Unable to execute HTTP request: Connect to 127.0.0.1:2000 [/127.0.0.1] failed: Connection refused (Connection refused)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleRetryableException(AmazonHttpClient.java:1201) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1147) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:796) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:764) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:738) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:698) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:680) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:544) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:524) ~[aws-java-sdk-core-1.11.739.jar!/:na]
at com.amazonaws.services.xray.AWSXRayClient.doInvoke(AWSXRayClient.java:1607) ~[aws-java-sdk-xray-1.11.739.jar!/:na]
at com.amazonaws.services.xray.AWSXRayClient.invoke(AWSXRayClient.java:1574) ~[aws-java-sdk-xray-1.11.739.jar!/:na]
at com.amazonaws.services.xray.AWSXRayClient.invoke(AWSXRayClient.java:1563) ~[aws-java-sdk-xray-1.11.739.jar!/:na]
at com.amazonaws.services.xray.AWSXRayClient.executeGetSamplingRules(AWSXRayClient.java:800) ~[aws-java-sdk-xray-1.11.739.jar!/:na]
at com.amazonaws.services.xray.AWSXRayClient.getSamplingRules(AWSXRayClient.java:771) ~[aws-java-sdk-xray-1.11.739.jar!/:na]
at com.amazonaws.xray.strategy.sampling.pollers.RulePoller.pollRule(RulePoller.java:65) ~[aws-xray-recorder-sdk-core-2.4.0.jar!/:na]
at com.amazonaws.xray.strategy.sampling.pollers.RulePoller.lambda$start$0(RulePoller.java:46) ~[aws-xray-recorder-sdk-core-2.4.0.jar!/:na]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[na:na]
at java.base/java.util.concurrent.FutureTask.runAndReset(Unknown Source) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:na]
at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]
...

这意味着 AWS 开发工具包不会按照文档的建议选择此环境变量,而仅使用默认值 127.0.0.1:2000

然后,我深入研究了 SDK 代码,以了解它如何检索此变量,并发现运行它的代码使用 System.getenv("AWS_XRAY_DAEMON_ADDRESS") ,如图所示如下:

    /**
* Environment variable key used to override the address to which UDP packets will be emitted. Valid values are of the form `ip_address:port`. Takes precedence over any system property,
* constructor value, or setter value used.
*/
public static final String DAEMON_ADDRESS_ENVIRONMENT_VARIABLE_KEY = "AWS_XRAY_DAEMON_ADDRESS";

/**
* System property key used to override the address to which UDP packets will be emitted. Valid values are of the form `ip_address:port`. Takes precedence over any constructor or setter value
* used.
*/
public static final String DAEMON_ADDRESS_SYSTEM_PROPERTY_KEY = "com.amazonaws.xray.emitters.daemonAddress";

public DaemonConfiguration() {
String environmentAddress = System.getenv(DAEMON_ADDRESS_ENVIRONMENT_VARIABLE_KEY);
String systemAddress = System.getProperty(DAEMON_ADDRESS_SYSTEM_PROPERTY_KEY);

if (setUDPAndTCPAddress(environmentAddress)) {
logger.info(String.format("Environment variable %s is set. Emitting to daemon on address %s.", DAEMON_ADDRESS_ENVIRONMENT_VARIABLE_KEY, getUDPAddress()));
} else if (setUDPAndTCPAddress(systemAddress)) {
logger.info(String.format("System property %s is set. Emitting to daemon on address %s.", DAEMON_ADDRESS_SYSTEM_PROPERTY_KEY, getUDPAddress()));
}
}

所以我想,也许是我没有正确设置环境变量?于是我添加了服务启动时检索环境变量的日志,发现JVM确实可以找到该值:

代码:

System.out.println("System.getenv(\"AWS_XRAY_DAEMON_ADDRESS\")" + " = " + System.getenv("AWS_XRAY_DAEMON_ADDRESS")) 

输出:

System.getenv("AWS_XRAY_DAEMON_ADDRESS") = aws-xray-daemon.default

据我所知,这段代码与AWS SDK应该运行的代码完全匹配,但它似乎从未被执行过,即使是这样,它也不会产生与我所执行的结果相同的结果用我的日志进行了测试。

在本地运行时,我无法复制此问题,因为它会获取我从本地环境变量中指定的主机。我还确认,使用断点在本地运行时可以到达上面粘贴的 AWS SDK 代码。

有什么想法吗?

<小时/>

Gradle 片段:

ext {
...
springCloudVersion = "Greenwich.RELEASE"
awsCoreVersion = '1.11.739'
awsXrayVersion = '2.4.0'
...
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "com.amazonaws:aws-java-sdk-bom:${awsCoreVersion}"
mavenBom "com.amazonaws:aws-xray-recorder-sdk-bom:${awsXrayVersion}"
}
}

dependencies {
...

implementation "com.amazonaws:aws-java-sdk-core"
implementation "com.amazonaws:aws-xray-recorder-sdk-core"
implementation "com.amazonaws:aws-xray-recorder-sdk-aws-sdk"
implementation "com.amazonaws:aws-xray-recorder-sdk-spring"
implementation "com.amazonaws:aws-xray-recorder-sdk-apache-http"
implementation "com.amazonaws:aws-xray-recorder-sdk-sql-postgres"

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'

...

}

其他信息:

  • 在 Spring Boot v2.2.1 中运行
  • OpenJDK v11.0.4
  • Gradle v6.0.1

其他尝试: - 我尝试通过 Dockerfile 设置环境变量。这产生了相同的结果。

最佳答案

事实证明blog post我链接的博客文章不是很好。在示例中,他们没有指定主机的端口:

env:
- name: AWS_XRAY_DAEMON_ADDRESS
value: xray-service.default

更改环境变量以包含端口修复了问题:

env:
- name: AWS_XRAY_DAEMON_ADDRESS
value: xray-service.default:2000

关于java - AWS XRay SDK 无法读取 Docker 容器内的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60622893/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com