Commit 3018866c62ad9231a65c810ab69e5c14e5d0a67a

Authored by dark
1 parent 11cf3853

为二次开发预留接入产生第三方流水号的接口

src/main/java/cn/iocoder/dashboard/framework/tracer/core/ITrace.java 0 → 100644
  1 +package cn.iocoder.dashboard.framework.tracer.core;
  2 +
  3 +/**
  4 + * 用于扩展获取traceId的场景,需要装载到Spring bean容器中.
  5 + *
  6 + * @author 麻薯
  7 + */
  8 +public interface ITrace {
  9 +
  10 + /**
  11 + * 用于接入三方traceId
  12 + *
  13 + * @return traceId
  14 + */
  15 + String getTraceId();
  16 +}
... ...
src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java
1 1 package cn.iocoder.dashboard.framework.tracer.core.util;
2 2  
3 3 import cn.hutool.core.util.StrUtil;
  4 +import cn.iocoder.dashboard.framework.tracer.core.ITrace;
  5 +import cn.iocoder.dashboard.util.bean.SpringUtil;
4 6 import org.apache.skywalking.apm.toolkit.trace.TraceContext;
5 7  
6 8 import java.util.UUID;
... ... @@ -13,25 +15,61 @@ import java.util.UUID;
13 15 public class TracerUtils {
14 16  
15 17 /**
  18 + * 私有化构造方法
  19 + */
  20 + private TracerUtils() {
  21 + }
  22 +
  23 + /**
16 24 * 获得链路追踪编号
17   - *
  25 + * <p>
18 26 * 一般来说,通过链路追踪编号,可以将访问日志,错误日志,链路追踪日志,logger 打印日志等,结合在一起,从而进行排错。
19   - *
  27 + * <p>
20 28 * 默认情况下,我们使用 Apache SkyWalking 的 traceId 作为链路追踪编号。当然,可能会存在并未引入 Skywalking 的情况,此时使用 UUID 。
21 29 *
22 30 * @return 链路追踪编号
23 31 */
24 32 public static String getTraceId() {
  33 + // 通过自定义扩展的tracer产生traceId, 在Spring容器加载完成前会获取不到对应的Bean
  34 + ITrace tracer = null;
  35 + try {
  36 + tracer = getTracer();
  37 + } catch (Throwable ignore) {
  38 + }
  39 + if (null != tracer) {
  40 + try {
  41 + return tracer.getTraceId();
  42 + } catch (Throwable ignored) {
  43 + }
  44 + }
25 45 // 通过 SkyWalking 获取链路编号
26 46 try {
27 47 String traceId = TraceContext.traceId();
28 48 if (StrUtil.isNotBlank(traceId)) {
29 49 return traceId;
30 50 }
31   - } catch (Throwable ignore) {}
  51 + } catch (Throwable ignore) {
  52 + }
32 53 // TODO 芋艿 多次调用会问题
33 54  
34   - // TODO 麻薯 定义一个给外部扩展的接口,默认在未接入Skywalking时,输出UUID
  55 + return defaultTraceId();
  56 + }
  57 +
  58 + /**
  59 + * 从Spring 容器中获取 ITrace 类,返回可以为null
  60 + *
  61 + * @return ITrace
  62 + */
  63 + private static ITrace getTracer() {
  64 + return SpringUtil.getBean(ITrace.class);
  65 + }
  66 +
  67 + /**
  68 + * 默认生成TraceId规则为UUID
  69 + *
  70 + * @return UUID
  71 + */
  72 + private static String defaultTraceId() {
35 73 return "UUID:" + UUID.randomUUID().toString();
36 74 }
37 75  
... ...
src/main/java/cn/iocoder/dashboard/util/bean/SpringUtil.java 0 → 100644
  1 +package cn.iocoder.dashboard.util.bean;
  2 +
  3 +import org.springframework.beans.BeansException;
  4 +
  5 +import org.springframework.beans.factory.NoSuchBeanDefinitionException;
  6 +
  7 +import org.springframework.context.ApplicationContext;
  8 +
  9 +import org.springframework.context.ApplicationContextAware;
  10 +
  11 +import org.springframework.stereotype.Component;
  12 +
  13 +
  14 +@Component
  15 +public class SpringUtil implements ApplicationContextAware {
  16 +
  17 + /**
  18 + * Spring context
  19 + */
  20 + private static ApplicationContext applicationContext = null;
  21 +
  22 + public void setApplicationContext(ApplicationContext context) throws BeansException {
  23 + if (applicationContext == null) {
  24 + applicationContext = context;
  25 + }
  26 + }
  27 +
  28 + public static ApplicationContext getApplicationContext() {
  29 + return applicationContext;
  30 + }
  31 +
  32 + public static void setAppCtx(ApplicationContext webAppCtx) {
  33 + if (webAppCtx != null) {
  34 + applicationContext = webAppCtx;
  35 + }
  36 + }
  37 +
  38 +
  39 + /**
  40 + * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
  41 + */
  42 + public static <T> T getBean(Class<T> clazz) {
  43 + return getApplicationContext() == null ? null : getApplicationContext().getBean(clazz);
  44 + }
  45 +
  46 +
  47 + public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException {
  48 + return getApplicationContext() == null ? null : getApplicationContext() .getBean(name, clazz);
  49 + }
  50 +
  51 +
  52 + public static final Object getBean(String beanName) {
  53 + return getApplicationContext() == null ? null : getApplicationContext() .getBean(beanName);
  54 + }
  55 +
  56 +
  57 + public static final Object getBean(String beanName, String className) throws ClassNotFoundException {
  58 + Class<?> clz = Class.forName(className);
  59 + return getApplicationContext() == null ? null : getApplicationContext() .getBean(beanName, clz.getClass());
  60 +
  61 + }
  62 +
  63 +
  64 + public static boolean containsBean(String name) {
  65 + return getApplicationContext() == null ? null : getApplicationContext() .containsBean(name);
  66 + }
  67 +
  68 +
  69 + public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
  70 + return getApplicationContext() == null ? null : getApplicationContext() .isSingleton(name);
  71 + }
  72 +
  73 +
  74 + public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
  75 + return getApplicationContext() == null ? null : getApplicationContext() .getType(name);
  76 + }
  77 +
  78 +
  79 + public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
  80 + return getApplicationContext() == null ? null : getApplicationContext() .getAliases(name);
  81 + }
  82 +
  83 +}
0 84 \ No newline at end of file
... ...