微服务工程构建(十四)16.SpringCloud Sleuth分布式请求链路追踪

这里写目录标题

  • 一.概述
  • 二.下载安装
    • 1.下载
    • 2.运行控制台:
  • 三.创建eureka7001
  • 四.创建 cloud-provider-payment8001
  • 五.创建 springcloud-sleuth-order80
  • 六.代码

一.概述

1.Spring Cloud Sleuth提供了一套完整的服务跟踪的解决方案
2.在分布式系统中提供追踪解决方案并且兼容支持了zipkin

官网

二.下载安装

官网

1.下载

微服务工程构建(十四)16.SpringCloud Sleuth分布式请求链路追踪_第1张图片
成功
微服务工程构建(十四)16.SpringCloud Sleuth分布式请求链路追踪_第2张图片

2.运行控制台:

http://localhost:9411/zipkin/

微服务工程构建(十四)16.SpringCloud Sleuth分布式请求链路追踪_第3张图片
Trace:类似于树结构的Span集合,表示一条调用链路,存在唯一标识
span:表示调用链路来源,通俗的理解span就是一次请求信息

三.创建eureka7001

微服务工程构建(四) eureka集群环境构建 server与server之间相互注册配置 client修改成多个注册

四.创建 cloud-provider-payment8001

1.pom

<dependencies>
        <!--包含了sleuth+zipkin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>com.psf.springcloud</groupId>
            <artifactId>springcloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.yml

server:
  port: 8001
spring:
  application:
    name: springcloud-payment-service #服务名称
  zipkin:
    base-url: http://localhost:9411
    sleuth:
      sampler:
      probability: 1
eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
  instance:
    instance-id: payment8001  #主机名
    prefer-ip-address: true   #显示ip
     #客户端向服务端发送心跳时间间隔,默认30秒
    lease-renewal-interval-in-seconds:  1
    #服务端收到最后一次时间上限,默认90秒,超过剔除服务
    lease-expiration-duration-in-seconds:  2

3.controller

@RestController
@Slf4j
public class PaymentController {


    @GetMapping("/payment/zipkin")
    public String paymentZipkin()
    {
        return "测试zipkin";
    }
}

五.创建 springcloud-sleuth-order80

  1. pom
<dependencies>
    <!--包含了sleuth+zipkin-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>

    <!--eureka client-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
        <groupId>com.psf.springcloud</groupId>
        <artifactId>springcloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web  -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. yml
server:
  port: 80
spring:
  application:
    name: springcloud-sleuth-order80 #服务名称
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
    probability: 1



#eureka client配置
eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

  1. controller
@RestController
@Slf4j
public class OrderController {
    public static final String url = "http://springcloud-payment-service";
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;


    // ====================> zipkin+sleuth
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/zipkin/", String.class);
        return result;
    }

  1. RestTemplate config
@Configuration
public class ApplicationContextConfig {

    @Bean
    //@LoadBalanced  //赋予RestTemplate负载均衡的能力
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

运行 7001-》8001-》80
http://localhost:80/consumer/payment/zipkin
访问 http:localhost:941
微服务工程构建(十四)16.SpringCloud Sleuth分布式请求链路追踪_第4张图片
微服务工程构建(十四)16.SpringCloud Sleuth分布式请求链路追踪_第5张图片
微服务工程构建(十四)16.SpringCloud Sleuth分布式请求链路追踪_第6张图片

六.代码

代码下载
其余:
微服务工程构建(十二)Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新(2.自动动态刷新)
微服务工程构建(十一)SpringCloud config分布式配置中心(1.半自动)
微服务工程构建(十)spingcloud整合Gateway(网关)
微服务工程构建(九)spingcloud整合eureka、 Hystrix断路器的降级、熔断、图形化
微服务工程构建(八)eureka环境OpenFeign使用
微服务工程构建(四) eureka集群环境构建 server与server之间相互注册配置 client修改成多个注册

END

你可能感兴趣的:(springcloud,sleuth,分布式,java,eureka)