美文网首页sporingboot
Spring Boot 2.0 + zipkin 分布式跟踪系统

Spring Boot 2.0 + zipkin 分布式跟踪系统

作者: MaxHongxi | 来源:发表于2018-06-20 16:53 被阅读317次

    注意

    Spring Boot 2.0之后,使用EnableZipkinServer创建自定义的zipkin服务器已经被废弃,将无法启动
    具体可看issue
    Deprecates EnableZipkinServer to explain custom servers are unsupported

    开始

    搭建zipkin服务器

    获取最新发布的zipkin服务器,一个可执行的jar

    curl -sSL https://zipkin.io/quickstart.sh | bash -s
    

    然后启动它

    java -jar zipkin.jar
    

    也可以通过docker来启动

    docker run -d -p 9411:9411 openzipkin/zipkin
    

    启动后就可以在浏览器打开http://your_host:9411/zipkin/查看了。

    创建两个服务

    pom.xml

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <sleuth.version>2.0.0.RELEASE</sleuth.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!-- Sleuth automatically adds trace interceptors when in the classpath -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-sleuth</artifactId>
            </dependency>
            <!-- Sends trace data to zipkin over http (defaults to http://localhost:9411/api/v2/spans) -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-sleuth-zipkin</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-sleuth</artifactId>
                    <version>${sleuth.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    

    Backend.java

    @EnableAutoConfiguration
    @RestController
    public class Backend {
    
      @RequestMapping("/api") public String printDate() {
        return new Date().toString() + " by hongxi";
      }
    
      public static void main(String[] args) {
        SpringApplication.run(Backend.class,
            "--spring.application.name=backend",
            "--server.port=9000"
        );
      }
    }
    

    Frontend.java

    @EnableAutoConfiguration
    @RestController
    @CrossOrigin // So that javascript can be hosted elsewhere
    public class Frontend {
    
      @Autowired RestTemplate restTemplate;
    
      @RequestMapping("/") public String callBackend() {
        return restTemplate.getForObject("http://localhost:9000/api", String.class);
      }
    
      @Bean RestTemplate restTemplate() {
        return new RestTemplate();
      }
    
      public static void main(String[] args) {
        SpringApplication.run(Frontend.class,
            "--spring.application.name=frontend",
            "--server.port=8081"
        );
      }
    }
    

    application.properties

    spring.sleuth.traceId128=true
    spring.sleuth.sampler.probability=1.0
    

    然后分别启动Backen和Frontend,在浏览器访问http://localhost:8080/,可以看到页面显示

    Wed Jun 20 16:47:00 CST 2018 by hongxi
    

    说明调用成功,然后回到http://your_host:9411/zipkin/点击大大的那个按钮“Find Traces”即可看到调用跟踪信息。

    WX20180620-164932@2x.png

    相关文章

      网友评论

        本文标题:Spring Boot 2.0 + zipkin 分布式跟踪系统

        本文链接:https://www.haomeiwen.com/subject/kqyxyftx.html