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

问题背景

根据上一篇文章完成了 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



2 查看注册中心http://localhost:8848/nacos,密码和账号都为:nacos

3 使用网关路由调用service1微服务API,service1微服务调用service2

4 查看gateway的日志,可以看到红框,gateway是微服务名,后面接着是traceID和spanID

5 查看service1微服务的日志,service1微服务名,后面接着是traceID和spanID

6 可以从上两张图看到service1的traceID和gateway的traceID和spanID相同,gateway是起始调用,所以traceID和spanID相同
7 如果微服务过多,这种方式查看及其不方便,所以下一章介绍zipkin

心得

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




作为程序员第 19 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 香浓的诱惑

使用Spring Cloud Alibaba进行链路追踪,需要使用阿里巴巴开源的中间件——Nacos和Sentinel。 具体步骤如下: 1. 引入依赖 在pom.xml文件中,引入以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> </dependency> ``` 2. 配置Nacos 在application.yml文件中,添加以下配置: ```yaml spring: cloud: nacos: discovery: server-addr: ${NACOS_SERVER_ADDR:localhost:8848} sentinel: transport: dashboard: localhost:8080 port: 8719 ``` 3. 配置Sentinel 在application.yml文件中,添加以下配置: ```yaml spring: cloud: sentinel: transport: dashboard: localhost:8080 port: 8719 ``` 4. 配置链路追踪 在启动类中添加@EnableZipkinServer注解,激活Zipkin Server: ```java @SpringBootApplication @EnableZipkinServer public class ZipkinServerApplication { public static void main(String[] args) { SpringApplication.run(ZipkinServerApplication.class, args); } } ``` 5. 配置Sleuth 在服务中,需要使用Spring Cloud Sleuth进行链路追踪。在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> ``` 然后在application.yml文件中配置: ```yaml spring: sleuth: sampler: probability: 1.0 ``` 以上就是使用Spring Cloud Alibaba进行链路追踪的步骤。总的来说,需要配置Nacos、Sentinel、Zipkin Server和Sleuth,以保证链路追踪的正常运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值