4

我们选择使用 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?不然怎么办?

4

2 回答 2

3

答对了!

我们需要如下设置 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();

更好的是,您可以从 Environment 创建配置,如下所示,提供如下环境变量

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 

“……

于 2018-08-07T06:18:44.197 回答
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.ReporterConfiguration reportConfig = 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();
    }
}
于 2019-12-04T06:43:41.773 回答