[英]How to change the Host and port for Jaeger API in java
我們選擇使用 Jaeger API 來進行跟蹤。 在那里,我們使用 docker 本地設置了 Jaeger,如下所述。
sudo docker run -d --name jaeger \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14268:14268 \
jaegertracing/all-in-one:latest
在ServletContextListener
,我們創建了如下的新配置。
@WebListener
public class TracingContextListener implements ServletContextListener {
@Inject
private io.opentracing.Tracer tracer;
public void contextInitialized(ServletContextEvent servletContextEvent) {
GlobalTracer.register(tracer);
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
@Produces
@Singleton
public static io.opentracing.Tracer jaegerTracer() {
return new Configuration("MyApplication", new Configuration.SamplerConfiguration(
ProbabilisticSampler.TYPE, 1),
new Configuration.ReporterConfiguration())
.getTracer();
}
}
現在這工作正常,我可以在http://localhost:16686 中看到跟蹤
問題:我想在外部環境中設置 Jager 並從另一個應用程序服務器連接(應用程序服務器在主機模式下運行在 wildfly 10 docker 上)。 將來,Jaeger 實例可能會被多個服務器實例用於跟蹤。
在查看了下面提到的來源和各種參考資料后,我嘗試了以下選項。 但它總是連接到本地。 我也嘗試了 5775、6831、6832 等各種端口,但結果是一樣的。
return new Configuration("MyApplication", new Configuration.SamplerConfiguration(
ProbabilisticSampler.TYPE, 1, "server2.mycompany.com:5778"),
new Configuration.ReporterConfiguration())
.getTracer();
此外,我還嘗試將 JAEGER_ENDPOINT 和 JAEGER_SAMPLER_MANAGER_HOST_PORT 設置為環境變量。 但是失敗了。
在一篇參考文獻中,我發現“Jaeger 客戶端庫期望 jaeger-agent 進程在每個主機上本地運行......” 。
這是否意味着我不能以 cebtrelized 的方式使用它,我需要在每個應用程序服務器實例中設置 Jaeger? 不然怎么辦?
答對了!
我們需要如下設置 ReporterConfigurations。 以前我的是默認的,這就是為什么它總是連接到本地。
return new Configuration("MyApplication",
new Configuration.SamplerConfiguration(ProbabilisticSampler.TYPE, 1, "server2.mycompany.com:5778"),
new Configuration.ReporterConfiguration(false, "server2.mycompany.com",6831,1000,100))
.getTracer();
更好的是,您可以從環境創建配置如下,提供如下環境變量
return Configuration.fromEnv().getTracer();
你可以在運行 docker 容器時提供這個
-e JAVA_OPTS="
-DJAEGER_SAMPLER_TYPE=probabilistic -DJAEGER_SAMPLER_PARAM=1 -DJAEGER_SAMPLER_MANAGER_HOST_PORT=server2.mycompany.com:5778 -DJAEGER_REPORTER_LOG_SPANS=false -DJAEGER_AGENT_HOST=server2.mycompany.com -DJAEGER_AGENT_PORT=6831 -DJAEGER_REPORTER_FLUSH_INTERVAL=1000 -DJAEGER_REPORTER_MAX_QUEUE_SIZE=100 -DJAEGER_SERVICE_NAME=MyApplicationNameX
“……
步驟1:首先我們需要配置遠程主機地址和端口。
private static final int JAEGER_PORT = HOST_PORT;
private static final String JAEGER_HOST = "HOST_IP";
第二步:配置sender配置,在withAgentHost、withAgentPort中傳入遠程主機和端口。
SenderConfiguration senderConfig = Configuration.SenderConfiguration.fromEnv() .withAgentHost(JAEGER_HOST) .withAgentPort(JAEGER_PORT);
第 3 步:在報告者配置中傳遞發送者配置
Configuration.ReporterConfigurationreporterConfig = Configuration.ReporterConfiguration.fromEnv() .withLogSpans(true) .withSender(senderConfig);
package com.studies.StudyService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import io.jaegertracing.Configuration;
import io.jaegertracing.Configuration.SenderConfiguration;
import io.jaegertracing.internal.samplers.ProbabilisticSampler;
import io.opentracing.Tracer;
@SpringBootApplication
//@EnableDiscoveryClient
public class StudyServiceApplication {
public static void main(String[] args) {
SpringApplication.run(StudyServiceApplication.class, args);
}
/* Configure sender host and port details */
private static final int JAEGER_PORT = HOST_PORT;
private static final String JAEGER_HOST = "HOST_IP";
/* End */
@Bean
public Tracer getTracer() {
Configuration.SamplerConfiguration samplerConfig = Configuration.SamplerConfiguration.fromEnv()
.withType(ProbabilisticSampler.TYPE).withParam(1);
/* Update default sender configuration with custom host and port */
SenderConfiguration senderConfig = Configuration.SenderConfiguration.fromEnv()
.withAgentHost(JAEGER_HOST)
.withAgentPort(JAEGER_PORT);
/* End */
Configuration.ReporterConfiguration reporterConfig = Configuration.ReporterConfiguration.fromEnv()
.withLogSpans(true)
.withSender(senderConfig);
Configuration config = new Configuration("Service_Name").withSampler(samplerConfig)
.withReporter(reporterConfig);
return config.getTracer();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.