10 - 性能优化和扩展 🚀

🎯 目标: 全面优化框架性能,提供强大的扩展能力,支持大规模生产环境

🤔 为什么需要性能优化?

在生产环境中,性能是关键因素:

  • 高并发处理: 支持大量并发流程执行
  • 🔄 资源优化: 合理利用CPU、内存等资源
  • 📈 可扩展性: 支持水平和垂直扩展
  • 🎯 低延迟: 减少流程执行延迟
  • 💾 内存管理: 避免内存泄漏和溢出
  • 🔧 可配置性: 灵活的性能调优参数

🏗️ 性能优化架构

graph TB subgraph "连接池层 🏊" A1[线程池管理] A2[数据库连接池] A3[HTTP连接池] A4[缓存连接池] end subgraph "缓存层 💾" B1[本地缓存] B2[分布式缓存] B3[查询缓存] B4[结果缓存] end subgraph "异步处理层 🔄" C1[异步执行器] C2[事件总线] C3[消息队列] C4[批处理器] end subgraph "负载均衡层 ⚖️" D1[流程分发器] D2[步骤调度器] D3[资源分配器] D4[限流器] end subgraph "监控优化层 📊" E1[性能监控] E2[瓶颈分析] E3[自动调优] E4[预警系统] end subgraph "扩展层 🔧" F1[插件系统] F2[SPI机制] F3[热插拔] F4[版本管理] end A1 --> C1 A2 --> B2 A3 --> C3 A4 --> B1 B1 --> D1 B2 --> D2 B3 --> D3 B4 --> D4 C1 --> E1 C2 --> E2 C3 --> E3 C4 --> E4 D1 --> F1 D2 --> F2 D3 --> F3 D4 --> F4

⚡ 线程池优化

🎯 智能线程池管理

java 复制代码
/**
 * 智能线程池管理器
 */
@Component
public class SmartThreadPoolManager {
    
    private static final Logger logger = LoggerFactory.getLogger(SmartThreadPoolManager.class);
    
    private final Map<String, ThreadPoolExecutor> threadPools = new ConcurrentHashMap<>();
    private final ThreadPoolProperties properties;
    private final MeterRegistry meterRegistry;
    private final ScheduledExecutorService monitoringExecutor;
    
    public SmartThreadPoolManager(ThreadPoolProperties properties, MeterRegistry meterRegistry) {
        this.properties = properties;
        this.meterRegistry = meterRegistry;
        this.monitoringExecutor = Executors.newScheduledThreadPool(1);
        
        // 初始化默认线程池
        initializeDefaultPools();
        
        // 启动监控
        startMonitoring();
        
        // 注册关闭钩子
        Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
    }
    
    /**
     * 获取线程池
     */
    public ThreadPoolExecutor getThreadPool(String poolName) {
        return threadPools.computeIfAbsent(poolName, this::createThreadPool);
    }
    
    /**
     * 创建自适应线程池
     */
    private ThreadPoolExecutor createThreadPool(String poolName) {
        ThreadPoolConfig config = properties.getPool(poolName);
        
        // 创建自适应线程池
        AdaptiveThreadPoolExecutor executor = new AdaptiveThreadPoolExecutor(
            config.getCoreSize(),
            config.getMaxSize(),
            config.getKeepAliveTime(),
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(config.getQueueSize()),
            new NamedThreadFactory(poolName),
            new SmartRejectedExecutionHandler(poolName)
        );
        
        // 配置自适应参数
        executor.setAdaptiveConfig(
            config.getTargetUtilization(),
            config.getAdjustmentPeriod(),
            config.getMinAdjustmentStep(),
            config.getMaxAdjustmentStep()
        );
        
        // 注册监控指标
        registerMetrics(poolName, executor);
        
        logger.info("创建线程池: {} - 核心线程数: {}, 最大线程数: {}, 队列大小: {}",
            poolName, config.getCoreSize(), config.getMaxSize(), config.getQueueSize());
        
        return executor;
    }
    
    /**
     * 自适应线程池执行器
     */
    public static class AdaptiveThreadPoolExecutor extends ThreadPoolExecutor {
        
        private volatile double targetUtilization = 0.8;
        private volatile long adjustmentPeriod = 30000; // 30秒
        private volatile int minAdjustmentStep = 1;
        private volatile int maxAdjustmentStep = 5;
        
        private final AtomicLong lastAdjustmentTime = new AtomicLong(0);
        private final AtomicInteger consecutiveAdjustments = new AtomicInteger(0);
        
        public AdaptiveThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
                                         long keepAliveTime, TimeUnit unit,
                                         BlockingQueue<Runnable> workQueue,
                                         ThreadFactory threadFactory,
                                         RejectedExecutionHandler handler) {
            super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
        }
        
        public void setAdaptiveConfig(double targetUtilization, long adjustmentPeriod,
                                    int minAdjustmentStep, int maxAdjustmentStep) {
            this.targetUtilization = targetUtilization;
            this.adjustmentPeriod = adjustmentPeriod;
            this.minAdjustmentStep = minAdjustmentStep;
            this.maxAdjustmentStep = maxAdjustmentStep;
        }
        
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            super.afterExecute(r, t);
            
