springBoot链路追踪之jaeger使用

2021-09-28  本文已影响0人  _Kantin

背景

代码

        <dependency>
            <groupId>io.opentracing.contrib</groupId>
            <artifactId>opentracing-spring-web-autoconfigure</artifactId>
            <version>0.0.4</version>
        </dependency>
        <dependency>
            <groupId>com.uber.jaeger</groupId>
            <artifactId>jaeger-core</artifactId>
            <version>0.18.0</version>
        </dependency>
     //opentracing服务端ip
    @Value("${opentracing.report.host}")
    private String agentHost;
    //opentracing服务端port
    @Value("${opentracing.report.port}")
    private Integer agentPort;
    //采样率(0.01表示只采集1%)
    @Value("${opentracing.sampler.percent}")
    private double percent;


    @Bean
    public io.opentracing.Tracer jaegerTracer() {
    //1000表示队列的长度
    Configuration.ReporterConfiguration reporterConfiguration = new Configuration.ReporterConfiguration(null, agentHost, agentPort, null, 1000);
        //spring-boot是显示在jaeger上的应用名字
        return new Configuration("spring-boot", new Configuration.SamplerConfiguration(ProbabilisticSampler.TYPE, percent),
                reporterConfiguration).getTracer();
    }
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RequestMapping("/tracing")
    public String tracing() throws InterruptedException {
        Thread.sleep(100);
        return "tracing";
    }

    @RequestMapping("/open")
    public String open() throws InterruptedException {
        String url = "http://localhost:8080/demo/tracing";
        ResponseEntity<String> response =
                restTemplate.getForEntity(url, String.class);
        Thread.sleep(200);
        return "open " + response.getBody();
    }
上一篇 下一篇

猜你喜欢

热点阅读