SpringCloud 2.x学习笔记:8、Spring Cloud Sleuth(Greenwich版本)

1、服务追踪组件zipkin

Spring Cloud Sleuth集成了zipkin组件

2、构建server-zipkin

从spring Cloud为F版本开始,已经不需要自己构建Zipkin Server了,只需要下载jar运行即可。

命令:java -jar zipkin-server-2.12.9-exec.jar

D:\Users\chengyq>c:

C:\>java -jar zipkin-server-2.12.9-exec.jar

浏览器打开

http://localhost:9411

3、构建server-x

3.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cntaiping.tpa</groupId>
    <artifactId>server-x</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>server-x</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.cntaiping.tpa</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>

</project>

3.2 application.properties

server.port=7201
spring.zipkin.base-url=http://localhost:9411
spring.application.name=server-x

3.3 Application类

package com.cntaiping.tpa.serverx;

import brave.sampler.Sampler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ServerXApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerXApplication.class, args);
    }

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }
}

3.4 控制器

package com.cntaiping.tpa.serverx.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.logging.Level;

@RestController
public class XController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RestTemplate restTemplate;



    @RequestMapping("/hello")
    public String hello(){
        logger.info("calling trace server-x ");
        return restTemplate.getForObject("http://localhost:7202/info", String.class);
    }
    @RequestMapping("/info")
    public String info(){
        logger.info("calling trace server-x ");
        return "i'm server-x";
    }

}

4、构建server-y

构建server-y与构建构建server-x过程相同,不同的项目文件如下

4.1 application.properties

server.port=7202
spring.zipkin.base-url=http://localhost:9411
spring.application.name=server-y

4.2 控制器

package com.cntaiping.tpa.servery.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class YController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/hello")
    public String hello(){
        logger.info("calling trace server-y ");
        return restTemplate.getForObject("http://localhost:7201/info", String.class);
    }
    @RequestMapping("/info")
    public String info(){
        logger.info("calling trace server-y ");
        return "i'm server-y";
    }

}

5、运行效果

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券