            // 定期调整线程池大小
            long now = System.currentTimeMillis();
            if (now - lastAdjustmentTime.get() > adjustmentPeriod) {
                if (lastAdjustmentTime.compareAndSet(lastAdjustmentTime.get(), now)) {
                    adjustPoolSize();
                }
            }
        }
        
        /**
         * 调整线程池大小
         */
        private void adjustPoolSize() {
            try {
                double currentUtilization = getCurrentUtilization();
                int currentCoreSize = getCorePoolSize();
                int currentMaxSize = getMaximumPoolSize();
                
                if (currentUtilization > targetUtilization) {
                    // 利用率过高,增加线程
                    int adjustment = calculateAdjustment(currentUtilization - targetUtilization);
                    int newCoreSize = Math.min(currentCoreSize + adjustment, currentMaxSize);
                    
                    if (newCoreSize > currentCoreSize) {
                        setCorePoolSize(newCoreSize);
                        consecutiveAdjustments.incrementAndGet();
                        logger.debug("增加核心线程数: {} -> {}, 当前利用率: {:.2f}",
                            currentCoreSize, newCoreSize, currentUtilization);
                    }
                    
                } else if (currentUtilization < targetUtilization * 0.6) {
                    // 利用率过低,减少线程
                    int adjustment = calculateAdjustment(targetUtilization - currentUtilization);
                    int newCoreSize = Math.max(currentCoreSize - adjustment, 1);
                    
                    if (newCoreSize < currentCoreSize) {
                        setCorePoolSize(newCoreSize);
                        consecutiveAdjustments.incrementAndGet();
                        logger.debug("减少核心线程数: {} -> {}, 当前利用率: {:.2f}",
                            currentCoreSize, newCoreSize, currentUtilization);
                    }
                } else {
                    // 利用率正常,重置连续调整计数
                    consecutiveAdjustments.set(0);
                }
                
            } catch (Exception e) {
                logger.warn("调整线程池大小失败", e);
            }
        }
        
        /**
         * 计算调整幅度
         */
        private int calculateAdjustment(double utilizationDiff) {
            // 基于利用率差异和连续调整次数计算调整幅度
            int baseAdjustment = (int) Math.ceil(utilizationDiff * 10);
            int consecutiveBonus = Math.min(consecutiveAdjustments.get() / 3, 2);
            
            return Math.max(minAdjustmentStep, 
                Math.min(maxAdjustmentStep, baseAdjustment + consecutiveBonus));
        }
        
        /**
         * 获取当前利用率
         */
        private double getCurrentUtilization() {
            int activeCount = getActiveCount();
            int poolSize = getPoolSize();
            
            if (poolSize == 0) {
                return 0.0;
            }
            
            return (double) activeCount / poolSize;
        }
    }
    
    /**
     * 智能拒绝策略
     */
    public static class SmartRejectedExecutionHandler implements RejectedExecutionHandler {
        
        private final String poolName;
        private final AtomicLong rejectedCount = new AtomicLong(0);
        
        public SmartRejectedExecutionHandler(String poolName) {
            this.poolName = poolName;
        }
        
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            long rejected = rejectedCount.incrementAndGet();
            
            // 记录拒绝统计
            logger.warn("线程池 {} 拒绝任务执行,累计拒绝: {} 次", poolName, rejected);
            
            // 尝试在调用线程中执行
            if (!executor.isShutdown()) {
                try {
                    r.run();
                    logger.debug("任务在调用线程中执行完成");
                } catch (Exception e) {
                    logger.error("调用线程执行任务失败", e);
                    throw new RejectedExecutionException("任务执行被拒绝且在调用线程中执行失败", e);
                }
            } else {
                throw new RejectedExecutionException("线程池已关闭,无法执行任务");
            }
        }
        
        public long getRejectedCount() {
            return rejectedCount.get();
        }
    }
    
    /**
     * 初始化默认线程池
     */
    private void initializeDefaultPools() {
        // 流程执行线程池
        getThreadPool("flow-execution");
        
        // 步骤执行线程池
        getThreadPool("step-execution");
        
        // 并行执行线程池
        getThreadPool("parallel-execution");
        
        // 异步通知线程池
        getThreadPool("async-notification");
        
        // 监控线程池
        getThreadPool("monitoring");
    }
    
    /**
     * 注册监控指标
     */
    private void registerMetrics(String poolName, ThreadPoolExecutor executor) {
        Tags tags = Tags.of("pool", poolName);
        
        Gauge.builder("threadpool.core.size")
            .tags(tags)
            .register(meterRegistry, executor, ThreadPoolExecutor::getCorePoolSize);
        
        Gauge.builder("threadpool.active.threads")
            .tags(tags)
            .register(meterRegistry, executor, ThreadPoolExecutor::getActiveCount);
        
        Gauge.builder("threadpool.pool.size")
            .tags(tags)
            .register(meterRegistry, executor, ThreadPoolExecutor::getPoolSize);
        
        Gauge.builder("threadpool.queue.size")
            .tags(tags)
            .register(meterRegistry, executor, e -> e.getQueue().size());
        
        Gauge.builder("threadpool.completed.tasks")
            .tags(tags)
            .register(meterRegistry, executor, ThreadPoolExecutor::getCompletedTaskCount);
        
        if (executor instanceof AdaptiveThreadPoolExecutor) {
            SmartRejectedExecutionHandler handler = 
                (SmartRejectedExecutionHandler) executor.getRejectedExecutionHandler();
            
            Gauge.builder("threadpool.rejected.tasks")
                .tags(tags)
                .register(meterRegistry, handler, SmartRejectedExecutionHandler::getRejectedCount);
        }
    }
    
    /**
     * 启动监控
     */
    private void startMonitoring() {
        monitoringExecutor.scheduleAtFixedRate(this::logPoolStatistics, 1, 1, TimeUnit.MINUTES);
    }
    
    /**
     * 记录线程池统计信息
     */
    private void logPoolStatistics() {
        for (Map.Entry<String, ThreadPoolExecutor> entry : threadPools.entrySet()) {
            String poolName = entry.getKey();
            ThreadPoolExecutor executor = entry.getValue();
            
            logger.info("线程池统计 [{}]: 核心={}, 活跃={}, 池大小={}, 队列={}, 完成={}",
                poolName,
                executor.getCorePoolSize(),
                executor.getActiveCount(),
                executor.getPoolSize(),
                executor.getQueue().size(),
                executor.getCompletedTaskCount());
        }
    }
    
    /**
     * 关闭所有线程池
     */
    public void shutdown() {
        logger.info("开始关闭线程池管理器");
        
        // 关闭监控线程池
        monitoringExecutor.shutdown();
        
        // 关闭所有业务线程池
        for (Map.Entry<String, ThreadPoolExecutor> entry : threadPools.entrySet()) {
            String poolName = entry.getKey();
            ThreadPoolExecutor executor = entry.getValue();
            
            try {
                executor.shutdown();
                if (!executor.awaitTermination(30, TimeUnit.SECONDS)) {
                    logger.warn("线程池 {} 未能在30秒内正常关闭,强制关闭", poolName);
                    executor.shutdownNow();
                }
                logger.info("线程池 {} 已关闭", poolName);
            } catch (InterruptedException e) {
                logger.warn("等待线程池 {} 关闭时被中断", poolName);
                executor.shutdownNow();
                Thread.currentThread().interrupt();
            }
        }
        
        logger.info("线程池管理器关闭完成");
    }
}

