skywalking在使用之前需要先部署服务,使用docker部署,比较简单,镜像的下载地址
本文使用的是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 server | 192.168.21.128 |
oap ui | 192.168.21.130 |
启动之后访问http://192.168.21.130:8088/
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
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration debug="false">
-
-
-
- <!--控制台日志, 控制台输出 skywalking-->
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
- <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%n</Pattern>
- </layout>
- </encoder>
- </appender>
-
- <!-- skyWalking日志采集 -->
- <appender name="APM_LOG" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
- <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
- <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout">
- <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%thread] %-5level %logger{36} -%msg%n</Pattern>
- </layout>
- </encoder>
- </appender>
-
-
-
- <!-- 日志输出级别 -->
- <root level="DEBUG">
- <appender-ref ref="STDOUT" />
- <appender-ref ref="APM_LOG" />
- </root>
- </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,也可以通过过滤器,以下的省略的示例:
- package com.dc.smart.core.common.log.interceptor;
-
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.serializer.SerializerFeature;
- import com.dc.smart.core.common.constant.CommonConstant;
- import com.dc.smart.core.common.log.LogHelper;
- import com.dc.smart.core.common.log.LogHelperProperties;
- import com.dc.smart.core.common.log.business.enums.EntityEnum;
- import com.dc.smart.core.common.log.request.MyHttpServletRequestWrapper;
- import com.dc.smart.core.common.log.request.RequestLog;
- import com.dc.smart.core.common.log.util.IpUtils;
- import com.dc.smart.core.common.log.util.TraceUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- import org.springframework.web.servlet.HandlerInterceptor;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
-
- /**
- * @author coco
- * @date 2022/3/1
- */
- public class LogInterceptor implements HandlerInterceptor {
-
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
-
-
- //设置trace
- String traceId = request.getHeader(CommonConstant.HEADER_XTraceId);
-
-
- TraceUtils.createTraceId();
-
- return true;
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
-
- //清除
- TraceUtils.destroyTraceId();
- }
- }
2)配置
- @Configuration
- public class MvcConfig implements WebMvcConfigurer {
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
- }
- }
3)TraceUtils,注意:这里通过TraceContext.traceId();来获取traceid
- package com.dc.smart.core.common.log.util;
-
- import com.dc.smart.core.common.constant.CommonConstant;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.skywalking.apm.toolkit.trace.TraceContext;
- import org.slf4j.MDC;
-
-
- /**
- * @author coco
- * @date 2022/3/1
- */
- @Slf4j
- public class TraceUtils {
- private static final String TRACE_ID = CommonConstant.HEADER_XTraceId;
-
-
- public static void createTraceId() {
- String traceId = MDC.get(TRACE_ID);
- if (StringUtils.isBlank(traceId)) {
-
- traceId = TraceContext.traceId();
- MDC.put(TRACE_ID, traceId);
- }
- }
-
- public static void destroyTraceId() {
- MDC.remove(TRACE_ID);
- }
-
- public static String getTraceId() {
- return MDC.get(TRACE_ID);
- }
-
- public static void setTraceId(String traceId) {
- MDC.put(TRACE_ID, traceId);
- }
-
-
-
- }
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采集日志