zoukankan      html  css  js  c++  java
  • SpringCloud Sleuth

    1.定义

    Sleuth(分布式请求链路跟踪):提供了一套完整的服务跟踪解决方案,也兼容zipkin。

    参考网址:https://github.com/spring-cloud/spring-cloud-sleuth 

    2.项目开发

    源代码:https://github.com/zhongyushi-git/cloud-sleuth-demo.git

    2.1环境搭建

    这里需要下载zipkin的jar才能使用,以2.12.9版本为例(其他版本可能有所差别),若无法下载,可在阿里云maven仓库下载。

    1)zipkin下载地址:https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/2.14.1/zipkin-server-2.14.1-exec.jar

    2)在下载的jar目录下执行命令

    java -jar zipkin-server-2.14.1-exec.jar
    

    看到下图说明配置成功

     3)访问http://localhost:9411/zipkin/可看到相关的页面,主要用来查看请求的调用记录的。

    2.2父工程搭建

    创建一个maven的父工程cloud-sleuth-demo,导入依赖并指定版本

        <properties>
            <spring.boot.version>2.2.2.RELEASE</spring.boot.version>
            <spring.cloud.version>Hoxton.SR1</spring.cloud.version>
        </properties>
        <!--  依赖管理,父工程锁定版本-->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring.boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring.cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>

    2.3服务提供者模块

    1)创建一个子工程cloud-provider8001作为服务消费者,导入依赖

     <dependencies>
            <!--web-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            </dependency>
            <!--包含了sleuth+zipkin-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zipkin</artifactId>
            </dependency>
        </dependencies>

    2)yml配置

    server:
      port: 8001
    
    spring:
      application:
        name: cloud-provider
      cloud:
        consul:
          host: localhost
          port: 8500
          discovery:
            service-name: ${spring.application.name}
      zipkin:
        #监控地查看址
        base-url: http://localhost:9411
      sleuth:
        sampler:
          #采样率
          probability: 1

    里面主要是配置了ziplin和consul的相关信息。

    3)创建包com.zys.cloud,包下创建启动类

    package com.zys.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class ProviderMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(ProviderMain8001.class, args);
        }
    }

    4)在包下新建controller接口

    package com.zys.cloud.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class UserController {
    
        @Value("${server.port}")
        private String port;
    
        @GetMapping("/user/get")
        public String get() {
            return "provider port is :" + port;
        }
    
    } 

    2.4服务消费者模块

    1)创建一个子工程cloud-provider8001作为服务消费者,导入依赖

        <dependencies>
            <!--web-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            </dependency>
            <!--feign-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!--包含了sleuth+zipkin-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zipkin</artifactId>
            </dependency>

    2)yml配置

    server:
      port: 80
    
    spring:
      application:
        name: cloud-consumer
      cloud:
        consul:
          host: localhost
          port: 8500
          discovery:
            service-name: ${spring.application.name}
      zipkin:
        #监控地查看址
        base-url: http://localhost:9411
      sleuth:
        sampler:
          #采样率
          probability: 1
    
    feign:
      client:
        config:
          #指定全局
          default:
            #连接超时时间
            connectTimeout: 5000
            #服务等待时间
            readTimeout: 5000
            loggerLevel: full

    3)创建包com.zys.cloud,包下创建启动类

    package com.zys.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    public class ConsumerMain80 {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerMain80.class, args);
        }
    }

    4)在包下新建client的客户端

    package com.zys.cloud.client;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    
    //指定微服务名称
    @FeignClient(value = "cloud-provider")
    public interface UserClient {
    
        @GetMapping("/user/get")
        String get();
    }

    5)在包下新建controller接口

    package com.zys.cloud.controller;
    
    import com.zys.cloud.client.UserClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    @RestController
    @RequestMapping("/consumer")
    public class UserController {
    
        @Resource
        private UserClient userClient;
    
        @GetMapping("/get")
        public String get(){
            return userClient.get();
        }
    
    }

    2.5测试

    先启动服务提供者,再启动服务消费者。访问http://localhost/consumer/get,然后再回到zipkin的页面,发现服务名多了两个,分别是设置的消费者和生产者。

     选择一个服务后点击查找,就会显示出链路信息

    就是这么简单,你学废了吗?感觉有用的话,给笔者点个赞吧 !
  • 相关阅读:
    206. Reverse Linked List
    简介AngularJS中使用factory和service的方法
    如何写一手漂亮的模型:面向对象编程的设计原则综述
    webpack入门操作教程
    webpack4.0.1安装问题和webpack.config.js的配置变化
    webpack.config.js配置遇到Error: Cannot find module '@babel/core'&&Cannot find module '@babel/plugin-transform-react-jsx' 问题
    解决webpack打包报错: Cannot find module '@webassemblyjs/wasm-parser'
    docker-compose介绍
    .NET Core+MySql+Nginx 容器化部署
    .net core使用ef core操作mysql数据库
  • 原文地址:https://www.cnblogs.com/zys2019/p/12682179.html
Copyright © 2011-2022 走看看