💾 缓存优化

🎯 多级缓存系统

java 复制代码
/**
 * 多级缓存管理器
 */
@Component
public class MultiLevelCacheManager {
    
    private static final Logger logger = LoggerFactory.getLogger(MultiLevelCacheManager.class);
    
    private final Cache<String, Object> l1Cache; // L1: 本地缓存
    private final Cache<String, Object> l2Cache; // L2: 分布式缓存
    private final CacheProperties properties;
    private final MeterRegistry meterRegistry;
    
    // 缓存统计
    private final AtomicLong l1Hits = new AtomicLong(0);
    private final AtomicLong l1Misses = new AtomicLong(0);
    private final AtomicLong l2Hits = new AtomicLong(0);
    private final AtomicLong l2Misses = new AtomicLong(0);
    
    public MultiLevelCacheManager(CacheProperties properties, MeterRegistry meterRegistry) {
        this.properties = properties;
        this.meterRegistry = meterRegistry;
        
        // 初始化L1缓存(本地)
        this.l1Cache = Caffeine.newBuilder()
            .maximumSize(properties.getL1MaxSize())
            .expireAfterWrite(properties.getL1ExpireAfterWrite())
            .expireAfterAccess(properties.getL1ExpireAfterAccess())
            .recordStats()
            .removalListener(this::onL1Removal)
            .build();
        
        // 初始化L2缓存(分布式)
        this.l2Cache = createL2Cache();
        
        // 注册监控指标
        registerMetrics();
        
        logger.info("多级缓存管理器初始化完成");
    }
    
    /**
     * 获取缓存值
     */
    @SuppressWarnings("unchecked")
    public <T> Optional<T> get(String key, Class<T> type) {
        // 先查L1缓存
        Object value = l1Cache.getIfPresent(key);
        if (value != null) {
            l1Hits.incrementAndGet();
            logger.debug("L1缓存命中: {}", key);
            return Optional.of((T) value);
        }
        l1Misses.incrementAndGet();
        
        // 再查L2缓存
        value = l2Cache.getIfPresent(key);
        if (value != null) {
            l2Hits.incrementAndGet();
            // 回填L1缓存
            l1Cache.put(key, value);
            logger.debug("L2缓存命中并回填L1: {}", key);
            return Optional.of((T) value);
        }
        l2Misses.incrementAndGet();
        
        logger.debug("缓存未命中: {}", key);
        return Optional.empty();
    }
    
    /**
     * 设置缓存值
     */
    public void put(String key, Object value) {
        put(key, value, properties.getDefaultTtl());
    }
    
    /**
     * 设置缓存值(带TTL)
     */
    public void put(String key, Object value, Duration ttl) {
        // 同时写入L1和L2缓存
        l1Cache.put(key, value);
        l2Cache.put(key, value);
        
        logger.debug("缓存已设置: {}, TTL: {}", key, ttl);
    }
    
    /**
     * 删除缓存
     */
    public void evict(String key) {
        l1Cache.invalidate(key);
        l2Cache.invalidate(key);
        
        logger.debug("缓存已删除: {}", key);
    }
    
    /**
     * 批量删除缓存
     */
    public void evictAll(Collection<String> keys) {
        l1Cache.invalidateAll(keys);
        l2Cache.invalidateAll(keys);
        
        logger.debug("批量删除缓存: {} 个key", keys.size());
    }
    
    /**
     * 清空所有缓存
     */
    public void clear() {
        l1Cache.invalidateAll();
        l2Cache.invalidateAll();
        
        logger.info("所有缓存已清空");
    }
    
    /**
     * 获取或计算缓存值
     */
    public <T> T getOrCompute(String key, Class<T> type, Supplier<T> supplier) {
        return getOrCompute(key, type, supplier, properties.getDefaultTtl());
    }
    
    /**
     * 获取或计算缓存值(带TTL)
     */
    @SuppressWarnings("unchecked")
    public <T> T getOrCompute(String key, Class<T> type, Supplier<T> supplier, Duration ttl) {
        // 先尝试从缓存获取
        Optional<T> cached = get(key, type);
        if (cached.isPresent()) {
            return cached.get();
        }
        
        // 缓存未命中,计算值
        T value = supplier.get();
        if (value != null) {
            put(key, value, ttl);
        }
        
        return value;
    }
    
    /**
     * 创建L2缓存
     */
    private Cache<String, Object> createL2Cache() {
        if (properties.isRedisEnabled()) {
            return new RedisCacheAdapter(properties);
        } else {
            // 如果没有Redis,使用本地缓存作为L2
            return Caffeine.newBuilder()
                .maximumSize(properties.getL2MaxSize())
                .expireAfterWrite(properties.getL2ExpireAfterWrite())
                .recordStats()
                .build();
        }
    }
    
