• 6_sleuth-zipkin-spring_boot_admin 链路追踪



    • 如果能跟踪每个请求,中间请求经过哪些微服务,请求耗时,网络延迟,业务逻辑耗时等。我们就能更好地分析系统瓶颈、解决系统问题。

    Sleuth

    • span(跨度),基本工作单元。一次链路调用,创建一个span,span用一个64位id唯一标识。包括:id,描述,时间戳事件,spanId,span父id。span被启动和停止时,记录了时间信息,初始化span叫:root span,它的span id和trace id相等。
    • trace(跟踪),一组共享“root span”的span组成的树状结构 称为 trace,trace也有一个64位ID,trace中所有span共享一个trace id。类似于一颗 span 树。
    • annotation(标签),annotation用来记录事件的存在,其中,核心annotation用来定义请求的开始和结束。
    • 开启sleuth: 只需要加依赖项就可以;
      • 加依赖:需要监控的系统都加上:
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-sleuthartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 日志级别开启 debug 级别
    logging:
      level:
        root: DEBUG
    
    
    • 1
    • 2
    • 3
    • 4
    • consumer 调用 provider 查看日志:
    --- 调用方 ---
    [consumer,a4bf19759a9260f7,a4bf19759a9260f7,true] 
    [服务名称,traceId(一条请求调用链中 唯一ID),spanID(基本的工作单元,获取数据等),是否向zipkin上报此信息]
    
    --- 被调用方 ---
    [provider,a4bf19759a9260f7,f0f55fb160be7380,true] 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 可以看到:traceId 是一样的,就可以定位一条调用链路;

    zipkin

    • 下载zipkin:

    • 启动:java -jar zipkin-server-2.23.18-exec.jar --server.port=9412

    • 开启 zipkin:

    • 依赖,consumer 、provider 都要加;

      <dependency>
          <groupId>org.springframework.cloudgroupId>
          <artifactId>spring-cloud-starter-zipkinartifactId>
      dependency>
      
      • 1
      • 2
      • 3
      • 4
    • 配置文件:consumer 、provider 都要加;

      spring:
        zipkin:
          base-url: http://localhost:9412/
          #采样比例1
        sleuth:
          sampler:
            rate: 1  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • http://localhost:9412/

    SpringBootAdmin

    • 也需要启动一个独立的服务,其他的服务把 actuator 收集的信息上边给 admin 服务;
    • 依赖:
    <dependency>
        <groupId>de.codecentricgroupId>
        <artifactId>spring-boot-admin-starter-serverartifactId>
        <version>2.2.1version>
    dependency>
    
    <dependency>
        <groupId>de.codecentricgroupId>
        <artifactId>spring-boot-admin-server-uiartifactId>
        <version>2.2.1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 完整 pom:
    
    <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.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.2.6.RELEASEversion>
            <relativePath/> 
        parent>
    
        <groupId>com.go.cngroupId>
        <artifactId>admin-serverartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>admin-servername>
    
        <properties>
            <java.version>1.8java.version>
            <spring-cloud.version>Hoxton.SR3spring-cloud.version>
            <spring-boot.version>2.2.6.RELEASEspring-boot.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>de.codecentricgroupId>
                <artifactId>spring-boot-admin-starter-serverartifactId>
                <version>2.2.1version>
            dependency>
            
            <dependency>
                <groupId>de.codecentricgroupId>
                <artifactId>spring-boot-admin-server-uiartifactId>
                <version>2.2.1version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>${spring-cloud.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 注解:@EnableAdminServer
    • 向 admin 上报信息的服务也需要加入 client 依赖:
    
    <dependency>
        <groupId>de.codecentricgroupId>
        <artifactId>spring-boot-admin-starter-clientartifactId>
        <version>2.2.1version>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • admin-client 端配置调整:
    management:
      endpoint:
        health:
          show-details: always
      endpoints:
        web:
          exposure:
            include: '*'
    spring:
      boot:
        admin:
          client:
            # admin-server 的地址
            url: http://localhost:8080
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 应用墙:http://localhost:8080/wallboard

    邮件通知

    • pom 依赖:
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-mailartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 配置文件调整:
    spring: 
      application: 
        name: cloud-admin
      security:
        user:
          name: root
          password: root
      # 邮件设置
      mail:
        host: smtp.qq.com
        username: username
        password: xxxxxxx # 授权码
        properties:
          mail: 
            smpt: 
              auth: true
              starttls: 
                enable: true
                required: true
    #收件邮箱
    spring.boot.admin.notify.mail.to: xxx@qq.com   
    # 发件邮箱
    spring.boot.admin.notify.mail.from: xxx@qq.com 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    钉钉通知

    ---启动类---
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    
    import de.codecentric.boot.admin.server.config.EnableAdminServer;
    import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
    
    @SpringBootApplication
    @EnableAdminServer
    public class AdminApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(AdminApplication.class, args);
    	}
    	   @Bean
    	    public DingDingNotifier dingDingNotifier(InstanceRepository repository) {
    	        return new DingDingNotifier(repository);
    	    }
    }
    --- 通知类 ---
    import java.util.Map;
    
    import com.alibaba.fastjson.JSONObject;
    
    import de.codecentric.boot.admin.server.domain.entities.Instance;
    import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
    import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
    import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
    import reactor.core.publisher.Mono;
    
    public class DingDingNotifier extends AbstractStatusChangeNotifier  {
    	public DingDingNotifier(InstanceRepository repository) {
            super(repository);
        }
        @Override
        protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
            String serviceName = instance.getRegistration().getName();
            String serviceUrl = instance.getRegistration().getServiceUrl();
            String status = instance.getStatusInfo().getStatus();
            Map<String, Object> details = instance.getStatusInfo().getDetails();
            StringBuilder str = new StringBuilder();
            str.append("服务预警 : [" + serviceName + "]");
            str.append("[服务地址]" + serviceUrl);
            str.append("[状态]" + status);
            str.append("[详情]" + JSONObject.toJSONString(details));
            return Mono.fromRunnable(() -> {
                DingDingMessageUtil.sendTextMessage(str.toString());
            });
        }
    }
    --- 发送工具类 ---
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import com.alibaba.fastjson.JSONObject;
    
    public class DingDingMessageUtil {
    	public static String access_token = "Token";
        public static void sendTextMessage(String msg) {
            try {
                Message message = new Message();
                message.setMsgtype("text");
                message.setText(new MessageInfo(msg));
                URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token);
                // 建立 http 连接
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Charset", "UTF-8");
                conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
                conn.connect();
                OutputStream out = conn.getOutputStream();
                String textMessage = JSONObject.toJSONString(message);
                byte[] data = textMessage.getBytes();
                out.write(data);
                out.flush();
                out.close();
                InputStream in = conn.getInputStream();
                byte[] data1 = new byte[in.available()];
                in.read(data1);
                System.out.println(new String(data1));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    --- 消息类 ---
    public class Message {
    	private String msgtype;
        private MessageInfo text;
        public String getMsgtype() {
            return msgtype;
        }
        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }
        public MessageInfo getText() {
            return text;
        }
        public void setText(MessageInfo text) {
            this.text = text;
        }
    }
    
    public class MessageInfo {
        private String content;
        public MessageInfo(String content) {
            this.content = content;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
  • 相关阅读:
    屏幕录像推荐:Apeaksoft Screen Recorder 中文 for mac
    MySQL - InnoDB记录结构
    Kubernetes Pod调度:从基础到高级实战技巧
    切换至root用户时,命令提示符颜色为白色,如何修改?
    双硬盘WIN+UBUNTU 系统, 重装UNBUNTU20.14系统后无法启动, 进入进入grub rescue
    【webpack】wabpack5 知识梳理
    参加2020年全国大学生机器人大赛ROBOCON的一点心得
    Gin使用及源码简析
    【sfu】回调接收测拼装的H264 帧
    自学网络安全(黑客)
  • 原文地址:https://blog.csdn.net/wwq921220/article/details/127146226