From 6dde9095a43bb22a465c83f12ae4e00e2f01498c Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sat, 6 Mar 2021 01:53:06 +0800 Subject: [PATCH 01/28] =?UTF-8?q?=E4=BD=BF=E5=BE=97jdbc=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E4=B8=B2=E6=94=AF=E6=8C=81mysql=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-local.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index 5019cd18d..2e1c21365 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -7,7 +7,7 @@ spring: # 数据源配置项 TODO 多数据源;TODO 监控配置 datasource: name: ruoyi-vue-pro - url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT + url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT&allowPublicKeyRetrieval=true driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 From 0997e456e7f79bfdac74bd4678638226f2b35af4 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sat, 6 Mar 2021 01:59:35 +0800 Subject: [PATCH 02/28] =?UTF-8?q?=E9=9B=86=E6=88=90skywalking=E6=89=93?= =?UTF-8?q?=E5=8D=B0traceId=E5=BF=85=E9=A1=BB=E7=9A=84=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4fe238cb9..8b327b6fe 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ - 4.0.0 + 4.0.0 cn.iocoder dashboard @@ -37,6 +37,7 @@ 1.7.0 8.3.0 + 6.1 2.3.1 1.16.14 @@ -158,6 +159,18 @@ apm-toolkit-trace ${skywalking.version} + + org.apache.skywalking + apm-toolkit-logback-1.x + ${skywalking.version} + + + + net.logstash.logback + logstash-logback-encoder + ${logback.encoder.version} + provided + de.codecentric From 11cf3853c7a0422e5f3a65c77136005d5c2d16ed Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sat, 6 Mar 2021 02:03:53 +0800 Subject: [PATCH 03/28] =?UTF-8?q?=E5=AE=9A=E4=B9=89logback=E7=9A=84Layout?= =?UTF-8?q?=E4=BB=A5=E5=8F=8AConverter,=E7=94=A8=E4=BA=8E=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tracer/core/util/TracerUtils.java | 4 +- .../LocalLogbackPatternConverter.java | 42 ++++++++++++++ .../TraceIdPatternLogbackLayout.java | 36 ++++++++++++ src/main/resources/logback-spring.xml | 55 +++++++++++++++++++ 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java create mode 100644 src/main/resources/logback-spring.xml diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index 1a6b29cb1..777c273cb 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -30,7 +30,9 @@ public class TracerUtils { } } catch (Throwable ignore) {} // TODO 芋艿 多次调用会问题 - return UUID.randomUUID().toString(); + + // TODO 麻薯 定义一个给外部扩展的接口,默认在未接入Skywalking时,输出UUID + return "UUID:" + UUID.randomUUID().toString(); } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java new file mode 100644 index 000000000..3b69098d2 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +package cn.iocoder.dashboard.framework.tracer.skywalking; + +import ch.qos.logback.classic.pattern.ClassicConverter; +import ch.qos.logback.classic.spi.ILoggingEvent; +import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; + +/** + * Created by mashu on 2021/3/6. + */ +public class LocalLogbackPatternConverter extends ClassicConverter { + /** + * 作为默认的方式, 从{@link TracerUtils}中获取traceId, + * 需要用这个去替代logback的Layout, + * 同时避免了sky-walking agent生效的情况下仍然输出定值的问题. + * + * @param iLoggingEvent the event + * @return the traceId: UUID, or the real traceId. + */ + @Override + public String convert(ILoggingEvent iLoggingEvent) { + return TracerUtils.getTraceId(); + } +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java new file mode 100644 index 000000000..6891c782b --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +package cn.iocoder.dashboard.framework.tracer.skywalking; + +import ch.qos.logback.classic.PatternLayout; + +/** + * Based on the logback-compoenent convert register mechanism, + * register {@link LocalLogbackPatternConverter} as a new convert, match to "tid". + * You can use "%tid" in logback config file, "Pattern" section. + * If sky-walking agent is not active mode, it will use UUID as tid. + *