    /**
     * L1缓存移除监听器
     */
    private void onL1Removal(String key, Object value, RemovalCause cause) {
        logger.debug("L1缓存移除: key={}, cause={}", key, cause);
    }
    
    /**
     * 注册监控指标
     */
    private void registerMetrics() {
        // L1缓存指标
        Gauge.builder("cache.l1.size")
            .register(meterRegistry, l1Cache, cache -> cache.estimatedSize());
        
        Gauge.builder("cache.l1.hit.rate")
            .register(meterRegistry, this, manager -> {
                long hits = manager.l1Hits.get();
                long total = hits + manager.l1Misses.get();
                return total == 0 ? 0.0 : (double) hits / total;
            });
        
        // L2缓存指标
        Gauge.builder("cache.l2.size")
            .register(meterRegistry, l2Cache, cache -> cache.estimatedSize());
        
        Gauge.builder("cache.l2.hit.rate")
            .register(meterRegistry, this, manager -> {
                long hits = manager.l2Hits.get();
                long total = hits + manager.l2Misses.get();
                return total == 0 ? 0.0 : (double) hits / total;
            });
        
        // 总体命中率
        Gauge.builder("cache.overall.hit.rate")
            .register(meterRegistry, this, manager -> {
                long hits = manager.l1Hits.get() + manager.l2Hits.get();
                long total = hits + manager.l1Misses.get() + manager.l2Misses.get();
                return total == 0 ? 0.0 : (double) hits / total;
            });
    }
    
    /**
     * 获取缓存统计信息
     */
    public CacheStatistics getStatistics() {
        return CacheStatistics.builder()
            .l1Size(l1Cache.estimatedSize())
            .l1Hits(l1Hits.get())
            .l1Misses(l1Misses.get())
            .l1HitRate(calculateHitRate(l1Hits.get(), l1Misses.get()))
            .l2Size(l2Cache.estimatedSize())
            .l2Hits(l2Hits.get())
            .l2Misses(l2Misses.get())
            .l2HitRate(calculateHitRate(l2Hits.get(), l2Misses.get()))
            .overallHitRate(calculateHitRate(
                l1Hits.get() + l2Hits.get(),
                l1Misses.get() + l2Misses.get()))
            .build();
    }
    
    private double calculateHitRate(long hits, long misses) {
        long total = hits + misses;
        return total == 0 ? 0.0 : (double) hits / total;
    }
}

/**
 * 智能缓存预热器
 */
@Component
public class CacheWarmer {
    
    private static final Logger logger = LoggerFactory.getLogger(CacheWarmer.class);
    
    private final MultiLevelCacheManager cacheManager;
    private final FlowDefinitionService flowDefinitionService;
    private final ThreadPoolExecutor warmupExecutor;
    
    public CacheWarmer(MultiLevelCacheManager cacheManager,
                      FlowDefinitionService flowDefinitionService,
                      SmartThreadPoolManager threadPoolManager) {
        this.cacheManager = cacheManager;
        this.flowDefinitionService = flowDefinitionService;
        this.warmupExecutor = threadPoolManager.getThreadPool("cache-warmup");
    }
    
    /**
     * 应用启动时预热缓存
     */
    @EventListener
    public void onApplicationReady(ApplicationReadyEvent event) {
        logger.info("开始缓存预热");
        
        CompletableFuture.runAsync(this::warmupFlowDefinitions, warmupExecutor)
            .thenRun(this::warmupCommonData)
            .thenRun(() -> logger.info("缓存预热完成"))
            .exceptionally(throwable -> {
                logger.error("缓存预热失败", throwable);
                return null;
            });
    }
    
    /**
     * 预热流程定义
     */
    private void warmupFlowDefinitions() {
        try {
            List<String> flowIds = flowDefinitionService.getAllFlowIds();
            
            for (String flowId : flowIds) {
                try {
                    FlowDefinition definition = flowDefinitionService.getFlowDefinition(flowId);
                    String cacheKey = "flow:definition:" + flowId;
                    cacheManager.put(cacheKey, definition);
                    
                    logger.debug("预热流程定义缓存: {}", flowId);
                } catch (Exception e) {
                    logger.warn("预热流程定义失败: {}", flowId, e);
                }
            }
            
            logger.info("流程定义缓存预热完成,共 {} 个流程", flowIds.size());
            
        } catch (Exception e) {
            logger.error("预热流程定义缓存失败", e);
        }
    }
    
    /**
     * 预热通用数据
     */
    private void warmupCommonData() {
        try {
            // 预热系统配置
            warmupSystemConfigurations();
            
            // 预热用户权限
            warmupUserPermissions();
            
            // 预热常用表达式
            warmupCommonExpressions();
            
            logger.info("通用数据缓存预热完成");
            
        } catch (Exception e) {
            logger.error("预热通用数据缓存失败", e);
        }
    }
    
    private void warmupSystemConfigurations() {
        // 实现系统配置预热逻辑
    }
    
    private void warmupUserPermissions() {
        // 实现用户权限预热逻辑
    }
    
    private void warmupCommonExpressions() {
        // 实现常用表达式预热逻辑
    }
}

🔧 插件扩展系统

🎯 SPI机制实现

java 复制代码
/**
 * 插件管理器
 */
@Component
public class PluginManager {
    
    private static final Logger logger = LoggerFactory.getLogger(PluginManager.class);
    
    private final Map<Class<?>, List<Object>> plugins = new ConcurrentHashMap<>();
    private final Map<String, PluginMetadata> pluginMetadata = new ConcurrentHashMap<>();
    private final PluginClassLoader pluginClassLoader;
    private final PluginProperties properties;
    
