12.2 应用性能监控

面试重要程度:⭐⭐⭐⭐⭐

常见提问方式: "如何搭建APM监控系统?" "链路追踪的实现原理?" "如何定位性能瓶颈?"

预计阅读时间:40分钟

🔍 APM工具选型与对比

主流APM工具分析

/**
 * APM工具选型分析
 */
public class APMToolComparison {
    
    /**
     * APM工具枚举
     */
    public enum APMTool {
        SKYWALKING("SkyWalking", "Apache开源", "Java生态友好,无侵入", "免费"),
        PINPOINT("Pinpoint", "Naver开源", "详细的调用链分析", "免费"),
        ZIPKIN("Zipkin", "Twitter开源", "轻量级,易部署", "免费"),
        JAEGER("Jaeger", "Uber开源", "云原生,CNCF项目", "免费"),
        NEW_RELIC("New Relic", "商业产品", "功能全面,易用性好", "付费"),
        DATADOG("Datadog", "商业产品", "多云支持,集成度高", "付费");
        
        private final String name;
        private final String vendor;
        private final String features;
        private final String pricing;
        
        APMTool(String name, String vendor, String features, String pricing) {
            this.name = name;
            this.vendor = vendor;
            this.features = features;
            this.pricing = pricing;
        }
    }
    
    /**
     * APM选型决策矩阵
     */
    public static APMTool recommend(ProjectRequirements requirements) {
        if (requirements.getBudget() == Budget.FREE) {
            if (requirements.getEcosystem() == Ecosystem.JAVA) {
                return APMTool.SKYWALKING;
            } else if (requirements.getDeployment() == Deployment.CLOUD_NATIVE) {
                return APMTool.JAEGER;
            } else {
                return APMTool.ZIPKIN;
            }
        } else {
            return requirements.getComplexity() == Complexity.HIGH ? 
                APMTool.NEW_RELIC : APMTool.DATADOG;
        }
    }
    
    public enum Budget { FREE, PAID }
    public enum Ecosystem { JAVA, POLYGLOT, MICROSERVICE }
    public enum Deployment { ON_PREMISE, CLOUD, CLOUD_NATIVE }
    public enum Complexity { LOW, MEDIUM, HIGH }
    
    public static class ProjectRequirements {
        private Budget budget;
        private Ecosystem ecosystem;
        private Deployment deployment;
        private Complexity complexity;
        
        public ProjectRequirements(Budget budget, Ecosystem ecosystem, 
                                 Deployment deployment, Complexity complexity) {
            this.budget = budget;
            this.ecosystem = ecosystem;
            this.deployment = deployment;
            this.complexity = complexity;
        }
        
        // Getters
        public Budget getBudget() { return budget; }
        public Ecosystem getEcosystem() { return ecosystem; }
        public Deployment getDeployment() { return deployment; }
        public Complexity getComplexity() { return complexity; }
    }
}

🕸️ SkyWalking集成实战

SkyWalking配置与集成

/**
 * SkyWalking集成配置
 */
@Configuration
@EnableConfigurationProperties(SkyWalkingProperties.class)
public class SkyWalkingConfig {
    
    /**
     * 自定义链路追踪注解
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Trace {
        String operationName() default "";
        String tag() default "";
    }
    
    /**
     * 链路追踪切面
     */
    @Aspect
    @Component
    public static class TraceAspect {
        
        @Around("@annotation(trace)")
        public Object traceMethod(ProceedingJoinPoint joinPoint, Trace trace) throws Throwable {
            String operationName = trace.operationName().isEmpty() ? 
                joinPoint.getSignature().getName() : trace.operationName();
            
            // 创建Span
            Span span = Tracer.createLocalSpan(operationName);
            
            try {
                if (!trace.tag().isEmpty()) {
                    Tags.COMPONENT.set(span, trace.tag());
                }
                
                Object[] args = joinPoint.getArgs();
                if (args.length > 0) {
                    span.tag("args.count", String.valueOf(args.length));
                }
                
                Object result = joinPoint.proceed();
                
                if (result != null) {
                    span.tag("return.type", result.getClass().getSimpleName());
                }
                
                return result;
            } catch (Exception e) {
                span.errorOccurred();
                span.log(e);
                throw e;
            } finally {
                Tracer.stopSpan();
            }
        }
    }
    
    @ConfigurationProperties(prefix = "skywalking")
    public static class SkyWalkingProperties {
        private boolean enabled = true;
        private String serviceName = "spring-boot-app";
        private String collectorAddress = "http://skywalking-oap:12800";
        private double sampleRate = 1.0;
        
        // Getters and Setters
        public boolean isEnabled() { return enabled; }
        public void setEnabled(boolean enabled) { this.enabled = enabled; }
        public String getServiceName() { return serviceName; }
        public void setServiceName(String serviceName) { this.serviceName = serviceName; }
        public String getCollectorAddress() { return collectorAddress; }
        public void setCollectorAddress(String collectorAddress) { this.collectorAddress = collectorAddress; }
        public double getSampleRate() { return sampleRate; }
        public void setSampleRate(double sampleRate) { this.sampleRate = sampleRate; }
    }
}

SkyWalking Docker部署

# docker-compose.yml for SkyWalking
version: '3.8'

services:
  elas

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

Java面试圣经 文章被收录于专栏

Java面试圣经,带你练透java圣经

全部评论

相关推荐

01-19 12:48
门头沟学院 C++
只想搞钱的鸽子很喜欢...:混账是很多的,还有那些在自己风华正茂的年纪说风凉话讥讽那些下岗前员工的。这些人都是现在职场环境这么烂的帮凶
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务