集成Sleuth实现链路追踪

文章目录

1.新增sunrays-common-cloud模块

1.在sunrays-framework下创建
2.pom.xml
xml 复制代码
<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-dependencies</artifactId>
        <version>1.0.5</version>
        <!-- 当要继承的模块不是目录中的当前模块的父模块时使用 -->
        <relativePath/>
    </parent>

    <version>1.0.5</version>

    <artifactId>sunrays-common-cloud</artifactId>

</project>
3.查看是否被sunrays-framework管理

2.创建common-cloud-sleuth-starter

1.目录结构
2.pom.xml
xml 复制代码
<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-common-cloud</artifactId>
        <version>1.0.5</version>
    </parent>

    <version>1.0.5</version>

    <artifactId>common-cloud-sleuth-starter</artifactId>

    <dependencies>
        <!-- spring-cloud-starter-sleuth -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
    </dependencies>
</project>
3.sunrays-dependencies指定cloud版本
xml 复制代码
            <spring-cloud.version>2020.0.1</spring-cloud.version>

						<!-- Spring Cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
4.SleuthAutoConfiguration.java
java 复制代码
package com.sunxiansheng.sleuth.config;

import org.springframework.context.annotation.Configuration;

/**
 * Description: Sleuth自动配置类
 *
 * @Author sun
 * @Create 2025/1/1 11:03
 * @Version 1.0
 */
@Configuration
public class SleuthAutoConfiguration {
}
5.spring.factories
java 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.sleuth.config.SleuthAutoConfiguration

3.创建common-cloud-sleuth-starter-demo

1.目录结构
2.pom.xml
xml 复制代码
<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.sunxiansheng</groupId>
        <artifactId>sunrays-demo</artifactId>
        <version>1.0.5</version>
    </parent>

    <version>1.0.5</version>

    <artifactId>common-cloud-sleuth-starter-demo</artifactId>

    <packaging>pom</packaging>
    <modules>
        <module>ServiceA</module>
        <module>ServiceB</module>
    </modules>

    <dependencies>
        <!-- 引入common-cloud-sleuth-starter -->
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-cloud-sleuth-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
        <!-- OpenFeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- 引入springboot-web模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 排除logging,防止日志冲突 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.sunxiansheng</groupId>
            <artifactId>common-log4j2-starter</artifactId>
            <version>1.0.5</version>
        </dependency>
    </dependencies>
</project>
3.ServiceB
1.application.yml 开启sleuth并指定服务名和端口
yaml 复制代码
spring:
  sleuth:
    enabled: true
    sampler:
      probability: 1.0
  application:
    name: ServiceB
server:
  port: 9092
2.ResponseToA.java 给服务A响应数据
java 复制代码
package com.sunxiansheng;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description: 给服务A响应数据
 *
 * @Author sun
 * @Create 2025/1/1 10:54
 * @Version 1.0
 */
@RestController
public class ResponseToA {

    @RequestMapping("/responseToA")
    public String responseToA() {
        return "responseToA";
    }
}
3.ServiceBApplication.java 启动类
java 复制代码
package com.sunxiansheng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: ServiceB启动类
 *
 * @Author sun
 * @Create 2025/1/1 10:50
 * @Version 1.0
 */

@SpringBootApplication
public class ServiceBApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceBApplication.class, args);
    }
}
4.ServiceA
1.application.yml 开启sleuth并指定服务名和端口
yaml 复制代码
spring:
  sleuth:
    enabled: true
    sampler:
      probability: 1.0
  application:
    name: ServiceA
server:
  port: 9091
2.ServiceBClient.java 暴露服务B的接口
java 复制代码
package com.sunxiansheng;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Description: ServiceBClient
 *
 * @Author sun
 * @Create 2025/1/1 10:43
 * @Version 1.0
 */
@FeignClient(name = "ServiceB", url = "http://localhost:9092")
public interface ServiceBClient {

    @RequestMapping("/responseToA")
    String responseToA();
}
3.RequestToB.java 向B服务发起请求
java 复制代码
package com.sunxiansheng;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Description: 向B服务发起请求
 *
 * @Author sun
 * @Create 2025/1/1 10:53
 * @Version 1.0
 */
@RestController
public class RequestToB {

    @Resource
    private ServiceBClient serviceBClient;

    @RequestMapping("/requestToB")
    public String requestToB() {
        return serviceBClient.responseToA();
    }
}
4.ServiceAApplication.java ServiceA启动类,开启Feign
java 复制代码
package com.sunxiansheng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * Description: ServiceA启动类
 *
 * @Author sun
 * @Create 2025/1/1 10:50
 * @Version 1.0
 */
@SpringBootApplication
@EnableFeignClients // 开启Feign
public class ServiceAApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceAApplication.class, args);
    }
}
5.common-log4j2-starter
1.log4j2-spring.xml中读取traceId和spanId
xml 复制代码
        <!-- 控制台日志输出格式,带颜色 -->
        <Property name="CONSOLE_LOG_PATTERN">
            <!-- %style{%d{yyyy-MM-dd HH:mm:ss.SSS}}{green} %style{[%t]}{blue} %highlight{%p}{FATAL=red blink, ERROR=red, WARN=yellow, INFO=green, DEBUG=cyan, TRACE=magenta} %style{[PFTID:%X{PFTID}]}{magenta} %style{[Module:${sys:log.module}]}{yellow} %style{%logger{36}}{cyan} - %msg%n%throwable -->
            %style{%d{yyyy-MM-dd HH:mm:ss.SSS}}{green} %style{[%t]}{blue} %highlight{%p}{FATAL=red blink, ERROR=red, WARN=yellow, INFO=green, DEBUG=cyan, TRACE=magenta} %style{[PFTID:%X{PFTID}]}{magenta} %style{[Module:${sys:log.module}]}{yellow} %style{[%X{traceId}]}{cyan} %style{[%X{spanId}]}{cyan} %style{%logger{36}}{cyan} - %msg%n%throwable

        </Property>
2.测试发送请求
1.ServiceA
2.ServiceB
3.即使是微服务之间的请求,他们的traceid也是一样的
相关推荐
踢球的打工仔5 小时前
PHP面向对象(5)
android·java·php
Rover.x5 小时前
错误:找不到或无法加载主类 @C:\Users\AppData\Local\Temp\idea_arg_file223456232
java·ide·intellij-idea
4***17275 小时前
使用 java -jar 命令启动 Spring Boot 应用时,指定特定的配置文件的几种实现方式
java·spring boot·jar
CoderYanger5 小时前
优选算法-字符串:63.二进制求和
java·开发语言·算法·leetcode·职场和发展·1024程序员节
3***31215 小时前
java进阶1——JVM
java·开发语言·jvm
charlie1145141915 小时前
深入理解C/C++的编译链接技术6——A2:动态库设计基础之ABI设计接口
c语言·开发语言·c++·学习·动态库·函数
Cx330❀5 小时前
C++ STL set 完全指南:从基础用法到实战技巧
开发语言·数据结构·c++·算法·leetcode·面试
white-persist5 小时前
【攻防世界】reverse | Reversing-x64Elf-100 详细题解 WP
c语言·开发语言·网络·python·学习·安全·php
FeiHuo565155 小时前
微信个人号开发中如何高效实现API二次开发
java·开发语言·python·微信
zmzb01036 小时前
C++课后习题训练记录Day33
开发语言·c++