java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > skywalking链路追踪

SkyWalking自定义链路追踪实现步骤

作者:VNone

本文详细介绍了如何使用SkyWalking进行自定义链路追踪的步骤,包括POM文件依赖和逻辑业务代码的编写,文章最后推荐了脚本之家作为进一步学习的资源

第一步: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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>vnacos</artifactId>
        <groupId>com.nacos</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>order</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <!-- ali 连接池 -->
        <druid.version>1.1.9</druid.version>
        <java.version>1.8</java.version>
        <!-- mybatis -->
        <mybatis-plus-boot-starter.version>3.0-RELEASE</mybatis-plus-boot-starter.version>
        <mybatis-spring-boot-starter.version>1.3.2</mybatis-spring-boot-starter.version>
        <mybatis.ehcache.version>1.1.0</mybatis.ehcache.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- ali 连接池依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- mybatis 依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.caches</groupId>
            <artifactId>mybatis-ehcache</artifactId>
            <version>${mybatis.ehcache.version}</version>
        </dependency>
        <!-- mysql 依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--nacos-服务注册发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
      <strong>  <!-- SkyWalking 工具类, 跟服务版本一致 -->
        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-toolkit-trace</artifactId>
            <version>8.5.0</version>
        </dependency></strong>
    </dependencies>
    <!--
    项目打包时会将java目录中的*.xml文件也进行打包
    此处必须添加项目打包,不然会报错:Invalid bound statement (not found): XXXXXXX
 -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

第二部:逻辑业务代码使用注释

package com.order.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.order.entity.Emp;
import com.order.mapper.EmpMapper;
import com.order.service.EmpService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author vn
 * @version 1.0
 * @date 2022/3/17 20:53
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Override
   <strong> @Trace
    @Tags({@Tag(key</strong><strong>="selectEmpById",value="returnedObj"),
            @Tag(key="selectEmpById",value="arg[0]")})
    </strong>public Emp selectEmpById(Integer id) {
        return empMapper.selectEmpById(id);
    }
    @Override
  <strong>  @Trace
    @Tag(key</strong><strong>="selectEmp",value="returnedObj")
    </strong>public List<Emp> selectEmp() {
        return empMapper.selectEmp();
    }
}
@Trace将方法加入追踪链路 
如果一个业务方法想在ui界面的跟踪链路上显示出来,只需要在业务方法上加上@Trace注解即可
@Tags或@Tag 我们还可以为追踪链路增加其他额外的信息,比如记录参数和返回信息。实现方式:在方法上增加@Tag或者@Tag; @Tag 注解中 key = 方法名 ; value = returnedObj 返回值 arg[0] 参数

到此这篇关于SkyWalking自定义链路追踪实现的文章就介绍到这了,更多相关skywalking链路追踪内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文