    public PluginManager(PluginProperties properties) {
        this.properties = properties;
        this.pluginClassLoader = new PluginClassLoader(getClass().getClassLoader());
        
        // 扫描并加载插件
        scanAndLoadPlugins();
    }
    
    /**
     * 获取指定类型的所有插件
     */
    @SuppressWarnings("unchecked")
    public <T> List<T> getPlugins(Class<T> type) {
        return (List<T>) plugins.getOrDefault(type, Collections.emptyList());
    }
    
    /**
     * 获取指定类型的第一个插件
     */
    public <T> Optional<T> getPlugin(Class<T> type) {
        List<T> pluginList = getPlugins(type);
        return pluginList.isEmpty() ? Optional.empty() : Optional.of(pluginList.get(0));
    }
    
    /**
     * 获取指定名称的插件
     */
    @SuppressWarnings("unchecked")
    public <T> Optional<T> getPlugin(Class<T> type, String name) {
        return getPlugins(type).stream()
            .filter(plugin -> {
                PluginMetadata metadata = pluginMetadata.get(plugin.getClass().getName());
                return metadata != null && name.equals(metadata.getName());
            })
            .findFirst();
    }
    
    /**
     * 注册插件
     */
    public void registerPlugin(Object plugin) {
        Class<?> pluginClass = plugin.getClass();
        
        // 获取插件实现的接口
        for (Class<?> interfaceClass : pluginClass.getInterfaces()) {
            if (isPluginInterface(interfaceClass)) {
                plugins.computeIfAbsent(interfaceClass, k -> new CopyOnWriteArrayList<>())
                       .add(plugin);
                
                logger.info("注册插件: {} -> {}", interfaceClass.getSimpleName(), pluginClass.getSimpleName());
            }
        }
        
        // 存储插件元数据
        PluginMetadata metadata = extractPluginMetadata(pluginClass);
        pluginMetadata.put(pluginClass.getName(), metadata);
    }
    
    /**
     * 卸载插件
     */
    public void unregisterPlugin(Object plugin) {
        Class<?> pluginClass = plugin.getClass();
        
        for (Class<?> interfaceClass : pluginClass.getInterfaces()) {
            if (isPluginInterface(interfaceClass)) {
                List<Object> pluginList = plugins.get(interfaceClass);
                if (pluginList != null) {
                    pluginList.remove(plugin);
                    logger.info("卸载插件: {} -> {}", interfaceClass.getSimpleName(), pluginClass.getSimpleName());
                }
            }
        }
        
        pluginMetadata.remove(pluginClass.getName());
    }
    
    /**
     * 热加载插件
     */
    public void hotLoadPlugin(String pluginPath) {
        try {
            // 加载插件JAR文件
            URL pluginUrl = new File(pluginPath).toURI().toURL();
            pluginClassLoader.addURL(pluginUrl);
            
            // 扫描插件类
            scanPluginJar(pluginPath);
            
            logger.info("热加载插件成功: {}", pluginPath);
            
        } catch (Exception e) {
            logger.error("热加载插件失败: {}", pluginPath, e);
            throw new PluginLoadException("热加载插件失败", e);
        }
    }
    
    /**
     * 扫描并加载插件
     */
    private void scanAndLoadPlugins() {
        try {
            // 扫描类路径下的插件
            scanClasspathPlugins();
            
            // 扫描插件目录
            if (properties.getPluginDirectory() != null) {
                scanPluginDirectory(properties.getPluginDirectory());
            }
            
            logger.info("插件扫描完成,共加载 {} 个插件类型", plugins.size());
            
        } catch (Exception e) {
            logger.error("扫描插件失败", e);
        }
    }
    
    /**
     * 扫描类路径插件
     */
    private void scanClasspathPlugins() {
        ServiceLoader.load(StepExecutor.class).forEach(this::registerPlugin);
        ServiceLoader.load(ExpressionEvaluator.class).forEach(this::registerPlugin);
        ServiceLoader.load(FlowExecutionListener.class).forEach(this::registerPlugin);
        ServiceLoader.load(AlertNotifier.class).forEach(this::registerPlugin);
    }
    
    /**
     * 扫描插件目录
     */
    private void scanPluginDirectory(String directory) {
        File pluginDir = new File(directory);
        if (!pluginDir.exists() || !pluginDir.isDirectory()) {
            logger.warn("插件目录不存在: {}", directory);
            return;
        }
        
        File[] jarFiles = pluginDir.listFiles((dir, name) -> name.endsWith(".jar"));
        if (jarFiles != null) {
            for (File jarFile : jarFiles) {
                try {
                    scanPluginJar(jarFile.getAbsolutePath());
                } catch (Exception e) {
                    logger.error("扫描插件JAR失败: {}", jarFile.getAbsolutePath(), e);
                }
            }
        }
    }
    
