如何在java中更改Jaeger API的主机和端口
我们选择使用Jaeger API进行跟踪。在这里,我们使用docker在本地设置Jaeger,如下所述如何在java中更改Jaeger API的主机和端口,java,trace,jaeger,Java,Trace,Jaeger,我们选择使用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 在ServletCo
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();
}
}
现在这一切都很好,我可以在中看到跟踪
问题:
我想在外部环境中设置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_端点和JAEGER_采样器_管理器_主机_端口设置为环境变量。但失败了
在一个例子中,我发现“Jaeger客户端库期望Jaeger代理进程在每个主机上本地运行…”
这是否意味着我不能以cebtrelized的方式使用它,我需要在每个应用服务器实例中设置Jaeger?否则怎么办?答对了
我们需要如下设置ReporterConfiguration。以前我的是默认的,这就是为什么它总是连接到本地
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=”
“
步骤1:首先我们需要配置远程主机地址和端口
private static final int JAEGER_PORT = HOST_PORT;
private static final String JAEGER_HOST = "HOST_IP";
步骤2:配置发送方配置,并将远程主机和端口传入withAgentHost和withAgentPort
SenderConfiguration SenderConfiguration=Configuration.SenderConfiguration.fromEnv()
.withAgentHost(JAEGER_主机)
.与代理港(JAEGER_港)合作
步骤3:在报告器配置中传递发送器配置
Configuration.ReporterConfiguration reporterConfig=Configuration.ReporterConfiguration.fromEnv()
.withLogSpans(真)
.与Sender(senderConfig)合作
只有代码的答案被认为是低质量的:确保提供一个解释,说明你的代码是做什么的,以及它是如何解决问题的。如果你能在你的文章中添加更多的信息,这将有助于询问者和未来的读者。另请参见解释完全基于代码的答案:带有适当注释的更新答案
private static final int JAEGER_PORT = HOST_PORT;
private static final String JAEGER_HOST = "HOST_IP";
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();
}
}