Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)

导读:本篇文章讲解 Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

问题背景

根据上一篇文章完成了 Gateway+FeignClient+Nacos 的初步搭建,这篇添加 Sleuth 链路追踪
注意事项:

  • 可以根据文中的代码自己创建工程,也可以直接下载源码进行学习
  • 默认已安装JDK
  • 可以根据文章自检创建工程,也可以直接下载源码进行参考

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+FeignClient+Nacos通过网关远程调用微服务(一)

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Zipkin安装部署可视化http方式链路追踪(三)

Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Zipkin持久化mysql存储rabbitmq消息队列链路追踪(四)

项目搭建

1 sleuth链路追踪可以直接根据上一篇文章的代码,再添加一个sleuth依赖

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

整个pom如下,service公共pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yg</groupId>
    <artifactId>sleuthTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modules>
        <module>service1</module>
        <module>service2</module>
    </modules>

    <name>sleuthTest</name>
    <description>sleuthTest</description>
    <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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!--            <version>2.1.0.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!--Spring Cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

gateway服务的pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yg</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>gateway</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
<!--            <version>2.1.0.RELEASE</version>-->
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
<!--            <scope>test</scope>-->
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!--            <version>2.1.0.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2 其他东西都不用改,只需要引入依赖,现在新的版本logback都不用添加,默认会打印traceID和spanID

项目测试

1 启动nacos服务端,gateway,service1,service2
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)
2 查看注册中心http://localhost:8848/nacos,密码和账号都为:nacos
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)
3 使用网关路由调用service1微服务API,service1微服务调用service2
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)

4 查看gateway的日志,可以看到红框,gateway是微服务名,后面接着是traceID和spanID
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)
5 查看service1微服务的日志,service1微服务名,后面接着是traceID和spanID
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)
6 可以从上两张图看到service1的traceID和gateway的traceID和spanID相同,gateway是起始调用,所以traceID和spanID相同
7 如果微服务过多,这种方式查看及其不方便,所以下一章介绍zipkin

心得

  • sleuth添加非常简单,添加即用,但查看不方便,引出zipkin带有web界面可视化

作为程序员第 19 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)

Lyric: 香浓的诱惑

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/110857.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!