• springboot+skywalking初体检


    一、部署skywalking环境

    skywalking在使用之前需要先部署服务,使用docker部署,比较简单,镜像的下载地址

    skywalking镜像

    本文使用的是docker部署的SkyWalking OAP Server + SkyWalking UI Image,agent 是部署在本地的Java Agent Downloads | Apache SkyWalking

    oap server 

    docker run --name oap --restart always -d -p 1234:1234 -p 11800:11800 -p 12800:12800 -e TZ=Asia/Shanghai apache/skywalking-oap-server:8.9.1

    oap ui

     docker run --name oap-ui --restart always -d -p 8088:8080 -e TZ=Asia/Shanghai -e SW_OAP_ADDRESS=http://192.168.21.128:12800 apache/skywalking-ui

    表1:

    服务ip
    oap server192.168.21.128
    oap ui192.168.21.130

    启动之后访问http://192.168.21.130:8088/

    二、在springboot中使用skywalking

    1.pom中加入依赖

    
    
        org.apache.skywalking
        apm-toolkit-logback-1.x
        8.12.0
    
    
    
    
    
        org.apache.skywalking
        apm-toolkit-trace
        8.12.0
    
    

    2. 本文使用的是logback做完日志框架,logback-config.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <configuration debug="false">
    3. <!--控制台日志, 控制台输出 skywalking-->
    4. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    5. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    6. <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
    7. <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%n</Pattern>
    8. </layout>
    9. </encoder>
    10. </appender>
    11. <!-- skyWalking日志采集 -->
    12. <appender name="APM_LOG" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
    13. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    14. <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout">
    15. <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%thread] %-5level %logger{36} -%msg%n</Pattern>
    16. </layout>
    17. </encoder>
    18. </appender>
    19. <!-- 日志输出级别 -->
    20. <root level="DEBUG">
    21. <appender-ref ref="STDOUT" />
    22. <appender-ref ref="APM_LOG" />
    23. </root>
    24. </configuration>

     注意:pattern和layout是要对应的,否则无法从mdc中注入tid。(追踪id)

    Pattern
    layout
    [%tid]
    org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout
    [%X{tid}]
    org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout

     3.结合mdc

    1)通过拦截器使用mdc,也可以通过过滤器,以下的省略的示例:

    1. package com.dc.smart.core.common.log.interceptor;
    2. import com.alibaba.fastjson.JSON;
    3. import com.alibaba.fastjson.serializer.SerializerFeature;
    4. import com.dc.smart.core.common.constant.CommonConstant;
    5. import com.dc.smart.core.common.log.LogHelper;
    6. import com.dc.smart.core.common.log.LogHelperProperties;
    7. import com.dc.smart.core.common.log.business.enums.EntityEnum;
    8. import com.dc.smart.core.common.log.request.MyHttpServletRequestWrapper;
    9. import com.dc.smart.core.common.log.request.RequestLog;
    10. import com.dc.smart.core.common.log.util.IpUtils;
    11. import com.dc.smart.core.common.log.util.TraceUtils;
    12. import org.apache.commons.lang3.StringUtils;
    13. import org.springframework.web.context.support.WebApplicationContextUtils;
    14. import org.springframework.web.servlet.HandlerInterceptor;
    15. import javax.servlet.http.HttpServletRequest;
    16. import javax.servlet.http.HttpServletResponse;
    17. import java.time.LocalDateTime;
    18. import java.time.format.DateTimeFormatter;
    19. /**
    20. * @author coco
    21. * @date 2022/3/1
    22. */
    23. public class LogInterceptor implements HandlerInterceptor {
    24. @Override
    25. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    26. //设置trace
    27. String traceId = request.getHeader(CommonConstant.HEADER_XTraceId);
    28. TraceUtils.createTraceId();
    29. return true;
    30. }
    31. @Override
    32. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    33. //清除
    34. TraceUtils.destroyTraceId();
    35. }
    36. }

    2)配置

    1. @Configuration
    2. public class MvcConfig implements WebMvcConfigurer {
    3. @Override
    4. public void addInterceptors(InterceptorRegistry registry) {
    5. registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
    6. }
    7. }

    3)TraceUtils,注意:这里通过TraceContext.traceId();来获取traceid

    1. package com.dc.smart.core.common.log.util;
    2. import com.dc.smart.core.common.constant.CommonConstant;
    3. import lombok.extern.slf4j.Slf4j;
    4. import org.apache.commons.lang3.StringUtils;
    5. import org.apache.skywalking.apm.toolkit.trace.TraceContext;
    6. import org.slf4j.MDC;
    7. /**
    8. * @author coco
    9. * @date 2022/3/1
    10. */
    11. @Slf4j
    12. public class TraceUtils {
    13. private static final String TRACE_ID = CommonConstant.HEADER_XTraceId;
    14. public static void createTraceId() {
    15. String traceId = MDC.get(TRACE_ID);
    16. if (StringUtils.isBlank(traceId)) {
    17. traceId = TraceContext.traceId();
    18. MDC.put(TRACE_ID, traceId);
    19. }
    20. }
    21. public static void destroyTraceId() {
    22. MDC.remove(TRACE_ID);
    23. }
    24. public static String getTraceId() {
    25. return MDC.get(TRACE_ID);
    26. }
    27. public static void setTraceId(String traceId) {
    28. MDC.put(TRACE_ID, traceId);
    29. }
    30. }

    三、启动

    java 
    -javaagent:D:\\java\\skywalking-agent\\skywalking-agent.jar
    -Dskywalking.agent.service_name=comp-local
    -Dskywalking.collector.backend_service=192.168.21.128:11800
    -jar xxxx.jar

    javaagent:  agent位置

    service_name:显示的服务名

    backend_service:sk server

    使用IDEA的话,直接配置在vm options中就可以

    这样就可以了,同时可以支持skywalking采集日志

  • 相关阅读:
    Unity接入北斗探针SDK(基于UnityPlayerActivity)丨三、新建AS工程,并导入北斗SDK
    [golang]在Gin框架中使用JWT鉴权
    高等工程数学 —— 第五章 (4)罚函数法
    【电源专题】接地的类型
    【接口测试】POST请求提交数据的三种方式及Postman实现
    记录每天学习的新知识:Composing builds
    怎样用一周时间研究 ChatGPT
    JAVA毕设项目社交的健身网课平台服务器端(java+VUE+Mybatis+Maven+Mysql)
    22.11.8打卡 Codeforces Round #831 (Div. 1 + Div. 2) A~C
    【概率论基础进阶】随机事件和概率-古典概型与伯努利概型
  • 原文地址:https://blog.csdn.net/Coco_chun/article/details/127408002