+ * Created by mashu on 2021/3/6. + */ +public class TraceIdPatternLogbackLayout extends PatternLayout { + static { + defaultConverterMap.put("tid", LocalLogbackPatternConverter.class.getName()); + } +} diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 000000000..840addd7e --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,55 @@ +    +       + + + %d{ISO8601} | %tid | %thread | %-5level | %msg%n + + + + + + ./logs/ruoyi-vue-pro-%d{yyyy-MM-dd_HH}.log + 3 + + + + %d{ISO8601} | %tid | %thread | %-5level | %msg%n + + + + 10MB + + + + 0 + 10 + + + + + + + + + + + + + + + + + + + + + +        +     +   + + + + + + \ No newline at end of file From 3018866c62ad9231a65c810ab69e5c14e5d0a67a Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sat, 6 Mar 2021 18:27:21 +0800 Subject: [PATCH 04/28] =?UTF-8?q?=E4=B8=BA=E4=BA=8C=E6=AC=A1=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E9=A2=84=E7=95=99=E6=8E=A5=E5=85=A5=E4=BA=A7=E7=94=9F?= =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E6=B5=81=E6=B0=B4=E5=8F=B7=E7=9A=84?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/tracer/core/ITrace.java | 16 ++++ .../tracer/core/util/TracerUtils.java | 46 +++++++++- .../dashboard/util/bean/SpringUtil.java | 83 +++++++++++++++++++ 3 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java create mode 100644 src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java new file mode 100644 index 000000000..483b408db --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java @@ -0,0 +1,16 @@ +package cn.iocoder.dashboard.framework.tracer.core; + +/** + * 用于扩展获取traceId的场景,需要装载到Spring bean容器中. + * + * @author 麻薯 + */ +public interface ITrace { + + /** + * 用于接入三方traceId + * + * @return traceId + */ + String getTraceId(); +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index 777c273cb..a9f85be41 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -1,6 +1,8 @@ package cn.iocoder.dashboard.framework.tracer.core.util; import cn.hutool.core.util.StrUtil; +import cn.iocoder.dashboard.framework.tracer.core.ITrace; +import cn.iocoder.dashboard.util.bean.SpringUtil; import org.apache.skywalking.apm.toolkit.trace.TraceContext; import java.util.UUID; @@ -12,26 +14,62 @@ import java.util.UUID; */ public class TracerUtils { + /** + * 私有化构造方法 + */ + private TracerUtils() { + } + /** * 获得链路追踪编号 - * + *

* 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。 - * + *

* 默认情况下,我们使用 Apache SkyWalking 的 traceId 作为链路追踪编号。当然,可能会存在并未引入 Skywalking 的情况,此时使用 UUID 。 * * @return 链路追踪编号 */ public static String getTraceId() { + // 通过自定义扩展的tracer产生traceId, 在Spring容器加载完成前会获取不到对应的Bean + ITrace tracer = null; + try { + tracer = getTracer(); + } catch (Throwable ignore) { + } + if (null != tracer) { + try { + return tracer.getTraceId(); + } catch (Throwable ignored) { + } + } // 通过 SkyWalking 获取链路编号 try { String traceId = TraceContext.traceId(); if (StrUtil.isNotBlank(traceId)) { return traceId; } - } catch (Throwable ignore) {} + } catch (Throwable ignore) { + } // TODO 芋艿 多次调用会问题 - // TODO 麻薯 定义一个给外部扩展的接口,默认在未接入Skywalking时,输出UUID + return defaultTraceId(); + } + + /** + * 从Spring 容器中获取 ITrace 类,返回可以为null + * + * @return ITrace + */ + private static ITrace getTracer() { + return SpringUtil.getBean(ITrace.class); + } + + /** + * 默认生成TraceId规则为UUID + * + * @return UUID + */ + private static String defaultTraceId() { return "UUID:" + UUID.randomUUID().toString(); } diff --git a/src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java b/src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java new file mode 100644 index 000000000..4cbfb4e36 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java @@ -0,0 +1,83 @@ +package cn.iocoder.dashboard.util.bean; + +import org.springframework.beans.BeansException; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; + +import org.springframework.context.ApplicationContext; + +import org.springframework.context.ApplicationContextAware; + +import org.springframework.stereotype.Component; + + +@Component +public class SpringUtil implements ApplicationContextAware { + + /** + * Spring context + */ + private static ApplicationContext applicationContext = null; + + public void setApplicationContext(ApplicationContext context) throws BeansException { + if (applicationContext == null) { + applicationContext = context; + } + } + + public static ApplicationContext getApplicationContext() { + return applicationContext; + } + + public static void setAppCtx(ApplicationContext webAppCtx) { + if (webAppCtx != null) { + applicationContext = webAppCtx; + } + } + + + /** + * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象 + */ + public static T getBean(Class clazz) { + return getApplicationContext() == null ? null : getApplicationContext().getBean(clazz); + } + + + public static T getBean(String name, Class clazz) throws ClassNotFoundException { + return getApplicationContext() == null ? null : getApplicationContext() .getBean(name, clazz); + } + + + public static final Object getBean(String beanName) { + return getApplicationContext() == null ? null : getApplicationContext() .getBean(beanName); + } + + + public static final Object getBean(String beanName, String className) throws ClassNotFoundException { + Class clz = Class.forName(className); + return getApplicationContext() == null ? null : getApplicationContext() .getBean(beanName, clz.getClass()); + + } + + + public static boolean containsBean(String name) { + return getApplicationContext() == null ? null : getApplicationContext() .containsBean(name); + } + + + public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { + return getApplicationContext() == null ? null : getApplicationContext() .isSingleton(name); + } + + + public static Class getType(String name) throws NoSuchBeanDefinitionException { + return getApplicationContext() == null ? null : getApplicationContext() .getType(name); + } + + + public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { + return getApplicationContext() == null ? null : getApplicationContext() .getAliases(name); + } + +} \ No newline at end of file From 4cc45d73fb828fa29da54baabf309f3eff6b2947 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sat, 6 Mar 2021 22:29:11 +0800 Subject: [PATCH 05/28] =?UTF-8?q?=E5=B0=86=20bizType=20=E5=92=8C=20bizId?= =?UTF-8?q?=20=E8=BF=9B=E8=A1=8C=E5=B0=81=E8=A3=85=EF=BC=8C=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=20SkyWalking=20Tag=20=E6=A3=80=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/annotation/BizTracing.java | 22 ++++++++++ .../common/annotation/BizTracingAop.java | 27 +++++++++++++ .../tracer/core/util/TracerUtils.java | 4 +- .../dashboard/util/sping/SpElUtil.java | 40 +++++++++++++++++++ .../SpringBeanUtil.java} | 4 +- 5 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java create mode 100644 src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java create mode 100644 src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java rename src/main/java/cn/iocoder/dashboard/util/{bean/SpringUtil.java => sping/SpringBeanUtil.java} (95%) diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java new file mode 100644 index 000000000..0ca4b4d8e --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java @@ -0,0 +1,22 @@ +package cn.iocoder.dashboard.common.annotation; + +import java.lang.annotation.*; + +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Inherited + +/** + * 打印业务流水号/业务类型注解 + * + * @author 麻薯 + */ +public @interface BizTracing { + String BIZ_ID = "bizId"; + String BIZ_TYPE = "bizType"; + + String bizId() default "NULL_ID"; + + String bizType() default "NULL_TYPE"; + +} diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java new file mode 100644 index 000000000..dd9093988 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java @@ -0,0 +1,27 @@ +package cn.iocoder.dashboard.common.annotation; + +import cn.iocoder.dashboard.util.sping.SpElUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.skywalking.apm.toolkit.trace.ActiveSpan; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.ResponseBody; + +@Aspect +@Slf4j +@Configuration +public class BizTracingAop { + + @Around(value = "@annotation(bizTracing)") + public void tagBizInfo( ProceedingJoinPoint joinPoint, BizTracing bizTracing) { + String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); + String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); + log.info("accept biz: bizId[{}], bizType[{}].", bizId, bizType); + ActiveSpan.tag(BizTracing.BIZ_ID, bizId); + ActiveSpan.tag(BizTracing.BIZ_TYPE, bizType); + } +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index a9f85be41..dfcb349a3 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -2,7 +2,7 @@ package cn.iocoder.dashboard.framework.tracer.core.util; import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.framework.tracer.core.ITrace; -import cn.iocoder.dashboard.util.bean.SpringUtil; +import cn.iocoder.dashboard.util.sping.SpringBeanUtil; import org.apache.skywalking.apm.toolkit.trace.TraceContext; import java.util.UUID; @@ -61,7 +61,7 @@ public class TracerUtils { * @return ITrace */ private static ITrace getTracer() { - return SpringUtil.getBean(ITrace.class); + return SpringBeanUtil.getBean(ITrace.class); } /** diff --git a/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java b/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java new file mode 100644 index 000000000..1291d326d --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java @@ -0,0 +1,40 @@ +package cn.iocoder.dashboard.util.sping; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.core.DefaultParameterNameDiscoverer; +import org.springframework.expression.EvaluationContext; +import org.springframework.expression.Expression; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.expression.spel.support.StandardEvaluationContext; + +import java.lang.reflect.Method; + +public class SpElUtil { + + private SpElUtil() { + } + + public static String analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { + SpelExpressionParser parser = new SpelExpressionParser(); + DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); + // 通过joinPoint获取被注解方法 + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); + Method method = methodSignature.getMethod(); + // 使用spring的DefaultParameterNameDiscoverer获取方法形参名数组 + String[] paramNames = nameDiscoverer.getParameterNames(method); + // 解析过后的Spring表达式对象 + Expression expression = parser.parseExpression(spElString); + // spring的表达式上下文对象 + EvaluationContext context = new StandardEvaluationContext(); + // 通过joinPoint获取被注解方法的形参 + Object[] args = joinPoint.getArgs(); + // 给上下文赋值 + for (int i = 0; i < args.length; i++) { + context.setVariable(paramNames[i], args[i]); + } + Object value = expression.getValue(context); + return value == null ? "null" : value.toString(); + + } +} diff --git a/src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java b/src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java similarity index 95% rename from src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java rename to src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java index 4cbfb4e36..c2fd5c487 100644 --- a/src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java +++ b/src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.util.bean; +package cn.iocoder.dashboard.util.sping; import org.springframework.beans.BeansException; @@ -12,7 +12,7 @@ import org.springframework.stereotype.Component; @Component -public class SpringUtil implements ApplicationContextAware { +public class SpringBeanUtil implements ApplicationContextAware { /** * Spring context From 5f45f73caa1763ee4474df45df243c8b7fabf808 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sat, 6 Mar 2021 22:55:28 +0800 Subject: [PATCH 06/28] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E6=95=88?= =?UTF-8?q?=E5=BC=95=E7=94=A8,=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/common/annotation/BizTracingAop.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java index dd9093988..9486d4d19 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java @@ -6,18 +6,20 @@ import org.apache.skywalking.apm.toolkit.trace.ActiveSpan; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; import org.springframework.context.annotation.Configuration; -import org.springframework.web.bind.annotation.ResponseBody; +/** + * 业务链路AOP切面 + * + * @author mashu + */ @Aspect @Slf4j @Configuration public class BizTracingAop { @Around(value = "@annotation(bizTracing)") - public void tagBizInfo( ProceedingJoinPoint joinPoint, BizTracing bizTracing) { + public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); log.info("accept biz: bizId[{}], bizType[{}].", bizId, bizType); From 395f5234b165cef28305071eeb6b7fc98c65c522 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Tue, 9 Mar 2021 20:19:30 +0800 Subject: [PATCH 07/28] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=97=A0=E6=95=88?= =?UTF-8?q?=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pom.xml b/pom.xml index 8b327b6fe..9aab6a370 100644 --- a/pom.xml +++ b/pom.xml @@ -164,13 +164,6 @@ apm-toolkit-logback-1.x ${skywalking.version} - - - net.logstash.logback - logstash-logback-encoder - ${logback.encoder.version} - provided - de.codecentric From f9adeddad0c27e899271b49ca3878171e5aa782a Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Tue, 9 Mar 2021 20:20:38 +0800 Subject: [PATCH 08/28] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E6=94=B9/?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/iocoder/dashboard/common/annotation/BizTracing.java | 1 + .../tracer/skywalking/TraceIdPatternLogbackLayout.java | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java index 0ca4b4d8e..3b67bf70b 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java @@ -12,6 +12,7 @@ import java.lang.annotation.*; * @author 麻薯 */ public @interface BizTracing { + String BIZ_ID = "bizId"; String BIZ_TYPE = "bizType"; diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java index 6891c782b..2f13ce517 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java @@ -22,11 +22,12 @@ package cn.iocoder.dashboard.framework.tracer.skywalking; import ch.qos.logback.classic.PatternLayout; /** - * Based on the logback-compoenent convert register mechanism, + * Based on the logback-component convert register mechanism, * register {@link LocalLogbackPatternConverter} as a new convert, match to "tid". * You can use "%tid" in logback config file, "Pattern" section. * If sky-walking agent is not active mode, it will use UUID as tid. *

+ * logback 的转换组件,为tid 添加占位符的转换器 * Created by mashu on 2021/3/6. */ public class TraceIdPatternLogbackLayout extends PatternLayout { From cc156f6ea3fea92307cc9299df9512d705bd2f8c Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Tue, 9 Mar 2021 20:23:37 +0800 Subject: [PATCH 09/28] =?UTF-8?q?bean=E5=B7=A5=E5=85=B7=E7=B1=BB=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E4=B8=BAhutool=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tracer/core/util/TracerUtils.java | 4 +- .../dashboard/util/sping/SpringBeanUtil.java | 83 ------------------- 2 files changed, 2 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index dfcb349a3..033216260 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -1,8 +1,8 @@ package cn.iocoder.dashboard.framework.tracer.core.util; import cn.hutool.core.util.StrUtil; +import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.dashboard.framework.tracer.core.ITrace; -import cn.iocoder.dashboard.util.sping.SpringBeanUtil; import org.apache.skywalking.apm.toolkit.trace.TraceContext; import java.util.UUID; @@ -61,7 +61,7 @@ public class TracerUtils { * @return ITrace */ private static ITrace getTracer() { - return SpringBeanUtil.getBean(ITrace.class); + return SpringUtil.getBean(ITrace.class); } /** diff --git a/src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java b/src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java deleted file mode 100644 index c2fd5c487..000000000 --- a/src/main/java/cn/iocoder/dashboard/util/sping/SpringBeanUtil.java +++ /dev/null @@ -1,83 +0,0 @@ -package cn.iocoder.dashboard.util.sping; - -import org.springframework.beans.BeansException; - -import org.springframework.beans.factory.NoSuchBeanDefinitionException; - -import org.springframework.context.ApplicationContext; - -import org.springframework.context.ApplicationContextAware; - -import org.springframework.stereotype.Component; - - -@Component -public class SpringBeanUtil implements ApplicationContextAware { - - /** - * Spring context - */ - private static ApplicationContext applicationContext = null; - - public void setApplicationContext(ApplicationContext context) throws BeansException { - if (applicationContext == null) { - applicationContext = context; - } - } - - public static ApplicationContext getApplicationContext() { - return applicationContext; - } - - public static void setAppCtx(ApplicationContext webAppCtx) { - if (webAppCtx != null) { - applicationContext = webAppCtx; - } - } - - - /** - * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象 - */ - public static T getBean(Class clazz) { - return getApplicationContext() == null ? null : getApplicationContext().getBean(clazz); - } - - - public static T getBean(String name, Class clazz) throws ClassNotFoundException { - return getApplicationContext() == null ? null : getApplicationContext() .getBean(name, clazz); - } - - - public static final Object getBean(String beanName) { - return getApplicationContext() == null ? null : getApplicationContext() .getBean(beanName); - } - - - public static final Object getBean(String beanName, String className) throws ClassNotFoundException { - Class clz = Class.forName(className); - return getApplicationContext() == null ? null : getApplicationContext() .getBean(beanName, clz.getClass()); - - } - - - public static boolean containsBean(String name) { - return getApplicationContext() == null ? null : getApplicationContext() .containsBean(name); - } - - - public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { - return getApplicationContext() == null ? null : getApplicationContext() .isSingleton(name); - } - - - public static Class getType(String name) throws NoSuchBeanDefinitionException { - return getApplicationContext() == null ? null : getApplicationContext() .getType(name); - } - - - public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { - return getApplicationContext() == null ? null : getApplicationContext() .getAliases(name); - } - -} \ No newline at end of file From f0a633373ece16ec66230886d67627b8a5cc047f Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 20:57:23 +0800 Subject: [PATCH 10/28] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=8E=B7=E5=8F=96trace?= =?UTF-8?q?Id=E7=9A=84=E4=BC=98=E5=85=88=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tracer/core/util/TracerUtils.java | 65 +++++++++++++++--- .../tracer/filter/SpringMvcTraceFilter.java | 68 +++++++++++++++++++ .../framework/tracer/filter/WebConfig.java | 23 +++++++ 3 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index 033216260..a7cd841da 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -5,7 +5,9 @@ import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.dashboard.framework.tracer.core.ITrace; import org.apache.skywalking.apm.toolkit.trace.TraceContext; +import java.util.Map; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; /** * 链路追踪工具类 @@ -14,6 +16,36 @@ import java.util.UUID; */ public class TracerUtils { + /** + * 维护请求线程对应的TraceId + */ + private final static Map threadTraceIdMap = new ConcurrentHashMap<>(); + + /** + * 保存链路流水号 + * + * @param traceId 链路流水号 + */ + public static void saveThreadTraceId(String traceId) { + threadTraceIdMap.put(Thread.currentThread(), traceId); + } + + /** + * 根据线程获取链路流水号 + * + * @return 链路流水号 + */ + public static String getThreadTraceId() { + return threadTraceIdMap.get(Thread.currentThread()); + } + + /** + * 根据线程删除链路流水 + */ + public static void deleteThreadTraceId() { + threadTraceIdMap.remove(Thread.currentThread()); + } + /** * 私有化构造方法 */ @@ -26,10 +58,27 @@ public class TracerUtils { * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。 *

* 默认情况下,我们使用 Apache SkyWalking 的 traceId 作为链路追踪编号。当然,可能会存在并未引入 Skywalking 的情况,此时使用 UUID 。 + * 方法获取顺序: skywalking - > map -> 扩展接口 -> default + * 项目整体获取traceId的优先级 skywalking > 自定义实现接口 > 默认 + * map中的traceId 当且仅当其他情况为产生时才会出现. * * @return 链路追踪编号 */ public static String getTraceId() { + String traceId; + // 通过 SkyWalking 获取链路编号 + try { + traceId = TraceContext.traceId(); + if (StrUtil.isNotBlank(traceId)) { + return traceId; + } + } catch (Throwable ignore) { + } + // 尝试从map中获取 + traceId = threadTraceIdMap.get(Thread.currentThread()); + if (StrUtil.isNotBlank(traceId)) { + return traceId; + } // 通过自定义扩展的tracer产生traceId, 在Spring容器加载完成前会获取不到对应的Bean ITrace tracer = null; try { @@ -42,19 +91,19 @@ public class TracerUtils { } catch (Throwable ignored) { } } - // 通过 SkyWalking 获取链路编号 - try { - String traceId = TraceContext.traceId(); - if (StrUtil.isNotBlank(traceId)) { - return traceId; - } - } catch (Throwable ignore) { - } // TODO 芋艿 多次调用会问题 return defaultTraceId(); } + /** + * 仅仅获取Skywalking 中的traceId, 若无skywalking,则会返回"" + * + * @return skywalking 的traceId + */ + public static String getSkywalkingTraceId() { + return TraceContext.traceId(); + } /** * 从Spring 容器中获取 ITrace 类,返回可以为null * diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java new file mode 100644 index 000000000..76fc93eb4 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java @@ -0,0 +1,68 @@ +package cn.iocoder.dashboard.framework.tracer.filter; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.lang.Nullable; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.AsyncHandlerInterceptor; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * 对Spring Mvc 的请求拦截, 添加traceId. + * + * @author mashu + */ + +@Slf4j +@Component +public class SpringMvcTraceFilter implements HandlerInterceptor { + + @Value("${cn.iocoder.tracer.name:global-trace-id}") + private String traceIdName; + + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + // 请求中traceId + String reqTraceId = (String)request.getAttribute(traceIdName); + // skywalking中的traceId + String skywalkingTraceId = TracerUtils.getSkywalkingTraceId(); + String traceId ; + if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { + // 两者皆空,添加默认的. + traceId = TracerUtils.getTraceId(); + request.setAttribute(traceIdName, traceId); + } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){ + // 若请求空,则添加,为没有skywalking的系统添加一个TraceId + traceId = skywalkingTraceId; + request.setAttribute(traceIdName, traceId); + } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { + // 请求非空, skywalking为空 + traceId = reqTraceId; + } else { + // 两者皆非空,不动请求头 + traceId = skywalkingTraceId; + } + TracerUtils.saveThreadTraceId(traceId); + log.debug("请求进入,添加traceId[{}]", traceId); + return true; + } + + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { + // 请求结束,删除本地的链路流水号 + log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId()); + TracerUtils.deleteThreadTraceId(); + + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java new file mode 100644 index 000000000..4dddc0803 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java @@ -0,0 +1,23 @@ +package cn.iocoder.dashboard.framework.tracer.filter; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import javax.annotation.Resource; + +@Configuration +@Component +public class WebConfig implements WebMvcConfigurer { + + @Resource + private SpringMvcTraceFilter springMvcTraceFilter; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(this.springMvcTraceFilter).addPathPatterns("/**"); + } + +} \ No newline at end of file From 5b181727948707fd8bc6cedf33cd1a24b8a31722 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 21:06:36 +0800 Subject: [PATCH 11/28] =?UTF-8?q?=E4=BD=BF=E7=94=A8FastThreadLocal?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2Map=E6=9D=A5=E7=BB=B4=E6=8A=A4traceId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/tracer/core/util/TracerUtils.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index a7cd841da..2d3b47068 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.framework.tracer.core.util; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.dashboard.framework.tracer.core.ITrace; +import io.netty.util.concurrent.FastThreadLocal; import org.apache.skywalking.apm.toolkit.trace.TraceContext; import java.util.Map; @@ -19,7 +20,7 @@ public class TracerUtils { /** * 维护请求线程对应的TraceId */ - private final static Map threadTraceIdMap = new ConcurrentHashMap<>(); + private static FastThreadLocal traceIdMap = new FastThreadLocal<>(); /** * 保存链路流水号 @@ -27,7 +28,7 @@ public class TracerUtils { * @param traceId 链路流水号 */ public static void saveThreadTraceId(String traceId) { - threadTraceIdMap.put(Thread.currentThread(), traceId); + traceIdMap.set(traceId); } /** @@ -36,14 +37,14 @@ public class TracerUtils { * @return 链路流水号 */ public static String getThreadTraceId() { - return threadTraceIdMap.get(Thread.currentThread()); + return traceIdMap.get(); } /** * 根据线程删除链路流水 */ public static void deleteThreadTraceId() { - threadTraceIdMap.remove(Thread.currentThread()); + traceIdMap.remove(); } /** @@ -75,7 +76,7 @@ public class TracerUtils { } catch (Throwable ignore) { } // 尝试从map中获取 - traceId = threadTraceIdMap.get(Thread.currentThread()); + traceId = traceIdMap.get(); if (StrUtil.isNotBlank(traceId)) { return traceId; } From fe0018b3a837abb386872fa224d5718fd40eb2bf Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 21:07:39 +0800 Subject: [PATCH 12/28] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E6=95=88?= =?UTF-8?q?=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/framework/tracer/core/util/TracerUtils.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index 2d3b47068..a623d66bf 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -6,9 +6,7 @@ import cn.iocoder.dashboard.framework.tracer.core.ITrace; import io.netty.util.concurrent.FastThreadLocal; import org.apache.skywalking.apm.toolkit.trace.TraceContext; -import java.util.Map; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; /** * 链路追踪工具类 From fe956b71f64cd75aef1f791c68d0f7f4bbf600b9 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 22:46:22 +0800 Subject: [PATCH 13/28] =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E4=BB=8Esprin?= =?UTF-8?q?g=20mvc=20=E6=94=B9=E4=B8=BAservlet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tracer/filter/ServletTraceFilter.java | 64 +++++++++++++++++ .../tracer/filter/SpringMvcTraceFilter.java | 68 ------------------- .../framework/tracer/filter/WebConfig.java | 23 ------- 3 files changed, 64 insertions(+), 91 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java new file mode 100644 index 000000000..dc3c1b03b --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java @@ -0,0 +1,64 @@ +package cn.iocoder.dashboard.framework.tracer.filter; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.lang.Nullable; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * 对Spring Mvc 的请求拦截, 添加traceId. + * + * @author mashu + */ + +@Slf4j +@Component +public class ServletTraceFilter implements Filter { + + @Value("${cn.iocoder.tracer.name:global-trace-id}") + private String traceIdName; + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; + HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; + try { + // 请求中traceId + String reqTraceId = (String)httpServletRequest.getHeader(traceIdName); + // skywalking中的traceId + String skywalkingTraceId = TracerUtils.getSkywalkingTraceId(); + String traceId ; + if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { + // 两者皆空,添加默认的. + traceId = TracerUtils.getTraceId(); + httpServletResponse.setHeader(traceIdName, traceId); + } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){ + // 若请求空,则添加,为没有skywalking的系统添加一个TraceId + traceId = skywalkingTraceId; + httpServletResponse.setHeader(traceIdName, traceId); + } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { + // 请求非空, skywalking为空 + traceId = reqTraceId; + } else { + // 两者皆非空,不动请求头 + traceId = skywalkingTraceId; + } + TracerUtils.saveThreadTraceId(traceId); + log.debug("请求进入,添加traceId[{}]", traceId); + + filterChain.doFilter(httpServletRequest, httpServletResponse); + } finally { + // 请求结束,删除本地的链路流水号 + log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId()); + TracerUtils.deleteThreadTraceId(); + } + } +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java deleted file mode 100644 index 76fc93eb4..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java +++ /dev/null @@ -1,68 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.filter; - -import cn.hutool.core.util.StrUtil; -import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; -import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; -import org.springframework.lang.Nullable; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.AsyncHandlerInterceptor; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; - -import javax.servlet.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -/** - * 对Spring Mvc 的请求拦截, 添加traceId. - * - * @author mashu - */ - -@Slf4j -@Component -public class SpringMvcTraceFilter implements HandlerInterceptor { - - @Value("${cn.iocoder.tracer.name:global-trace-id}") - private String traceIdName; - - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - // 请求中traceId - String reqTraceId = (String)request.getAttribute(traceIdName); - // skywalking中的traceId - String skywalkingTraceId = TracerUtils.getSkywalkingTraceId(); - String traceId ; - if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { - // 两者皆空,添加默认的. - traceId = TracerUtils.getTraceId(); - request.setAttribute(traceIdName, traceId); - } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){ - // 若请求空,则添加,为没有skywalking的系统添加一个TraceId - traceId = skywalkingTraceId; - request.setAttribute(traceIdName, traceId); - } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { - // 请求非空, skywalking为空 - traceId = reqTraceId; - } else { - // 两者皆非空,不动请求头 - traceId = skywalkingTraceId; - } - TracerUtils.saveThreadTraceId(traceId); - log.debug("请求进入,添加traceId[{}]", traceId); - return true; - } - - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { - // 请求结束,删除本地的链路流水号 - log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId()); - TracerUtils.deleteThreadTraceId(); - - } - -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java deleted file mode 100644 index 4dddc0803..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java +++ /dev/null @@ -1,23 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.filter; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -import javax.annotation.Resource; - -@Configuration -@Component -public class WebConfig implements WebMvcConfigurer { - - @Resource - private SpringMvcTraceFilter springMvcTraceFilter; - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(this.springMvcTraceFilter).addPathPatterns("/**"); - } - -} \ No newline at end of file From 7f84e44e94d79cc33a109cbd415c981886bf3b73 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 22:56:35 +0800 Subject: [PATCH 14/28] =?UTF-8?q?=E4=BA=A4=E6=98=93=E9=93=BE=E8=B7=AF?= =?UTF-8?q?=E6=B5=81=E6=B0=B4=E5=8F=B7=E4=B8=BA=E7=A9=BA=E6=97=B6=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E9=94=99=E8=AF=AF=E6=97=A5=E5=BF=97,=E4=B8=94?= =?UTF-8?q?=E4=B8=8D=E8=AE=B0=E5=BD=95=E9=93=BE=E8=B7=AF=E8=87=B3skywalkin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iocoder/dashboard/common/annotation/BizTracing.java | 8 ++++---- .../dashboard/common/annotation/BizTracingAop.java | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java index 3b67bf70b..d254552d1 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java @@ -13,11 +13,11 @@ import java.lang.annotation.*; */ public @interface BizTracing { - String BIZ_ID = "bizId"; - String BIZ_TYPE = "bizType"; + String BIZ_ID_TAG = "bizId"; + String BIZ_TYPE_TAG = "bizType"; - String bizId() default "NULL_ID"; + String bizId(); - String bizType() default "NULL_TYPE"; + String bizType(); } diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java index 9486d4d19..f8b2241b6 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java @@ -1,5 +1,6 @@ package cn.iocoder.dashboard.common.annotation; +import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.util.sping.SpElUtil; import lombok.extern.slf4j.Slf4j; import org.apache.skywalking.apm.toolkit.trace.ActiveSpan; @@ -22,8 +23,12 @@ public class BizTracingAop { public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); + if (StrUtil.isBlankIfStr(bizId)) { + log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType); + return; + } log.info("accept biz: bizId[{}], bizType[{}].", bizId, bizType); - ActiveSpan.tag(BizTracing.BIZ_ID, bizId); - ActiveSpan.tag(BizTracing.BIZ_TYPE, bizType); + ActiveSpan.tag(BizTracing.BIZ_ID_TAG, bizId); + ActiveSpan.tag(BizTracing.BIZ_TYPE_TAG, bizType); } } From 27faa6153adece4fcca63a5f3a0cbeef500e1b28 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 23:12:49 +0800 Subject: [PATCH 15/28] =?UTF-8?q?=E4=BC=98=E5=8C=96spel=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F=E8=A7=A3=E6=9E=90=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/annotation/BizTracing.java | 6 +++ .../common/annotation/BizTracingAop.java | 4 +- .../dashboard/util/sping/SpElUtil.java | 40 ++++++++++++++++--- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java index d254552d1..c974d8430 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java @@ -13,7 +13,13 @@ import java.lang.annotation.*; */ public @interface BizTracing { + /** + * 交易流水tag名 + */ String BIZ_ID_TAG = "bizId"; + /** + * 交易类型tag名 + */ String BIZ_TYPE_TAG = "bizType"; String bizId(); diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java index f8b2241b6..3f8b890d8 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java @@ -21,8 +21,8 @@ public class BizTracingAop { @Around(value = "@annotation(bizTracing)") public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { - String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); - String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); + String bizId = (String) SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); + String bizType = (String) SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); if (StrUtil.isBlankIfStr(bizId)) { log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType); return; diff --git a/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java b/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java index 1291d326d..3c5a82a29 100644 --- a/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java +++ b/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java @@ -9,15 +9,31 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +/** + * SpEl解析类 + * + * @author mashu + */ public class SpElUtil { + private static SpelExpressionParser parser = new SpelExpressionParser(); + private static DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); + private SpElUtil() { } - public static String analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { - SpelExpressionParser parser = new SpelExpressionParser(); - DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); + /** + * 解析切面SpEL + * + * @param spElString 表达式 + * @param joinPoint 切面点 + * @return 执行界面 + */ + public static Object analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { // 通过joinPoint获取被注解方法 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); @@ -33,8 +49,22 @@ public class SpElUtil { for (int i = 0; i < args.length; i++) { context.setVariable(paramNames[i], args[i]); } - Object value = expression.getValue(context); - return value == null ? "null" : value.toString(); + return expression.getValue(context); + } + /** + * 批量解析切面SpEL + * + * @param spElStrings 表达式 + * @param joinPoint 切面点 + * @return 执行界面 + */ + public static Map analysisSpEls(List spElStrings, ProceedingJoinPoint joinPoint) { + if (null == spElStrings) { + return null; + } + Map resultMap = new HashMap<>(spElStrings.size()); + spElStrings.forEach(expression -> resultMap.put(expression, analysisSpEl(expression, joinPoint))); + return resultMap; } } From 92c04c0c153817387454937641a36a1233eb00dd Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sun, 21 Mar 2021 20:53:50 +0800 Subject: [PATCH 16/28] =?UTF-8?q?=E7=BB=99ThreadLoocal=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8Afinal=E4=BF=AE=E9=A5=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/framework/tracer/core/util/TracerUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index a623d66bf..a492149b4 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -18,7 +18,7 @@ public class TracerUtils { /** * 维护请求线程对应的TraceId */ - private static FastThreadLocal traceIdMap = new FastThreadLocal<>(); + private final static FastThreadLocal traceIdMap = new FastThreadLocal<>(); /** * 保存链路流水号 From 912bea0c53bf05e4407bec3e5d053285032cd052 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sun, 21 Mar 2021 21:30:11 +0800 Subject: [PATCH 17/28] =?UTF-8?q?=E8=BF=81=E7=A7=BB=E5=8C=85=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE,=E5=B0=86=E6=B3=A8=E8=A7=A3=E4=BB=8Ecommon=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=E5=AF=B9=E5=BA=94=E7=9A=84tracer=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{common => framework/tracer}/annotation/BizTracing.java | 2 +- .../{common => framework/tracer}/annotation/BizTracingAop.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/cn/iocoder/dashboard/{common => framework/tracer}/annotation/BizTracing.java (87%) rename src/main/java/cn/iocoder/dashboard/{common => framework/tracer}/annotation/BizTracingAop.java (95%) diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracing.java similarity index 87% rename from src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java rename to src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracing.java index c974d8430..646f5277a 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracing.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.common.annotation; +package cn.iocoder.dashboard.framework.tracer.annotation; import java.lang.annotation.*; diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java similarity index 95% rename from src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java rename to src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java index 3f8b890d8..3b5bb5fa0 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.common.annotation; +package cn.iocoder.dashboard.framework.tracer.annotation; import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.util.sping.SpElUtil; From 00bcb50ba98ba07f651b99967ef69fce33d17036 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sun, 21 Mar 2021 22:51:08 +0800 Subject: [PATCH 18/28] =?UTF-8?q?AOP=E7=9A=84=E6=B3=A8=E5=85=A5=E4=BB=8E@C?= =?UTF-8?q?omponent=E6=94=B9=E4=B8=BA@Configuration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tracer/annotation/BizTracingAop.java | 1 - .../config/BizTracerAutoConfiguration.java | 29 +++++++++++++++++++ .../tracer/config/BizTracerProperties.java | 9 ++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java index 3b5bb5fa0..3cd1c35fb 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java @@ -16,7 +16,6 @@ import org.springframework.context.annotation.Configuration; */ @Aspect @Slf4j -@Configuration public class BizTracingAop { @Around(value = "@annotation(bizTracing)") diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java new file mode 100644 index 000000000..3b63b9c1a --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java @@ -0,0 +1,29 @@ +package cn.iocoder.dashboard.framework.tracer.config; + +import cn.iocoder.dashboard.framework.tracer.annotation.BizTracingAop; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnClass({BizTracingAop.class}) +@EnableConfigurationProperties(BizTracerProperties.class) +@ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true) +public class BizTracerAutoConfiguration { + + + @Bean + @ConditionalOnMissingBean + public BizTracerProperties bizTracerProperties() { + return new BizTracerProperties(); + } + + @Bean + public BizTracingAop enableBizTracingAop() { + return new BizTracingAop(); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java new file mode 100644 index 000000000..b831464e6 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java @@ -0,0 +1,9 @@ +package cn.iocoder.dashboard.framework.tracer.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("yudao.tracer") +@Data +public class BizTracerProperties { +} From 76d91ac7db2855e1daca8abaeff459065bcd70bc Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Sun, 21 Mar 2021 22:52:42 +0800 Subject: [PATCH 19/28] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/tracer/config/BizTracerAutoConfiguration.java | 5 +++++ .../framework/tracer/config/BizTracerProperties.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java index 3b63b9c1a..b48a9c49b 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java @@ -8,6 +8,11 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +/** + * BizTracer Bean 配置类 + * + * @author mashu + */ @Configuration @ConditionalOnClass({BizTracingAop.class}) @EnableConfigurationProperties(BizTracerProperties.class) diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java index b831464e6..798d2eea3 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java @@ -3,6 +3,11 @@ package cn.iocoder.dashboard.framework.tracer.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +/** + * BizTracer配置类 + * + * @author 麻薯 + */ @ConfigurationProperties("yudao.tracer") @Data public class BizTracerProperties { From 3d2961fabf163e1b3c201295d9a4ae0054a8b84f Mon Sep 17 00:00:00 2001 From: tinyWolf <189192663@qq.com> Date: Fri, 9 Apr 2021 20:32:45 +0800 Subject: [PATCH 20/28] =?UTF-8?q?skywalking=E7=89=88=E6=9C=AC=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E4=B8=BA8.4.0,=E6=94=AF=E6=8C=81=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=94=B6=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/resources/logback-spring.xml | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 9aab6a370..ffb61c14c 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,7 @@ 2.2.0 1.7.0 - 8.3.0 + 8.4.0 6.1 2.3.1 diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index 840addd7e..fcf202143 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -25,6 +25,9 @@ 10 + + + @@ -35,6 +38,7 @@ + From d16ca426b59af7b467844f422de241b2e3e4d3b4 Mon Sep 17 00:00:00 2001 From: tinyWolf <189192663@qq.com> Date: Fri, 9 Apr 2021 20:41:54 +0800 Subject: [PATCH 21/28] =?UTF-8?q?=E4=B8=8D=E5=86=8D=E4=BA=A7=E7=94=9F?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=B5=81=E6=B0=B4=E5=8F=B7,=E4=BB=A5skywalki?= =?UTF-8?q?ng=E4=B8=BA=E5=87=86,=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=9A=84=E6=8B=A6=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/tracer/core/ITrace.java | 16 ---- .../tracer/core/util/TracerUtils.java | 94 +------------------ .../tracer/filter/ServletTraceFilter.java | 64 ------------- 3 files changed, 1 insertion(+), 173 deletions(-) delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java deleted file mode 100644 index 483b408db..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java +++ /dev/null @@ -1,16 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.core; - -/** - * 用于扩展获取traceId的场景,需要装载到Spring bean容器中. - * - * @author 麻薯 - */ -public interface ITrace { - - /** - * 用于接入三方traceId - * - * @return traceId - */ - String getTraceId(); -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index a492149b4..25562a8eb 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -1,9 +1,5 @@ package cn.iocoder.dashboard.framework.tracer.core.util; -import cn.hutool.core.util.StrUtil; -import cn.hutool.extra.spring.SpringUtil; -import cn.iocoder.dashboard.framework.tracer.core.ITrace; -import io.netty.util.concurrent.FastThreadLocal; import org.apache.skywalking.apm.toolkit.trace.TraceContext; import java.util.UUID; @@ -15,36 +11,6 @@ import java.util.UUID; */ public class TracerUtils { - /** - * 维护请求线程对应的TraceId - */ - private final static FastThreadLocal traceIdMap = new FastThreadLocal<>(); - - /** - * 保存链路流水号 - * - * @param traceId 链路流水号 - */ - public static void saveThreadTraceId(String traceId) { - traceIdMap.set(traceId); - } - - /** - * 根据线程获取链路流水号 - * - * @return 链路流水号 - */ - public static String getThreadTraceId() { - return traceIdMap.get(); - } - - /** - * 根据线程删除链路流水 - */ - public static void deleteThreadTraceId() { - traceIdMap.remove(); - } - /** * 私有化构造方法 */ @@ -54,71 +20,13 @@ public class TracerUtils { /** * 获得链路追踪编号 *

- * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。 + * 直接返回skywalking 的TraceId 如果不存在的话为空字符串"" *

- * 默认情况下,我们使用 Apache SkyWalking 的 traceId 作为链路追踪编号。当然,可能会存在并未引入 Skywalking 的情况,此时使用 UUID 。 - * 方法获取顺序: skywalking - > map -> 扩展接口 -> default - * 项目整体获取traceId的优先级 skywalking > 自定义实现接口 > 默认 - * map中的traceId 当且仅当其他情况为产生时才会出现. * * @return 链路追踪编号 */ public static String getTraceId() { - String traceId; - // 通过 SkyWalking 获取链路编号 - try { - traceId = TraceContext.traceId(); - if (StrUtil.isNotBlank(traceId)) { - return traceId; - } - } catch (Throwable ignore) { - } - // 尝试从map中获取 - traceId = traceIdMap.get(); - if (StrUtil.isNotBlank(traceId)) { - return traceId; - } - // 通过自定义扩展的tracer产生traceId, 在Spring容器加载完成前会获取不到对应的Bean - ITrace tracer = null; - try { - tracer = getTracer(); - } catch (Throwable ignore) { - } - if (null != tracer) { - try { - return tracer.getTraceId(); - } catch (Throwable ignored) { - } - } - // TODO 芋艿 多次调用会问题 - - return defaultTraceId(); - } - - /** - * 仅仅获取Skywalking 中的traceId, 若无skywalking,则会返回"" - * - * @return skywalking 的traceId - */ - public static String getSkywalkingTraceId() { return TraceContext.traceId(); } - /** - * 从Spring 容器中获取 ITrace 类,返回可以为null - * - * @return ITrace - */ - private static ITrace getTracer() { - return SpringUtil.getBean(ITrace.class); - } - - /** - * 默认生成TraceId规则为UUID - * - * @return UUID - */ - private static String defaultTraceId() { - return "UUID:" + UUID.randomUUID().toString(); - } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java deleted file mode 100644 index dc3c1b03b..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java +++ /dev/null @@ -1,64 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.filter; - -import cn.hutool.core.util.StrUtil; -import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.lang.Nullable; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.HandlerInterceptor; - -import javax.servlet.*; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -/** - * 对Spring Mvc 的请求拦截, 添加traceId. - * - * @author mashu - */ - -@Slf4j -@Component -public class ServletTraceFilter implements Filter { - - @Value("${cn.iocoder.tracer.name:global-trace-id}") - private String traceIdName; - - @Override - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { - HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; - HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; - try { - // 请求中traceId - String reqTraceId = (String)httpServletRequest.getHeader(traceIdName); - // skywalking中的traceId - String skywalkingTraceId = TracerUtils.getSkywalkingTraceId(); - String traceId ; - if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { - // 两者皆空,添加默认的. - traceId = TracerUtils.getTraceId(); - httpServletResponse.setHeader(traceIdName, traceId); - } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){ - // 若请求空,则添加,为没有skywalking的系统添加一个TraceId - traceId = skywalkingTraceId; - httpServletResponse.setHeader(traceIdName, traceId); - } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { - // 请求非空, skywalking为空 - traceId = reqTraceId; - } else { - // 两者皆非空,不动请求头 - traceId = skywalkingTraceId; - } - TracerUtils.saveThreadTraceId(traceId); - log.debug("请求进入,添加traceId[{}]", traceId); - - filterChain.doFilter(httpServletRequest, httpServletResponse); - } finally { - // 请求结束,删除本地的链路流水号 - log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId()); - TracerUtils.deleteThreadTraceId(); - } - } -} From 81b1f3751ad4c857a0bc8003ae8af37c725a1c0f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 22 Apr 2021 01:10:30 +0800 Subject: [PATCH 22/28] =?UTF-8?q?=E8=A1=A5=E5=85=A8=E4=B8=8B=20tracer=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../tracer/annotation/BizTracing.java | 29 ---------------- ...tion.java => TracerAutoConfiguration.java} | 10 +++--- .../tracer/core/annotation/BizTracing.java | 34 +++++++++++++++++++ .../{ => core}/annotation/BizTracingAop.java | 11 +++--- 5 files changed, 45 insertions(+), 41 deletions(-) delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracing.java rename src/main/java/cn/iocoder/dashboard/framework/tracer/config/{BizTracerAutoConfiguration.java => TracerAutoConfiguration.java} (81%) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java rename src/main/java/cn/iocoder/dashboard/framework/tracer/{ => core}/annotation/BizTracingAop.java (76%) diff --git a/pom.xml b/pom.xml index f3acf5108..fc89b9800 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,7 @@ 2.2.0 1.7.0 - 8.4.0 + 8.5.0 6.1 2.3.1 diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracing.java deleted file mode 100644 index 646f5277a..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracing.java +++ /dev/null @@ -1,29 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.annotation; - -import java.lang.annotation.*; - -@Target({ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -@Inherited - -/** - * 打印业务流水号/业务类型注解 - * - * @author 麻薯 - */ -public @interface BizTracing { - - /** - * 交易流水tag名 - */ - String BIZ_ID_TAG = "bizId"; - /** - * 交易类型tag名 - */ - String BIZ_TYPE_TAG = "bizType"; - - String bizId(); - - String bizType(); - -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java similarity index 81% rename from src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java rename to src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java index b48a9c49b..917bc673c 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerAutoConfiguration.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java @@ -1,6 +1,6 @@ package cn.iocoder.dashboard.framework.tracer.config; -import cn.iocoder.dashboard.framework.tracer.annotation.BizTracingAop; +import cn.iocoder.dashboard.framework.tracer.core.annotation.BizTracingAop; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -9,7 +9,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** - * BizTracer Bean 配置类 + * Tracer 配置类 * * @author mashu */ @@ -17,8 +17,7 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnClass({BizTracingAop.class}) @EnableConfigurationProperties(BizTracerProperties.class) @ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true) -public class BizTracerAutoConfiguration { - +public class TracerAutoConfiguration { @Bean @ConditionalOnMissingBean @@ -27,7 +26,8 @@ public class BizTracerAutoConfiguration { } @Bean - public BizTracingAop enableBizTracingAop() { + @ConditionalOnMissingBean + public BizTracingAop bizTracingAop() { return new BizTracingAop(); } diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java new file mode 100644 index 000000000..c663ce62d --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java @@ -0,0 +1,34 @@ +package cn.iocoder.dashboard.framework.tracer.core.annotation; + +import java.lang.annotation.*; + +/** + * 打印业务编号 / 业务类型注解 + * + * @author 麻薯 + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Inherited +public @interface BizTracing { + + /** + * 业务编号 tag 名 + */ + String ID_TAG = "biz.id"; + /** + * 业务类型 tag 名 + */ + String TYPE_TAG = "biz.type"; + + /** + * @return 业务编号 + */ + String id(); + + /** + * @return 业务类型 + */ + String type(); + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java similarity index 76% rename from src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java rename to src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java index 3cd1c35fb..8ce87c027 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/annotation/BizTracingAop.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.framework.tracer.annotation; +package cn.iocoder.dashboard.framework.tracer.core.annotation; import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.util.sping.SpElUtil; @@ -7,7 +7,6 @@ import org.apache.skywalking.apm.toolkit.trace.ActiveSpan; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; -import org.springframework.context.annotation.Configuration; /** * 业务链路AOP切面 @@ -20,14 +19,14 @@ public class BizTracingAop { @Around(value = "@annotation(bizTracing)") public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { - String bizId = (String) SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); - String bizType = (String) SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); + String bizId = (String) SpElUtil.analysisSpEl(bizTracing.id(), joinPoint); + String bizType = (String) SpElUtil.analysisSpEl(bizTracing.type(), joinPoint); if (StrUtil.isBlankIfStr(bizId)) { log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType); return; } log.info("accept biz: bizId[{}], bizType[{}].", bizId, bizType); - ActiveSpan.tag(BizTracing.BIZ_ID_TAG, bizId); - ActiveSpan.tag(BizTracing.BIZ_TYPE_TAG, bizType); + ActiveSpan.tag(BizTracing.ID_TAG, bizId); + ActiveSpan.tag(BizTracing.TYPE_TAG, bizType); } } From e351cdf7850b2af4de5927e0ba2514451457e701 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 26 Apr 2021 01:35:05 +0800 Subject: [PATCH 23/28] =?UTF-8?q?=E5=AE=8C=E5=96=84=20skywalking=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E7=9A=84=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ++ .../config/TracerAutoConfiguration.java | 36 ++++++++-- ...rProperties.java => TracerProperties.java} | 2 +- .../{BizTracing.java => BizTrace.java} | 7 +- .../tracer/core/annotation/BizTracingAop.java | 32 --------- .../tracer/core/aop/BizTraceAspect.java | 65 +++++++++++++++++++ .../tracer/core/filter/TraceFilter.java | 33 ++++++++++ .../tracer/core/util/TracerUtils.java | 8 +-- .../LocalLogbackPatternConverter.java | 42 ------------ .../TraceIdPatternLogbackLayout.java | 37 ----------- .../web/core/enums/FilterOrderEnum.java | 1 + .../controller/auth/SysAuthController.java | 2 + src/main/resources/logback-spring.xml | 19 ++++-- 13 files changed, 159 insertions(+), 130 deletions(-) rename src/main/java/cn/iocoder/dashboard/framework/tracer/config/{BizTracerProperties.java => TracerProperties.java} (87%) rename src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/{BizTracing.java => BizTrace.java} (82%) delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/core/filter/TraceFilter.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java diff --git a/pom.xml b/pom.xml index fc89b9800..546c01014 100644 --- a/pom.xml +++ b/pom.xml @@ -196,6 +196,11 @@ apm-toolkit-logback-1.x ${skywalking.version} + + org.apache.skywalking + apm-toolkit-opentracing + ${skywalking.version} + de.codecentric diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java index 917bc673c..aeb90af56 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerAutoConfiguration.java @@ -1,10 +1,15 @@ package cn.iocoder.dashboard.framework.tracer.config; -import cn.iocoder.dashboard.framework.tracer.core.annotation.BizTracingAop; +import cn.iocoder.dashboard.framework.tracer.core.aop.BizTraceAspect; +import cn.iocoder.dashboard.framework.tracer.core.filter.TraceFilter; +import cn.iocoder.dashboard.framework.web.core.enums.FilterOrderEnum; +import io.opentracing.Tracer; +import org.apache.skywalking.apm.toolkit.opentracing.SkywalkingTracer; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -14,21 +19,38 @@ import org.springframework.context.annotation.Configuration; * @author mashu */ @Configuration -@ConditionalOnClass({BizTracingAop.class}) -@EnableConfigurationProperties(BizTracerProperties.class) +@ConditionalOnClass({BizTraceAspect.class}) +@EnableConfigurationProperties(TracerProperties.class) @ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true) public class TracerAutoConfiguration { @Bean @ConditionalOnMissingBean - public BizTracerProperties bizTracerProperties() { - return new BizTracerProperties(); + public TracerProperties bizTracerProperties() { + return new TracerProperties(); } @Bean @ConditionalOnMissingBean - public BizTracingAop bizTracingAop() { - return new BizTracingAop(); + public BizTraceAspect bizTracingAop() { + return new BizTraceAspect(); + } + + @Bean + @ConditionalOnMissingBean + public Tracer tracer() { + return new SkywalkingTracer(); + } + + /** + * 创建 TraceFilter 过滤器,响应 header 设置 traceId + */ + @Bean + public FilterRegistrationBean traceFilter() { + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); + registrationBean.setFilter(new TraceFilter()); + registrationBean.setOrder(FilterOrderEnum.TRACE_FILTER); + return registrationBean; } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerProperties.java similarity index 87% rename from src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java rename to src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerProperties.java index 798d2eea3..093179cad 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/config/BizTracerProperties.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/config/TracerProperties.java @@ -10,5 +10,5 @@ import org.springframework.boot.context.properties.ConfigurationProperties; */ @ConfigurationProperties("yudao.tracer") @Data -public class BizTracerProperties { +public class TracerProperties { } diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTrace.java similarity index 82% rename from src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java rename to src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTrace.java index c663ce62d..c371cabc1 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracing.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTrace.java @@ -10,7 +10,7 @@ import java.lang.annotation.*; @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited -public @interface BizTracing { +public @interface BizTrace { /** * 业务编号 tag 名 @@ -21,6 +21,11 @@ public @interface BizTracing { */ String TYPE_TAG = "biz.type"; + /** + * @return 操作名 + */ + String operationName() default ""; + /** * @return 业务编号 */ diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java deleted file mode 100644 index 8ce87c027..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/annotation/BizTracingAop.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.core.annotation; - -import cn.hutool.core.util.StrUtil; -import cn.iocoder.dashboard.util.sping.SpElUtil; -import lombok.extern.slf4j.Slf4j; -import org.apache.skywalking.apm.toolkit.trace.ActiveSpan; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; - -/** - * 业务链路AOP切面 - * - * @author mashu - */ -@Aspect -@Slf4j -public class BizTracingAop { - - @Around(value = "@annotation(bizTracing)") - public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { - String bizId = (String) SpElUtil.analysisSpEl(bizTracing.id(), joinPoint); - String bizType = (String) SpElUtil.analysisSpEl(bizTracing.type(), joinPoint); - if (StrUtil.isBlankIfStr(bizId)) { - log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType); - return; - } - log.info("accept biz: bizId[{}], bizType[{}].", bizId, bizType); - ActiveSpan.tag(BizTracing.ID_TAG, bizId); - ActiveSpan.tag(BizTracing.TYPE_TAG, bizType); - } -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java new file mode 100644 index 000000000..5f1d945b0 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java @@ -0,0 +1,65 @@ +package cn.iocoder.dashboard.framework.tracer.core.aop; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.dashboard.framework.tracer.core.annotation.BizTrace; +import cn.iocoder.dashboard.util.sping.SpElUtil; +import io.opentracing.Span; +import io.opentracing.Tracer; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; + +import javax.annotation.Resource; + +/** + * {@link BizTrace} 切面,记录业务链路 + * + * @author mashu + */ +@Aspect +@Slf4j +public class BizTraceAspect { + + private static final String BIZ_OPERATION_NAME_PREFIX = "Biz/"; + + @Resource + private Tracer tracer; + + @Around(value = "@annotation(trace)") + public Object around(ProceedingJoinPoint joinPoint, BizTrace trace) throws Throwable { + // 创建 span + String operationName = getOperationName(joinPoint, trace); + Span span = tracer.buildSpan(operationName).startManual(); + try { + // 执行原有方法 + return joinPoint.proceed(); + } finally { + // 设置 Span 的 biz 属性 + setBizTag(span, joinPoint, trace); + // 完成 Span + span.finish(); + } + } + + private String getOperationName(ProceedingJoinPoint joinPoint, BizTrace trace) { + // 自定义操作名 + if (StrUtil.isNotEmpty(trace.operationName())) { + return BIZ_OPERATION_NAME_PREFIX + trace.operationName(); + } + // 默认操作名,使用方法名 + return BIZ_OPERATION_NAME_PREFIX + + joinPoint.getSignature().getDeclaringType().getSimpleName() + + "/" + joinPoint.getSignature().getName(); + } + + private void setBizTag(Span span, ProceedingJoinPoint joinPoint, BizTrace trace) { + try { + span.setTag(BizTrace.TYPE_TAG, StrUtil.toString(SpElUtil.analysisSpEl(trace.type(), joinPoint))); + span.setTag(BizTrace.ID_TAG, StrUtil.toString(SpElUtil.analysisSpEl(trace.id(), joinPoint))); + } catch (Exception ex) { + log.error("[around][解析 bizType 与 bizId 发生异常]", ex); + } + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/filter/TraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/filter/TraceFilter.java new file mode 100644 index 000000000..bedd52889 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/filter/TraceFilter.java @@ -0,0 +1,33 @@ +package cn.iocoder.dashboard.framework.tracer.core.filter; + +import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * Trace 过滤器,打印 traceId 到 header 中返回 + * + * @author 芋道源码 + */ +public class TraceFilter extends OncePerRequestFilter { + + /** + * Header 名 - 链路追踪编号 + */ + private static final String HEADER_NAME_TRACE_ID = "trace-id"; + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws IOException, ServletException { + // 设置响应 traceId + response.addHeader(HEADER_NAME_TRACE_ID, TracerUtils.getTraceId()); + // 继续过滤 + chain.doFilter(request, response); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index 25562a8eb..e8af904ba 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -2,8 +2,6 @@ package cn.iocoder.dashboard.framework.tracer.core.util; import org.apache.skywalking.apm.toolkit.trace.TraceContext; -import java.util.UUID; - /** * 链路追踪工具类 * @@ -18,10 +16,8 @@ public class TracerUtils { } /** - * 获得链路追踪编号 - *

- * 直接返回skywalking 的TraceId 如果不存在的话为空字符串"" - *

+ * 获得链路追踪编号,直接返回 SkyWalking 的 TraceId。 + * 如果不存在的话为空字符串!!! * * @return 链路追踪编号 */ diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java deleted file mode 100644 index 3b69098d2..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/LocalLogbackPatternConverter.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - - -package cn.iocoder.dashboard.framework.tracer.skywalking; - -import ch.qos.logback.classic.pattern.ClassicConverter; -import ch.qos.logback.classic.spi.ILoggingEvent; -import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; - -/** - * Created by mashu on 2021/3/6. - */ -public class LocalLogbackPatternConverter extends ClassicConverter { - /** - * 作为默认的方式, 从{@link TracerUtils}中获取traceId, - * 需要用这个去替代logback的Layout, - * 同时避免了sky-walking agent生效的情况下仍然输出定值的问题. - * - * @param iLoggingEvent the event - * @return the traceId: UUID, or the real traceId. - */ - @Override - public String convert(ILoggingEvent iLoggingEvent) { - return TracerUtils.getTraceId(); - } -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java deleted file mode 100644 index 2f13ce517..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/skywalking/TraceIdPatternLogbackLayout.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - - -package cn.iocoder.dashboard.framework.tracer.skywalking; - -import ch.qos.logback.classic.PatternLayout; - -/** - * Based on the logback-component convert register mechanism, - * register {@link LocalLogbackPatternConverter} as a new convert, match to "tid". - * You can use "%tid" in logback config file, "Pattern" section. - * If sky-walking agent is not active mode, it will use UUID as tid. - *

- * logback 的转换组件,为tid 添加占位符的转换器 - * Created by mashu on 2021/3/6. - */ -public class TraceIdPatternLogbackLayout extends PatternLayout { - static { - defaultConverterMap.put("tid", LocalLogbackPatternConverter.class.getName()); - } -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/web/core/enums/FilterOrderEnum.java b/src/main/java/cn/iocoder/dashboard/framework/web/core/enums/FilterOrderEnum.java index 7c6ac8c6a..93bdb032e 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/web/core/enums/FilterOrderEnum.java +++ b/src/main/java/cn/iocoder/dashboard/framework/web/core/enums/FilterOrderEnum.java @@ -9,6 +9,7 @@ public interface FilterOrderEnum { int CORS_FILTER = Integer.MIN_VALUE; + int TRACE_FILTER = CORS_FILTER + 1; int REQUEST_BODY_CACHE_FILTER = Integer.MIN_VALUE + 500; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java index 39bef0a17..16612f2f6 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/auth/SysAuthController.java @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.modules.system.controller.auth; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.framework.logger.operatelog.core.annotations.OperateLog; +import cn.iocoder.dashboard.framework.tracer.core.annotation.BizTrace; import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthLoginReqVO; import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthLoginRespVO; import cn.iocoder.dashboard.modules.system.controller.auth.vo.auth.SysAuthMenuRespVO; @@ -58,6 +59,7 @@ public class SysAuthController { @GetMapping("/get-permission-info") @ApiOperation("获取登陆用户的权限信息") + @BizTrace(id = "1", type = "'user'") public CommonResult getPermissionInfo() { // 获得用户信息 SysUserDO user = userService.getUser(getLoginUserId()); diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index fcf202143..16f7d5261 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -1,11 +1,13 @@ -    +   +         - + %d{ISO8601} | %tid | %thread | %-5level | %msg%n + ./logs/ruoyi-vue-pro-%d{yyyy-MM-dd_HH}.log @@ -20,13 +22,20 @@ 10MB + 0 10 + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%thread] %-5level %logger{36} -%msg%n + + s @@ -35,12 +44,14 @@ + + @@ -48,7 +59,7 @@ -        +              @@ -56,4 +67,4 @@ - \ No newline at end of file + From 33ab45fd0086a11582f2bb1c12bfae73938399b2 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 26 Apr 2021 09:46:49 +0800 Subject: [PATCH 24/28] =?UTF-8?q?=E6=A2=B3=E7=90=86=20logback-spring.xml?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tracer/core/aop/BizTraceAspect.java | 2 +- src/main/resources/application-local.yaml | 1 + src/main/resources/logback-spring.xml | 62 ++++++++----------- 3 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java index 5f1d945b0..bff0fe300 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/aop/BizTraceAspect.java @@ -58,7 +58,7 @@ public class BizTraceAspect { span.setTag(BizTrace.TYPE_TAG, StrUtil.toString(SpElUtil.analysisSpEl(trace.type(), joinPoint))); span.setTag(BizTrace.ID_TAG, StrUtil.toString(SpElUtil.analysisSpEl(trace.id(), joinPoint))); } catch (Exception ex) { - log.error("[around][解析 bizType 与 bizId 发生异常]", ex); + log.error("[setBizTag][解析 bizType 与 bizId 发生异常]", ex); } } diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index f99655ebe..fd25bfc64 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -146,6 +146,7 @@ spring: logging: file: path: ${user.home}/logs/ # 日志文件的路径 + name: ${spring.application.name} # 日志文件名 --- #################### 芋道相关配置 #################### diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index 16f7d5261..49eb3c249 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -1,70 +1,62 @@ -   -   + + + + + +       - %d{ISO8601} | %tid | %thread | %-5level | %msg%n + ${PATTERN_DEFAULT} + - - ./logs/ruoyi-vue-pro-%d{yyyy-MM-dd_HH}.log - 3 - - %d{ISO8601} | %tid | %thread | %-5level | %msg%n + ${PATTERN_DEFAULT} - - 10MB - + + ${LOG_FILE}.%d{yyyy-MM-dd}.%i.log} + 10MB + 7 + false + - 0 10 - + - %d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%thread] %-5level %logger{36} -%msg%n + ${PATTERN_DEFAULT} - s + - - + + + + + - - - + + + + - - - - - - - -        -     -   - - - - - From d5a42a92df8817ece8d4e0a9c19cfbcc2f4055e1 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 26 Apr 2021 20:47:42 +0800 Subject: [PATCH 25/28] =?UTF-8?q?=E6=A2=B3=E7=90=86=20logback-spring.xml?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-local.yaml | 4 +-- src/main/resources/logback-spring.xml | 30 +++++++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index fd25bfc64..9c30bf9f5 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -145,8 +145,8 @@ spring: # 日志文件配置 logging: file: - path: ${user.home}/logs/ # 日志文件的路径 - name: ${spring.application.name} # 日志文件名 +# path: ${user.home}/logs/ # 日志文件的路径 + name: ${user.home}/logs/${spring.application.name}.log # 日志文件名,全路径 --- #################### 芋道相关配置 #################### diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index 49eb3c249..fb75bc3b1 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -1,7 +1,11 @@ - + + + - + + +       @@ -13,22 +17,34 @@ + ${PATTERN_DEFAULT} + + ${LOG_FILE} - ${LOG_FILE}.%d{yyyy-MM-dd}.%i.log} - 10MB - 7 - false + + ${LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz} + + ${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-false} + + ${LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-10MB} + + ${LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP:-0} + + ${LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-30} + + 0 - 10 + + 256 From 33412466bb2d1c6029496ac276956fec73c0abd5 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 26 Apr 2021 21:53:54 +0800 Subject: [PATCH 26/28] =?UTF-8?q?=E6=A2=B3=E7=90=86=20logback-spring.xml?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/deploy.sh | 1 + src/main/resources/application-dev.yaml | 2 +- src/main/resources/application-local.yaml | 1 - src/main/resources/logback-spring.xml | 10 ++++------ 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/bin/deploy.sh b/bin/deploy.sh index 479303f69..935d4606b 100644 --- a/bin/deploy.sh +++ b/bin/deploy.sh @@ -26,6 +26,7 @@ JAVA_OPS="-Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$HE # SkyWalking Agent 配置 export SW_AGENT_NAME=$SERVER_NAME export SW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.0.84:11800 +export SW_GRPC_LOG_SERVER_HOST=192.168.0.84 export JAVA_AGENT=-javaagent:/work/skywalking/apache-skywalking-apm-bin/agent/skywalking-agent.jar # 备份 diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml index 1ecf43d6a..526bf3d51 100644 --- a/src/main/resources/application-dev.yaml +++ b/src/main/resources/application-dev.yaml @@ -145,7 +145,7 @@ spring: # 日志文件配置 logging: file: - path: ${user.home}/logs/ # 日志文件的路径 + name: ${user.home}/logs/${spring.application.name}.log # 日志文件名,全路径 --- #################### 芋道相关配置 #################### diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index 9c30bf9f5..8761a78a8 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -145,7 +145,6 @@ spring: # 日志文件配置 logging: file: -# path: ${user.home}/logs/ # 日志文件的路径 name: ${user.home}/logs/${spring.application.name}.log # 日志文件名,全路径 --- #################### 芋道相关配置 #################### diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index fb75bc3b1..1d9bec6dc 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -4,8 +4,7 @@ - - +       @@ -51,7 +50,7 @@ - + ${PATTERN_DEFAULT} @@ -61,9 +60,8 @@ - - - + + From e65488265d744522cc58f73756188fe8275aaf1f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 26 Apr 2021 23:18:13 +0800 Subject: [PATCH 27/28] =?UTF-8?q?UI=20=E7=95=8C=E9=9D=A2=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20skywalking=20=E4=BD=9C=E4=B8=BA=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E4=B8=AD=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/deploy.sh | 1 + ruoyi-ui/src/views/infra/skywalking/index.vue | 2 +- ruoyi-ui/src/views/infra/skywalking/log.vue | 26 +++++++++++++++++++ .../tracer/core/annotation/BizTrace.java | 3 +++ .../framework/tracer/package-info.java | 4 +-- .../controller/auth/SysAuthController.java | 1 - .../test/ToolTestDemoController.java | 8 +++--- 7 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 ruoyi-ui/src/views/infra/skywalking/log.vue diff --git a/bin/deploy.sh b/bin/deploy.sh index 935d4606b..3a156c9b0 100644 --- a/bin/deploy.sh +++ b/bin/deploy.sh @@ -27,6 +27,7 @@ JAVA_OPS="-Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$HE export SW_AGENT_NAME=$SERVER_NAME export SW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.0.84:11800 export SW_GRPC_LOG_SERVER_HOST=192.168.0.84 +export SW_AGENT_TRACE_IGNORE_PATH="Redisson/PING,/actuator/**,/admin/**" export JAVA_AGENT=-javaagent:/work/skywalking/apache-skywalking-apm-bin/agent/skywalking-agent.jar # 备份 diff --git a/ruoyi-ui/src/views/infra/skywalking/index.vue b/ruoyi-ui/src/views/infra/skywalking/index.vue index 00411d64e..843830464 100644 --- a/ruoyi-ui/src/views/infra/skywalking/index.vue +++ b/ruoyi-ui/src/views/infra/skywalking/index.vue @@ -8,7 +8,7 @@ export default { name: "SkyWalking", data() { return { - src: "http://skywalking.shop.iocoder.cn", // TODO 芋艿,后续改成配置读取 + src: "http://skywalking.shop.iocoder.cn/trace", // TODO 芋艿,后续改成配置读取 height: document.documentElement.clientHeight - 94.5 + "px;", loading: true }; diff --git a/ruoyi-ui/src/views/infra/skywalking/log.vue b/ruoyi-ui/src/views/infra/skywalking/log.vue new file mode 100644 index 000000000..06cab5d56 --- /dev/null +++ b/ruoyi-ui/src/views/infra/skywalking/log.vue @@ -0,0 +1,26 @@ +