mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-16 20:15:06 +08:00
项目结构调整 x 16 : 将 monitor、sms、dict 等组件拆分出去
This commit is contained in:
@ -0,0 +1,56 @@
|
||||
package cn.iocoder.yudao.framework.tracer.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.tracer.core.aop.BizTraceAspect;
|
||||
import cn.iocoder.yudao.framework.tracer.core.filter.TraceFilter;
|
||||
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Tracer 配置类
|
||||
*
|
||||
* @author mashu
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({BizTraceAspect.class})
|
||||
@EnableConfigurationProperties(TracerProperties.class)
|
||||
@ConditionalOnProperty(prefix = "yudao.tracer", value = "enable", matchIfMissing = true)
|
||||
public class TracerAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public TracerProperties bizTracerProperties() {
|
||||
return new TracerProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public BizTraceAspect bizTracingAop() {
|
||||
return new BizTraceAspect();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public Tracer tracer() {
|
||||
return new SkywalkingTracer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 TraceFilter 过滤器,响应 header 设置 traceId
|
||||
*/
|
||||
@Bean
|
||||
public FilterRegistrationBean<TraceFilter> traceFilter() {
|
||||
FilterRegistrationBean<TraceFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
registrationBean.setFilter(new TraceFilter());
|
||||
registrationBean.setOrder(WebFilterOrderEnum.TRACE_FILTER);
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package cn.iocoder.yudao.framework.tracer.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* BizTracer配置类
|
||||
*
|
||||
* @author 麻薯
|
||||
*/
|
||||
@ConfigurationProperties("yudao.tracer")
|
||||
@Data
|
||||
public class TracerProperties {
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package cn.iocoder.yudao.framework.tracer.core.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 打印业务编号 / 业务类型注解
|
||||
*
|
||||
* 使用时,需要设置 SkyWalking OAP Server 的 application.yaml 配置文件,修改 SW_SEARCHABLE_TAG_KEYS 配置项,
|
||||
* 增加 biz.type 和 biz.id 两值,然后重启 SkyWalking OAP Server 服务器。
|
||||
*
|
||||
* @author 麻薯
|
||||
*/
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
public @interface BizTrace {
|
||||
|
||||
/**
|
||||
* 业务编号 tag 名
|
||||
*/
|
||||
String ID_TAG = "biz.id";
|
||||
/**
|
||||
* 业务类型 tag 名
|
||||
*/
|
||||
String TYPE_TAG = "biz.type";
|
||||
|
||||
/**
|
||||
* @return 操作名
|
||||
*/
|
||||
String operationName() default "";
|
||||
|
||||
/**
|
||||
* @return 业务编号
|
||||
*/
|
||||
String id();
|
||||
|
||||
/**
|
||||
* @return 业务类型
|
||||
*/
|
||||
String type();
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package cn.iocoder.yudao.framework.tracer.core.aop;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.tracer.core.annotation.BizTrace;
|
||||
import cn.iocoder.yudao.framework.common.util.sping.SpringExpressionUtils;
|
||||
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;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
/**
|
||||
* {@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 {
|
||||
Map<String, Object> result = SpringExpressionUtils.parseExpressions(joinPoint, asList(trace.type(), trace.id()));
|
||||
span.setTag(BizTrace.TYPE_TAG, MapUtil.getStr(result, trace.type()));
|
||||
span.setTag(BizTrace.ID_TAG, MapUtil.getStr(result, trace.id()));
|
||||
} catch (Exception ex) {
|
||||
log.error("[setBizTag][解析 bizType 与 bizId 发生异常]", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package cn.iocoder.yudao.framework.tracer.core.filter;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.monitor.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);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* 使用 SkyWalking 组件,作为链路追踪、日志中心。
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
package cn.iocoder.yudao.framework.tracer;
|
@ -0,0 +1 @@
|
||||
<http://www.iocoder.cn/Spring-Boot/SkyWalking/?yudao>
|
Reference in New Issue
Block a user