    /**
     * 扫描插件JAR文件
     */
    private void scanPluginJar(String jarPath) throws Exception {
        try (JarFile jarFile = new JarFile(jarPath)) {
            Enumeration<JarEntry> entries = jarFile.entries();
            
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                String entryName = entry.getName();
                
                if (entryName.endsWith(".class") && !entryName.contains("$")) {
                    String className = entryName.replace('/', '.').replace(".class", "");
                    
                    try {
                        Class<?> clazz = pluginClassLoader.loadClass(className);
                        
                        if (isPluginClass(clazz)) {
                            Object plugin = clazz.getDeclaredConstructor().newInstance();
                            registerPlugin(plugin);
                        }
                        
                    } catch (Exception e) {
                        logger.debug("跳过类: {} - {}", className, e.getMessage());
                    }
                }
            }
        }
    }
    
    /**
     * 检查是否为插件接口
     */
    private boolean isPluginInterface(Class<?> clazz) {
        return clazz.isAnnotationPresent(PluginInterface.class) ||
               StepExecutor.class.isAssignableFrom(clazz) ||
               ExpressionEvaluator.class.isAssignableFrom(clazz) ||
               FlowExecutionListener.class.isAssignableFrom(clazz) ||
               AlertNotifier.class.isAssignableFrom(clazz);
    }
    
    /**
     * 检查是否为插件类
     */
    private boolean isPluginClass(Class<?> clazz) {
        if (clazz.isInterface() || clazz.isAbstract()) {
            return false;
        }
        
        for (Class<?> interfaceClass : clazz.getInterfaces()) {
            if (isPluginInterface(interfaceClass)) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * 提取插件元数据
     */
    private PluginMetadata extractPluginMetadata(Class<?> pluginClass) {
        Plugin annotation = pluginClass.getAnnotation(Plugin.class);
        
        if (annotation != null) {
            return PluginMetadata.builder()
                .name(annotation.name())
                .version(annotation.version())
                .description(annotation.description())
                .author(annotation.author())
                .className(pluginClass.getName())
                .build();
        } else {
            return PluginMetadata.builder()
                .name(pluginClass.getSimpleName())
                .version("1.0.0")
                .description("")
                .author("Unknown")
                .className(pluginClass.getName())
                .build();
        }
    }
    
    /**
     * 获取所有插件信息
     */
    public List<PluginInfo> getAllPluginInfo() {
        return pluginMetadata.values().stream()
            .map(metadata -> PluginInfo.builder()
                .name(metadata.getName())
                .version(metadata.getVersion())
                .description(metadata.getDescription())
                .author(metadata.getAuthor())
                .className(metadata.getClassName())
                .status("ACTIVE")
                .loadTime(Instant.now())
                .build())
            .collect(Collectors.toList());
    }
}

/**
 * 插件接口标记注解
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PluginInterface {
    String value() default "";
}

/**
 * 插件注解
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Plugin {
    String name();
    String version() default "1.0.0";
    String description() default "";
    String author() default "";
    String[] dependencies() default {};
}

/**
 * 自定义步骤执行器示例
 */
@Plugin(
    name = "HttpStepExecutor",
    version = "1.0.0",
    description = "HTTP请求步骤执行器",
    author = "杨杨杨大侠"
)
public class HttpStepExecutor extends AbstractStepExecutor {
    
    private static final Logger logger = LoggerFactory.getLogger(HttpStepExecutor.class);
    
    @Override
    public StepType getSupportedType() {
        return StepType.HTTP;
    }
    
    @Override
    protected StepExecutionResult doExecute(StepDefinition step, FlowContext context) {
        try {
            // 获取HTTP配置
            String url = step.getParameter("url", String.class);
            String method = step.getParameter("method", String.class, "GET");
            Map<String, String> headers = step.getParameter("headers", Map.class, Collections.emptyMap());
            Object body = step.getParameter("body");
            
            // 执行HTTP请求
            HttpResponse response = executeHttpRequest(url, method, headers, body);
            
            // 返回结果
            return StepExecutionResult.success()
                .output("response", response)
                .output("statusCode", response.getStatusCode())
                .output("body", response.getBody())
                .build();
            
        } catch (Exception e) {
            logger.error("HTTP步骤执行失败", e);
            return StepExecutionResult.failure("HTTP请求失败: " + e.getMessage());
        }
    }
    
    private HttpResponse executeHttpRequest(String url, String method, 
                                          Map<String, String> headers, Object body) {
        // 实现HTTP请求逻辑
        // 这里可以使用OkHttp、Apache HttpClient等
        return new HttpResponse(200, "Success", Collections.emptyMap());
    }
    
    @Data
    @AllArgsConstructor
    public static class HttpResponse {
        private int statusCode;
        private String body;
        private Map<String, String> headers;
    }
}

📊 性能监控和调优

🎯 自动性能调优

java 复制代码
/**
 * 自动性能调优器
 */
@Component
public class AutoPerformanceTuner {
    
    private static final Logger logger = LoggerFactory.getLogger(AutoPerformanceTuner.class);
    
    private final SmartThreadPoolManager threadPoolManager;
    private final MultiLevelCacheManager cacheManager;
    private final MeterRegistry meterRegistry;
    private final ScheduledExecutorService scheduler;
    
    private final Map<String, PerformanceBaseline> baselines = new ConcurrentHashMap<>();
    private final AtomicBoolean tuningEnabled = new AtomicBoolean(true);
    
    public AutoPerformanceTuner(SmartThreadPoolManager threadPoolManager,
                              MultiLevelCacheManager cacheManager,
                              MeterRegistry meterRegistry) {
        this.threadPoolManager = threadPoolManager;
        this.cacheManager = cacheManager;
        this.meterRegistry = meterRegistry;
        this.scheduler = Executors.newScheduledThreadPool(1);
        
        // 启动性能监控和调优
        startPerformanceMonitoring();
    }
    
    /**
     * 启动性能监控
     */
    private void startPerformanceMonitoring() {
        // 每5分钟收集性能指标
        scheduler.scheduleAtFixedRate(this::collectPerformanceMetrics, 1, 5, TimeUnit.MINUTES);
        
        // 每15分钟进行性能分析和调优
        scheduler.scheduleAtFixedRate(this::analyzeAndTune, 5, 15, TimeUnit.MINUTES);
        
        logger.info("自动性能调优器已启动");
    }
    
    /**
     * 收集性能指标
     */
    private void collectPerformanceMetrics() {
        try {
            PerformanceMetrics metrics = collectCurrentMetrics();
            
            // 更新基线
            updateBaseline("current", metrics);
            
            // 检查性能异常
            checkPerformanceAnomalies(metrics);
            
            logger.debug("性能指标收集完成: {}", metrics);
            
        } catch (Exception e) {
            logger.error("收集性能指标失败", e);
        }
    }
    
    /**
     * 分析和调优
     */
    private void analyzeAndTune() {
        if (!tuningEnabled.get()) {
            return;
        }
        
        try {
            PerformanceMetrics current = collectCurrentMetrics();
            PerformanceBaseline baseline = baselines.get("current");
            
            if (baseline != null) {
                // 分析性能趋势
                PerformanceAnalysis analysis = analyzePerformance(current, baseline);
                
                // 执行调优建议
                executeTuningRecommendations(analysis);
                
                logger.info("性能分析和调优完成: {}", analysis.getSummary());
            }
            
        } catch (Exception e) {
            logger.error("性能分析和调优失败", e);
        }
    }
    
    /**
     * 收集当前性能指标
     */
    private PerformanceMetrics collectCurrentMetrics() {
        // JVM指标
        MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemory = memoryBean.getHeapMemoryUsage();
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        
        // 缓存指标
        CacheStatistics cacheStats = cacheManager.getStatistics();
        
        // 线程池指标
        Map<String, ThreadPoolStatistics> threadPoolStats = collectThreadPoolStatistics();
        
        // 业务指标
        double flowThroughput = getFlowThroughput();
        double averageResponseTime = getAverageResponseTime();
        double errorRate = getErrorRate();
        
        return PerformanceMetrics.builder()
            .timestamp(Instant.now())
            .heapUsage((double) heapMemory.getUsed() / heapMemory.getMax())
            .threadCount(threadBean.getThreadCount())
            .cacheHitRate(cacheStats.getOverallHitRate())
            .threadPoolStats(threadPoolStats)
            .flowThroughput(flowThroughput)
            .averageResponseTime(averageResponseTime)
            .errorRate(errorRate)
            .build();
    }
    
    /**
     * 分析性能
     */
    private PerformanceAnalysis analyzePerformance(PerformanceMetrics current, 
                                                  PerformanceBaseline baseline) {
        List<String> issues = new ArrayList<>();
        List<TuningRecommendation> recommendations = new ArrayList<>();
        
        // 分析内存使用
        if (current.getHeapUsage() > 0.8) {
            issues.add("堆内存使用率过高: " + String.format("%.2f%%", current.getHeapUsage() * 100));
            recommendations.add(new TuningRecommendation(
                TuningType.MEMORY,
                "增加堆内存或优化内存使用",
                TuningPriority.HIGH
            ));
        }
        
        // 分析线程池
        for (Map.Entry<String, ThreadPoolStatistics> entry : current.getThreadPoolStats().entrySet()) {
            String poolName = entry.getKey();
            ThreadPoolStatistics stats = entry.getValue();
            
            if (stats.getUtilization() > 0.9) {
                issues.add(String.format("线程池 %s 利用率过高: %.2f%%", poolName, stats.getUtilization() * 100));
                recommendations.add(new TuningRecommendation(
                    TuningType.THREAD_POOL,
                    "增加线程池 " + poolName + " 的大小",
                    TuningPriority.MEDIUM
                ));
            }
            
            if (stats.getQueueSize() > stats.getMaxQueueSize() * 0.8) {
                issues.add(String.format("线程池 %s 队列积压严重", poolName));
                recommendations.add(new TuningRecommendation(
                    TuningType.THREAD_POOL,
                    "增加线程池 " + poolName + " 的队列大小或处理能力",
                    TuningPriority.HIGH
                ));
            }
        }
        
        // 分析缓存命中率
        if (current.getCacheHitRate() < 0.7) {
            issues.add(String.format("缓存命中率过低: %.2f%%", current.getCacheHitRate() * 100));
            recommendations.add(new TuningRecommendation(
                TuningType.CACHE,
                "优化缓存策略或增加缓存大小",
                TuningPriority.MEDIUM
            ));
        }
        
        // 分析响应时间
        double baselineResponseTime = baseline.getAverageMetric("averageResponseTime");
        if (current.getAverageResponseTime() > baselineResponseTime * 1.5) {
            issues.add("平均响应时间显著增加");
            recommendations.add(new TuningRecommendation(
                TuningType.PERFORMANCE,
                "检查系统瓶颈,优化慢查询或增加资源",
                TuningPriority.HIGH
            ));
        }
        
        return PerformanceAnalysis.builder()
            .timestamp(Instant.now())
            .issues(issues)
            .recommendations(recommendations)
            .summary(generateAnalysisSummary(issues, recommendations))
            .build();
    }
    
    /**
     * 执行调优建议
     */
    private void executeTuningRecommendations(PerformanceAnalysis analysis) {
        for (TuningRecommendation recommendation : analysis.getRecommendations()) {
            try {
                switch (recommendation.getType()) {
                    case THREAD_POOL:
                        tuneThreadPool(recommendation);
                        break;
                    case CACHE:
                        tuneCache(recommendation);
                        break;
                    case MEMORY:
                        tuneMemory(recommendation);
                        break;
                    case PERFORMANCE:
                        tunePerformance(recommendation);
                        break;
                }
                
                logger.info("执行调优建议: {}", recommendation.getDescription());
                
            } catch (Exception e) {
                logger.error("执行调优建议失败: {}", recommendation.getDescription(), e);
            }
        }
    }
    
    private void tuneThreadPool(TuningRecommendation recommendation) {
        // 实现线程池调优逻辑
        // 例如:动态调整线程池大小
    }
    
    private void tuneCache(TuningRecommendation recommendation) {
        // 实现缓存调优逻辑
        // 例如:调整缓存大小或过期策略
    }
    
    private void tuneMemory(TuningRecommendation recommendation) {
        // 实现内存调优逻辑
        // 例如:触发GC或清理缓存
        System.gc();
    }
    
    private void tunePerformance(TuningRecommendation recommendation) {
        // 实现性能调优逻辑
        // 例如:调整批处理大小或并发度
    }
    
    // 其他辅助方法...
    
    private Map<String, ThreadPoolStatistics> collectThreadPoolStatistics() {
        // 实现线程池统计收集
        return new HashMap<>();
    }
    
    private double getFlowThroughput() {
        // 实现流程吞吐量计算
        return 0.0;
    }
    
    private double getAverageResponseTime() {
        // 实现平均响应时间计算
        return 0.0;
    }
    
    private double getErrorRate() {
        // 实现错误率计算
        return 0.0;
    }
    
    private void updateBaseline(String name, PerformanceMetrics metrics) {
        // 实现基线更新逻辑
    }
    
    private void checkPerformanceAnomalies(PerformanceMetrics metrics) {
        // 实现性能异常检查
    }
    
    private String generateAnalysisSummary(List<String> issues, List<TuningRecommendation> recommendations) {
        return String.format("发现 %d 个性能问题,生成 %d 个调优建议", issues.size(), recommendations.size());
    }
}

🎯 设计亮点

⚡ 智能线程池

  • 自适应调整: 根据负载自动调整线程池大小
  • 智能拒绝策略: 优雅处理任务拒绝情况
  • 全面监控: 详细的线程池运行指标
  • 资源隔离: 不同类型任务使用独立线程池

💾 多级缓存

  • L1/L2缓存: 本地+分布式的多级缓存架构
  • 智能预热: 应用启动时自动预热热点数据
  • 命中率优化: 实时监控和优化缓存命中率
  • 内存管理: 防止缓存导致的内存溢出

🔧 插件系统

  • SPI机制: 标准的服务提供者接口
  • 热插拔: 运行时动态加载和卸载插件
  • 版本管理: 支持插件版本控制和依赖管理
  • 安全隔离: 插件运行在独立的类加载器中

📊 自动调优

  • 性能监控: 全方位的性能指标收集
  • 智能分析: 基于历史数据的性能趋势分析
  • 自动调优: 根据分析结果自动执行调优策略
  • 异常检测: 及时发现和处理性能异常

📝 本章小结

本章实现了全面的性能优化和扩展能力,具备以下特性:

智能线程池管理 : 自适应调整和全面监控

多级缓存系统 : 高效的数据缓存和预热机制

插件扩展架构 : 灵活的SPI机制和热插拔能力

自动性能调优 : 智能的性能分析和调优策略

全面监控体系: 详细的性能指标和异常检测

至此,我们的《从 0-1 搭建一个编排框架》系列教程全部完成!🎉

🚀 系列总结

通过这10篇文章,我们从零开始构建了一个功能完整、性能优异的流程编排框架:

  1. 项目初始化与架构设计 - 奠定了坚实的架构基础
  2. 核心模型设计 - 定义了清晰的领域模型
  3. 流程引擎实现 - 构建了可靠的执行引擎
  4. 步骤执行器设计 - 实现了灵活的执行器架构
  5. 上下文管理机制 - 提供了安全的数据共享
  6. 表达式引擎实现 - 支持了动态的条件判断
  7. YAML配置解析器 - 实现了友好的配置方式
  8. Spring Boot集成 - 提供了开箱即用的体验
  9. 监控和日志系统 - 构建了完整的可观测性
  10. 性能优化和扩展 - 实现了企业级的性能和扩展能力

这个框架具备了生产环境所需的所有特性:

🏗️ 架构特性:

  • 模块化设计,职责清晰
  • 事件驱动架构,松耦合
  • 插件化扩展,高度灵活
  • 多级缓存,性能优异

🔧 功能特性:

  • 支持多种步骤类型(脚本、服务、条件、并行、循环)
  • 强大的表达式引擎
  • 灵活的YAML配置
  • 完整的监控和告警

🚀 性能特性:

  • 智能线程池管理
  • 自动性能调优
  • 多级缓存优化
  • 资源合理利用

🔒 企业特性:

  • 完整的日志审计
  • 安全的权限控制
  • 高可用架构
  • 水平扩展支持

希望这个系列能够帮助你深入理解流程编排框架的设计和实现!

如果你有任何问题或建议,欢迎交流讨论! 💬


作者 : 杨杨杨大侠
许可证 : MIT License
项目地址 : GitHub Repository

相关推荐
SimonKing20 分钟前
Spring Boot Admin:一站式监控微服务,这个运维神器真香!
java·后端·程序员
uhakadotcom29 分钟前
如何安装和使用开源的Meilisearch
后端·面试·github
高松燈32 分钟前
自动拆箱 导致的空指针问题复盘
后端
IT_陈寒1 小时前
Java性能优化实战:5个立竿见影的技巧让你的应用提速50%
前端·人工智能·后端
zkmall2 小时前
ZKmall开源商城多端兼容实践:鸿蒙、iOS、安卓全平台适配的技术路径
ios·开源·harmonyos
陈随易2 小时前
10年老前端,分享20+严选技术栈
前端·后端·程序员
汪子熙2 小时前
计算机世界里的 blob:从数据库 BLOB 到 Git、Web API 与云存储的二进制宇宙
后端
芝士加2 小时前
还在用html2canvas?介绍一个比它快100倍的截图神器!
前端·javascript·开源
鞋尖的灰尘3 小时前
springboot-事务
java·后端
元元的飞3 小时前
6、Spring AI Alibaba MCP结合Nacos自动注册与发现
后端·ai编程