yshop-pro init

This commit is contained in:
hupeng
2023-05-19 18:29:26 +08:00
commit 6ff21a3799
1846 changed files with 114288 additions and 0 deletions

View File

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>co.yixiang.boot</groupId>
<artifactId>yshop-framework</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yshop-spring-boot-starter-monitor</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>服务监控,提供链路追踪、日志服务、指标收集等等功能</description>
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
<dependencies>
<dependency>
<groupId>co.yixiang.boot</groupId>
<artifactId>yshop-common</artifactId>
</dependency>
<!-- Spring 核心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>provided</scope> <!-- 设置为 provided只有 TraceFilter 使用 -->
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope> <!-- 设置为 provided只有 TraceFilter 使用 -->
</dependency>
<!-- 监控相关 -->
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
</dependency>
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
</dependency>
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
</dependency>
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-opentracing</artifactId>
</dependency>
<!-- Micrometer 对 Prometheus 的支持 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId> <!-- 实现 Spring Boot Admin Server 服务端 -->
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
package co.yixiang.yshop.framework.tracer.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* BizTracer配置类
*
* @author 麻薯
*/
@ConfigurationProperties("yshop.tracer")
@Data
public class TracerProperties {
}

View File

@ -0,0 +1,27 @@
package co.yixiang.yshop.framework.tracer.config;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
/**
* Metrics 配置类
*
* @author yshop
*/
@AutoConfiguration
@ConditionalOnClass({MeterRegistryCustomizer.class})
@ConditionalOnProperty(prefix = "yshop.metrics", value = "enable", matchIfMissing = true) // 允许使用 yshop.metrics.enable=false 禁用 Metrics
public class YshopMetricsAutoConfiguration {
@Bean
public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags(
@Value("${spring.application.name}") String applicationName) {
return registry -> registry.config().commonTags("application", applicationName);
}
}

View File

@ -0,0 +1,55 @@
package co.yixiang.yshop.framework.tracer.config;
import co.yixiang.yshop.framework.common.enums.WebFilterOrderEnum;
import co.yixiang.yshop.framework.tracer.core.aop.BizTraceAspect;
import co.yixiang.yshop.framework.tracer.core.filter.TraceFilter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;
/**
* Tracer 配置类
*
* @author mashu
*/
@AutoConfiguration
@ConditionalOnClass({BizTraceAspect.class})
@EnableConfigurationProperties(TracerProperties.class)
@ConditionalOnProperty(prefix = "yshop.tracer", value = "enable", matchIfMissing = true)
public class YshopTracerAutoConfiguration {
// TODO @yshop重要。目前 opentracing 版本存在冲突,要么保证 skywalking要么保证阿里云短信 sdk
// @Bean
// public TracerProperties bizTracerProperties() {
// return new TracerProperties();
// }
//
// @Bean
// public BizTraceAspect bizTracingAop() {
// return new BizTraceAspect(tracer());
// }
//
// @Bean
// public Tracer tracer() {
// // 创建 SkywalkingTracer 对象
// SkywalkingTracer tracer = new SkywalkingTracer();
// // 设置为 GlobalTracer 的追踪器
// GlobalTracer.register(tracer);
// return tracer;
// }
/**
* 创建 TraceFilter 过滤器,响应 header 设置 traceId
*/
@Bean
public FilterRegistrationBean<TraceFilter> traceFilter() {
FilterRegistrationBean<TraceFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new TraceFilter());
registrationBean.setOrder(WebFilterOrderEnum.TRACE_FILTER);
return registrationBean;
}
}

View File

@ -0,0 +1,42 @@
package co.yixiang.yshop.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();
}

View File

@ -0,0 +1,77 @@
package co.yixiang.yshop.framework.tracer.core.aop;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import co.yixiang.yshop.framework.tracer.core.annotation.BizTrace;
import co.yixiang.yshop.framework.common.util.spring.SpringExpressionUtils;
import co.yixiang.yshop.framework.tracer.core.util.TracerFrameworkUtils;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.tag.Tags;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import java.util.Map;
import static java.util.Arrays.asList;
/**
* {@link BizTrace} 切面,记录业务链路
*
* @author mashu
*/
@Aspect
@AllArgsConstructor
@Slf4j
public class BizTraceAspect {
private static final String BIZ_OPERATION_NAME_PREFIX = "Biz/";
private final 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)
.withTag(Tags.COMPONENT.getKey(), "biz")
.start();
try {
// 执行原有方法
return joinPoint.proceed();
} catch (Throwable throwable) {
TracerFrameworkUtils.onError(throwable, span);
throw throwable;
} 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);
}
}
}

View File

@ -0,0 +1,33 @@
package co.yixiang.yshop.framework.tracer.core.filter;
import co.yixiang.yshop.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 yshop
*/
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);
}
}

View File

@ -0,0 +1,46 @@
package co.yixiang.yshop.framework.tracer.core.util;
import io.opentracing.Span;
import io.opentracing.tag.Tags;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
/**
* 链路追踪 Util
*
* @author yshop
*/
public class TracerFrameworkUtils {
/**
* 将异常记录到 Span 中,参考自 com.aliyuncs.utils.TraceUtils
*
* @param throwable 异常
* @param span Span
*/
public static void onError(Throwable throwable, Span span) {
Tags.ERROR.set(span, Boolean.TRUE);
if (throwable != null) {
span.log(errorLogs(throwable));
}
}
private static Map<String, Object> errorLogs(Throwable throwable) {
Map<String, Object> errorLogs = new HashMap<String, Object>(10);
errorLogs.put("event", Tags.ERROR.getKey());
errorLogs.put("error.object", throwable);
errorLogs.put("error.kind", throwable.getClass().getName());
String message = throwable.getCause() != null ? throwable.getCause().getMessage() : throwable.getMessage();
if (message != null) {
errorLogs.put("message", message);
}
StringWriter sw = new StringWriter();
throwable.printStackTrace(new PrintWriter(sw));
errorLogs.put("stack", sw.toString());
return errorLogs;
}
}

View File

@ -0,0 +1,6 @@
/**
* 使用 SkyWalking 组件,作为链路追踪、日志中心。
*
* @author yshop
*/
package co.yixiang.yshop.framework.tracer;

View File

@ -0,0 +1,2 @@
co.yixiang.yshop.framework.tracer.config.YshopTracerAutoConfiguration
co.yixiang.yshop.framework.tracer.config.YshopMetricsAutoConfiguration

View File

@ -0,0 +1 @@
<https://www.iocoder.cn/Spring-Boot/Admin/?yshop>

View File

@ -0,0 +1 @@
<https://www.iocoder.cn/Spring-Boot/Actuator/?yshop>

View File

@ -0,0 +1 @@
<https://www.yixiang.co/Spring-Boot/SkyWalking/?yshop>