Spring cloud sleuth

sleuth

随着业务的发展, 系统的规模也会变大, 各个服务之间的调用变得更加的复杂。 通常一个由客户端发起的请求
会交给好一个服务进行处理返回一个最终的结果, 那么后台的服务是如何调用的呢, 是怎么样的依赖关系, sleuth
为服务的访问路径追踪提供了一个很好的机制。

整合zipkin

构成部分 collector, storage, restful API , web UI  
  
启动zipkin-server :  java -jar zipkin-server-2.10.1-exec.jar
    
链接:https://pan.baidu.com/s/1Fdhwj6POC-MWZ3l5nxydOA 
提取码:eg71 
复制这段内容后打开百度网盘手机App,操作更方便哦

启动eureka-server , sleuth-client , sleuth-server
访问:http://localhost:8189/hi  , http://localhost:8188/hi
访问:http://localhost:9411/zipkin
会出现访问的请求路径

sleuth server

<?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.ninuxGithub</groupId>
    <artifactId>spring-cloud-sleuth-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-sleuth-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>spring-cloud-eureka-master</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
@SpringBootApplication
@EnableEurekaClient
@RestController
public class SleuthServerApplication {

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

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

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/info")
    public String info() {
        return "sleuth-server";
    }

    @RequestMapping(value = "/hi")
    public String callHome() {
        return restTemplate.getForObject("http://localhost:8188/info", String.class);
    }

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

}

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8189
spring:
  application:
    name: sleuth-server

  zipkin:
      base-url: http://localhost:9411

sleuth client

<?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.ninuxGithub</groupId>
    <artifactId>spring-cloud-sleuth-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-sleuth-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>spring-cloud-eureka-master</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

package com.ninuxgithub.sleuthclient;

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
@EnableEurekaClient
public class SleuthClientApplication {

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


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

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/info")
    public String info() {
        return "sleuth-client";
    }

    @RequestMapping(value = "/hi")
    public String callHome() {
        return restTemplate.getForObject("http://localhost:8189/info", String.class);
    }

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

}




eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8188
spring:
  application:
    name: sleuth-client

  zipkin:
      base-url: http://localhost:9411