服务化架构中微内核&插件化思想的应用

服务化架构中微内核&插件化思想的应用

前言

服务化架构作为现代分布式系统设计的核心范式,其成功实践深度依赖于微内核(Microkernel)和插件化(Plugin-based)的架构思想。通过将复杂的单体应用分解为松耦合的微服务集群,服务化架构实现了系统的高可扩展性、高可用性和技术栈的多样化。本文将深入探讨服务化架构中微内核思想的应用实践,以及如何构建高效的插件化服务生态系统。

一、服务化架构中的微内核设计

1.1 微内核架构模式

在服务化架构中,微内核模式将整个系统划分为核心服务层和业务服务层:

  • 服务内核(Service Kernel): 提供服务注册发现、负载均衡、配置管理、监控治理等基础设施能力
  • 业务服务(Business Services): 独立部署的业务微服务,通过标准协议与服务内核交互

1.2 服务化微内核架构图

graph TD subgraph "服务化微内核架构" A[服务网关
API Gateway] --> B[服务注册中心
Service Registry] A --> C[负载均衡器
Load Balancer] A --> D[配置中心
Config Center] A --> E[监控中心
Monitor Center] B --> F[服务发现
Discovery] C --> G[路由策略
Routing] D --> H[动态配置
Dynamic Config] E --> I[链路追踪
Tracing] subgraph "业务服务层" J[用户服务
User Service] K[订单服务
Order Service] L[商品服务
Product Service] M[支付服务
Payment Service] end A --> J A --> K A --> L A --> M subgraph "数据层" N[用户DB
User DB] O[订单DB
Order DB] P[商品DB
Product DB] Q[支付DB
Payment DB] end J --> N K --> O L --> P M --> Q subgraph "中间件层" R[消息队列
Message Queue] S[缓存集群
Cache Cluster] T[任务调度
Task Scheduler] end J --> R K --> R L --> S M --> T end style A fill:#FF6B6B style B fill:#4ECDC4 style C fill:#45B7D1 style D fill:#96CEB4

1.3 服务内核实现

服务内核是整个服务化系统的控制中心,负责服务的生命周期管理:

javascript 复制代码
// 服务化架构微内核
class ServiceKernel {
  constructor() {
    this.services = new Map()
    this.registry = new ServiceRegistry()
    this.loadBalancer = new LoadBalancer()
    this.configCenter = new ConfigCenter()
    this.monitor = new MonitorCenter()
    this.gateway = new APIGateway()
    this.pluginManager = new PluginManager()
    
    this.initializeKernel()
  }

  // 初始化服务内核
  async initializeKernel() {
    // 启动基础组件
    await this.registry.start()
    await this.configCenter.start()
    await this.monitor.start()
    await this.gateway.start()
    
    // 加载核心插件
    await this.loadCorePlugins()
    
    // 设置健康检查
    this.setupHealthCheck()
    
    // 启动服务发现
    this.startServiceDiscovery()
    
    console.log('Service kernel initialized successfully')
  }

  // 注册服务
  async registerService(serviceConfig) {
    const {
      name,
      version,
      host,
      port,
      protocol = 'http',
      healthCheck = '/health',
      metadata = {}
    } = serviceConfig

    // 验证服务配置
    this.validateServiceConfig(serviceConfig)

    // 创建服务实例
    const serviceInstance = new ServiceInstance({
      name,
      version,
      host,
      port,
      protocol,
      healthCheck,
      metadata,
      kernel: this
    })

    // 注册到服务注册中心
    await this.registry.register(serviceInstance)

    // 添加到本地服务列表
    const serviceKey = `${name}:${version}`
    if (!this.services.has(serviceKey)) {
      this.services.set(serviceKey, new Set())
    }
    this.services.get(serviceKey).add(serviceInstance)

    // 配置负载均衡
    this.loadBalancer.addService(serviceInstance)

    // 启动健康检查
    this.monitor.startHealthCheck(serviceInstance)

    console.log(`Service registered: ${name}:${version} at ${host}:${port}`)
    return serviceInstance
  }

  // 注销服务
  async unregisterService(serviceId) {
    const serviceInstance = this.findServiceInstance(serviceId)
    if (!serviceInstance) {
      throw new Error(`Service instance not found: ${serviceId}`)
    }

    try {
      // 从注册中心移除
      await this.registry.unregister(serviceInstance)

      // 从负载均衡器移除
      this.loadBalancer.removeService(serviceInstance)

      // 停止健康检查
      this.monitor.stopHealthCheck(serviceInstance)

      // 从本地列表移除
      const serviceKey = `${serviceInstance.name}:${serviceInstance.version}`
      const instances = this.services.get(serviceKey)
      if (instances) {
        instances.delete(serviceInstance)
        if (instances.size === 0) {
          this.services.delete(serviceKey)
        }
      }

      console.log(`Service unregistered: ${serviceInstance.id}`)
    } catch (error) {
      console.error('Failed to unregister service:', error)
      throw error
    }
  }

  // 服务发现
  async discoverServices(serviceName, version = 'latest') {
    try {
      // 从注册中心查询服务
      const services = await this.registry.discover(serviceName, version)
      
      // 过滤健康的服务实例
      const healthyServices = services.filter(service => 
        this.monitor.isServiceHealthy(service.id)
      )

      // 应用负载均衡策略
      return this.loadBalancer.selectServices(healthyServices)
    } catch (error) {
      console.error(`Service discovery failed for ${serviceName}:`, error)
      throw error
    }
  }

  // 服务调用
  async invokeService(serviceName, method, params = {}, options = {}) {
    const {
      version = 'latest',
      timeout = 30000,
      retries = 3,
      circuit = true
    } = options

    try {
      // 服务发现
      const serviceInstances = await this.discoverServices(serviceName, version)
      if (serviceInstances.length === 0) {
        throw new Error(`No available instances for service: ${serviceName}`)
      }

      // 负载均衡选择实例
      const selectedInstance = this.loadBalancer.selectInstance(serviceInstances)

      // 构建请求
      const request = this.buildServiceRequest(selectedInstance, method, params, options)

      // 执行调用(支持熔断器)
      if (circuit) {
        return await this.circuitBreakerCall(selectedInstance, request, options)
      } else {
        return await this.directCall(selectedInstance, request, options)
      }
    } catch (error) {
      console.error(`Service invocation failed: ${serviceName}.${method}`, error)
      
      // 记录调用失败指标
      this.monitor.recordFailure(serviceName, method, error)
      
      throw error
    }
  }

  // 熔断器调用
  async circuitBreakerCall(serviceInstance, request, options) {
    const circuitBreaker = this.getCircuitBreaker(serviceInstance.id)
    
    return circuitBreaker.call(async () => {
      return await this.directCall(serviceInstance, request, options)
    })
  }

  // 直接调用
  async directCall(serviceInstance, request, options) {
    const startTime = Date.now()
    
    try {
      // 添加链路追踪信息
      request.headers = {
        ...request.headers,
        'X-Trace-Id': this.generateTraceId(),
        'X-Span-Id': this.generateSpanId(),
        'X-Service-Source': 'service-kernel'
      }

      // 发送请求
      const response = await this.httpClient.request(request)
      
      const duration = Date.now() - startTime
      
      // 记录成功指标
      this.monitor.recordSuccess(serviceInstance.name, request.method, duration)
      
      return response
    } catch (error) {
      const duration = Date.now() - startTime
      
      // 记录失败指标
      this.monitor.recordFailure(serviceInstance.name, request.method, duration, error)
      
      throw error
    }
  }

  // 构建服务请求
  buildServiceRequest(serviceInstance, method, params, options) {
    const baseUrl = `${serviceInstance.protocol}://${serviceInstance.host}:${serviceInstance.port}`
    
    return {
      method: 'POST',
      url: `${baseUrl}/api/${method}`,
      data: params,
      timeout: options.timeout || 30000,
      headers: {
        'Content-Type': 'application/json',
        'X-Service-Version': serviceInstance.version,
        ...options.headers
      }
    }
  }

  // 加载核心插件
  async loadCorePlugins() {
    const corePlugins = [
      'authentication-plugin',
      'rate-limiting-plugin', 
      'logging-plugin',
      'metrics-plugin',
      'circuit-breaker-plugin'
    ]

    for (const pluginName of corePlugins) {
      try {
        await this.pluginManager.loadPlugin(pluginName)
        console.log(`Core plugin loaded: ${pluginName}`)
      } catch (error) {
        console.error(`Failed to load core plugin ${pluginName}:`, error)
      }
    }
  }

  // 设置健康检查
  setupHealthCheck() {
    setInterval(async () => {
      for (const [serviceKey, instances] of this.services) {
        for (const instance of instances) {
          try {
            const isHealthy = await this.checkServiceHealth(instance)
            this.monitor.updateHealthStatus(instance.id, isHealthy)
          } catch (error) {
            console.error(`Health check failed for ${instance.id}:`, error)
            this.monitor.updateHealthStatus(instance.id, false)
          }
        }
      }
    }, 10000) // 每10秒检查一次
  }

  // 检查服务健康状态
  async checkServiceHealth(serviceInstance) {
    try {
      const healthUrl = `${serviceInstance.protocol}://${serviceInstance.host}:${serviceInstance.port}${serviceInstance.healthCheck}`
      const response = await this.httpClient.get(healthUrl, { timeout: 5000 })
      return response.status === 200
    } catch (error) {
      return false
    }
  }

  // 启动服务发现
  startServiceDiscovery() {
    // 监听服务注册事件
    this.registry.on('service-registered', (serviceInstance) => {
      console.log('New service registered:', serviceInstance.id)
      this.loadBalancer.addService(serviceInstance)
    })

    // 监听服务注销事件
    this.registry.on('service-unregistered', (serviceInstance) => {
      console.log('Service unregistered:', serviceInstance.id)
      this.loadBalancer.removeService(serviceInstance)
    })

    // 定期同步服务列表
    setInterval(async () => {
      try {
        await this.syncServiceRegistry()
      } catch (error) {
        console.error('Service registry sync failed:', error)
      }
    }, 30000) // 每30秒同步一次
  }

  // 同步服务注册表
  async syncServiceRegistry() {
    const registeredServices = await this.registry.listServices()
    const localServices = new Set()
    
    // 收集本地服务
    for (const instances of this.services.values()) {
      for (const instance of instances) {
        localServices.add(instance.id)
      }
    }

    // 添加新发现的服务
    for (const service of registeredServices) {
      if (!localServices.has(service.id)) {
        this.loadBalancer.addService(service)
      }
    }

    // 移除已注销的服务
    const registeredIds = new Set(registeredServices.map(s => s.id))
    for (const localId of localServices) {
      if (!registeredIds.has(localId)) {
        const instance = this.findServiceInstance(localId)
        if (instance) {
          this.loadBalancer.removeService(instance)
        }
      }
    }
  }

  // 查找服务实例
  findServiceInstance(serviceId) {
    for (const instances of this.services.values()) {
      for (const instance of instances) {
        if (instance.id === serviceId) {
          return instance
        }
      }
    }
    return null
  }

  // 获取熔断器
  getCircuitBreaker(serviceId) {
    return this.pluginManager.getPlugin('circuit-breaker')?.getBreaker(serviceId)
  }

  // 生成链路追踪ID
  generateTraceId() {
    return 'trace_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
  }

  // 生成Span ID
  generateSpanId() {
    return 'span_' + Math.random().toString(36).substr(2, 9)
  }

  // 验证服务配置
  validateServiceConfig(config) {
    const required = ['name', 'version', 'host', 'port']
    
    for (const field of required) {
      if (!config[field]) {
        throw new Error(`Service config missing required field: ${field}`)
      }
    }

    // 验证端口范围
    if (config.port < 1 || config.port > 65535) {
      throw new Error('Invalid port number')
    }

    // 验证协议
    if (config.protocol && !['http', 'https', 'grpc'].includes(config.protocol)) {
      throw new Error('Unsupported protocol')
    }
  }

  // 获取服务统计信息
  getServiceStats() {
    const stats = {
      totalServices: 0,
      totalInstances: 0,
      healthyInstances: 0,
      services: {}
    }

    for (const [serviceKey, instances] of this.services) {
      stats.totalServices++
      stats.totalInstances += instances.size
      
      let healthyCount = 0
      for (const instance of instances) {
        if (this.monitor.isServiceHealthy(instance.id)) {
          healthyCount++
        }
      }
      
      stats.healthyInstances += healthyCount
      stats.services[serviceKey] = {
        totalInstances: instances.size,
        healthyInstances: healthyCount
      }
    }

    return stats
  }

  // 关闭服务内核
  async shutdown() {
    console.log('Shutting down service kernel...')
    
    // 注销所有服务
    for (const instances of this.services.values()) {
      for (const instance of instances) {
        await this.unregisterService(instance.id)
      }
    }

    // 关闭基础组件
    await this.gateway.stop()
    await this.monitor.stop()
    await this.configCenter.stop()
    await this.registry.stop()
    
    console.log('Service kernel shutdown complete')
  }
}

二、微服务插件系统实现

2.1 服务插件管理器

微服务架构中的插件系统需要支持动态加载、热插拔和分布式管理:

javascript 复制代码
// 微服务插件管理器
class PluginManager {
  constructor() {
    this.plugins = new Map()
    this.pluginRegistry = new PluginRegistry()
    this.lifecycleManager = new PluginLifecycleManager()
    this.dependencyResolver = new DependencyResolver()
    this.eventBus = new EventBus()
  }

  // 加载插件
  async loadPlugin(pluginName, version = 'latest') {
    try {
      // 检查插件是否已加载
      const pluginKey = `${pluginName}:${version}`
      if (this.plugins.has(pluginKey)) {
        return this.plugins.get(pluginKey)
      }

      // 从插件注册中心获取插件信息
      const pluginInfo = await this.pluginRegistry.getPlugin(pluginName, version)
      if (!pluginInfo) {
        throw new Error(`Plugin not found: ${pluginName}:${version}`)
      }

      // 解析插件依赖
      await this.dependencyResolver.resolveDependencies(pluginInfo)

      // 下载并初始化插件
      const plugin = await this.initializePlugin(pluginInfo)

      // 注册插件
      this.plugins.set(pluginKey, plugin)

      // 启动插件生命周期
      await this.lifecycleManager.startPlugin(plugin)

      // 触发插件加载事件
      this.eventBus.emit('plugin:loaded', { name: pluginName, version, plugin })

      console.log(`Plugin loaded successfully: ${pluginName}:${version}`)
      return plugin

    } catch (error) {
      console.error(`Failed to load plugin ${pluginName}:`, error)
      throw error
    }
  }

  // 卸载插件
  async unloadPlugin(pluginName, version = 'latest') {
    const pluginKey = `${pluginName}:${version}`
    const plugin = this.plugins.get(pluginKey)
    
    if (!plugin) {
      throw new Error(`Plugin not loaded: ${pluginName}:${version}`)
    }

    try {
      // 停止插件生命周期
      await this.lifecycleManager.stopPlugin(plugin)

      // 清理插件资源
      await this.cleanupPluginResources(plugin)

      // 从插件列表移除
      this.plugins.delete(pluginKey)

      // 触发插件卸载事件
      this.eventBus.emit('plugin:unloaded', { name: pluginName, version, plugin })

      console.log(`Plugin unloaded successfully: ${pluginName}:${version}`)
    } catch (error) {
      console.error(`Failed to unload plugin ${pluginName}:`, error)
      throw error
    }
  }

  // 获取插件
  getPlugin(pluginName, version = 'latest') {
    const pluginKey = `${pluginName}:${version}`
    return this.plugins.get(pluginKey)
  }

  // 初始化插件
  async initializePlugin(pluginInfo) {
    const {
      name,
      version,
      entry,
      config,
      permissions
    } = pluginInfo

    // 创建插件沙箱环境
    const sandbox = await this.createPluginSandbox(name, permissions)

    // 加载插件代码
    const pluginModule = await this.loadPluginModule(entry, sandbox)

    // 创建插件实例
    const plugin = new ServicePlugin({
      name,
      version,
      module: pluginModule,
      config,
      sandbox,
      manager: this
    })

    return plugin
  }

  // 创建插件沙箱
  async createPluginSandbox(pluginName, permissions) {
    return new PluginSandbox({
      name: pluginName,
      permissions: permissions || [],
      isolateStorage: true,
      allowedAPIs: this.getAllowedAPIs(permissions)
    })
  }

  // 加载插件模块
  async loadPluginModule(entry, sandbox) {
    if (entry.startsWith('http://') || entry.startsWith('https://')) {
      // 远程插件加载
      return await this.loadRemotePlugin(entry, sandbox)
    } else {
      // 本地插件加载
      return await this.loadLocalPlugin(entry, sandbox)
    }
  }

  // 加载远程插件
  async loadRemotePlugin(url, sandbox) {
    const response = await fetch(url)
    const pluginCode = await response.text()
    
    // 在沙箱中执行插件代码
    return await sandbox.execute(pluginCode)
  }

  // 加载本地插件
  async loadLocalPlugin(path, sandbox) {
    const pluginModule = await import(path)
    return pluginModule.default || pluginModule
  }

  // 获取允许的API
  getAllowedAPIs(permissions) {
    const apiMap = {
      'service:invoke': ['invokeService', 'discoverServices'],
      'config:read': ['getConfig', 'watchConfig'],
      'config:write': ['setConfig', 'updateConfig'],
      'event:publish': ['publishEvent', 'emit'],
      'event:subscribe': ['subscribeEvent', 'on'],
      'storage:read': ['getStorage', 'listStorage'],
      'storage:write': ['setStorage', 'deleteStorage']
    }

    const allowedAPIs = []
    permissions.forEach(permission => {
      if (apiMap[permission]) {
        allowedAPIs.push(...apiMap[permission])
      }
    })

    return allowedAPIs
  }

  // 清理插件资源
  async cleanupPluginResources(plugin) {
    try {
      // 调用插件的清理方法
      if (plugin.module && plugin.module.cleanup) {
        await plugin.module.cleanup()
      }

      // 销毁沙箱
      if (plugin.sandbox) {
        await plugin.sandbox.destroy()
      }

      // 清理事件监听器
      this.eventBus.removeAllListeners(`plugin:${plugin.name}`)

    } catch (error) {
      console.error(`Plugin resource cleanup failed for ${plugin.name}:`, error)
    }
  }

  // 重新加载插件
  async reloadPlugin(pluginName, version = 'latest') {
    await this.unloadPlugin(pluginName, version)
    return await this.loadPlugin(pluginName, version)
  }

  // 列出已加载的插件
  listPlugins() {
    const plugins = []
    for (const [key, plugin] of this.plugins) {
      plugins.push({
        name: plugin.name,
        version: plugin.version,
        status: plugin.status,
        loadedAt: plugin.loadedAt
      })
    }
    return plugins
  }

  // 获取插件统计信息
  getPluginStats() {
    return {
      totalPlugins: this.plugins.size,
      runningPlugins: Array.from(this.plugins.values()).filter(p => p.status === 'running').length,
      failedPlugins: Array.from(this.plugins.values()).filter(p => p.status === 'failed').length
    }
  }
}

// 服务插件基类
class ServicePlugin {
  constructor(options) {
    this.name = options.name
    this.version = options.version
    this.module = options.module
    this.config = options.config
    this.sandbox = options.sandbox
    this.manager = options.manager
    this.status = 'loaded'
    this.loadedAt = new Date()
  }

  // 启动插件
  async start() {
    try {
      if (this.module.start) {
        await this.module.start(this.config)
      }
      this.status = 'running'
      console.log(`Plugin started: ${this.name}`)
    } catch (error) {
      this.status = 'failed'
      console.error(`Plugin start failed: ${this.name}`, error)
      throw error
    }
  }

  // 停止插件
  async stop() {
    try {
      if (this.module.stop) {
        await this.module.stop()
      }
      this.status = 'stopped'
      console.log(`Plugin stopped: ${this.name}`)
    } catch (error) {
      console.error(`Plugin stop failed: ${this.name}`, error)
      throw error
    }
  }

  // 重启插件
  async restart() {
    await this.stop()
    await this.start()
  }

  // 获取插件信息
  getInfo() {
    return {
      name: this.name,
      version: this.version,
      status: this.status,
      loadedAt: this.loadedAt,
      config: this.config
    }
  }
}

2.2 服务注册中心实现

javascript 复制代码
// 服务注册中心
class ServiceRegistry {
  constructor() {
    this.services = new Map()
    this.watchers = new Set()
    this.healthCheckers = new Map()
    this.eventEmitter = new EventEmitter()
    this.persistenceLayer = new RegistryPersistence()
    this.clusterManager = new ClusterManager()
  }

  // 启动注册中心
  async start() {
    // 从持久化层加载服务数据
    await this.loadPersistedServices()
    
    // 启动集群同步
    await this.clusterManager.start()
    
    // 设置集群事件监听
    this.setupClusterEventHandlers()
    
    console.log('Service registry started')
  }

  // 注册服务实例
  async register(serviceInstance) {
    const {
      name,
      version,
      host,
      port,
      protocol,
      healthCheck,
      metadata
    } = serviceInstance

    // 生成服务实例ID
    const instanceId = this.generateInstanceId(name, version, host, port)
    
    // 创建服务注册信息
    const registration = {
      id: instanceId,
      name,
      version,
      host,
      port,
      protocol,
      healthCheck,
      metadata: metadata || {},
      registeredAt: new Date(),
      lastHeartbeat: new Date(),
      status: 'healthy'
    }

    // 添加到服务列表
    const serviceKey = `${name}:${version}`
    if (!this.services.has(serviceKey)) {
      this.services.set(serviceKey, new Map())
    }
    this.services.get(serviceKey).set(instanceId, registration)

    // 持久化服务信息
    await this.persistenceLayer.saveService(registration)

    // 同步到集群
    await this.clusterManager.syncServiceRegistration(registration)

    // 启动健康检查
    this.startHealthCheck(registration)

    // 通知观察者
    this.notifyWatchers('service-registered', registration)
    this.eventEmitter.emit('service-registered', registration)

    console.log(`Service registered: ${instanceId}`)
    return registration
  }

  // 注销服务实例
  async unregister(serviceInstance) {
    const instanceId = serviceInstance.id || 
      this.generateInstanceId(serviceInstance.name, serviceInstance.version, serviceInstance.host, serviceInstance.port)

    const registration = this.findServiceInstance(instanceId)
    if (!registration) {
      throw new Error(`Service instance not found: ${instanceId}`)
    }

    // 从服务列表移除
    const serviceKey = `${registration.name}:${registration.version}`
    const instances = this.services.get(serviceKey)
    if (instances) {
      instances.delete(instanceId)
      if (instances.size === 0) {
        this.services.delete(serviceKey)
      }
    }

    // 停止健康检查
    this.stopHealthCheck(instanceId)

    // 从持久化层删除
    await this.persistenceLayer.deleteService(instanceId)

    // 同步到集群
    await this.clusterManager.syncServiceUnregistration(registration)

    // 通知观察者
    this.notifyWatchers('service-unregistered', registration)
    this.eventEmitter.emit('service-unregistered', registration)

    console.log(`Service unregistered: ${instanceId}`)
  }

  // 发现服务
  async discover(serviceName, version = 'latest') {
    const serviceKey = `${serviceName}:${version}`
    const instances = this.services.get(serviceKey)
    
    if (!instances || instances.size === 0) {
      return []
    }

    // 返回健康的服务实例
    return Array.from(instances.values())
      .filter(instance => instance.status === 'healthy')
      .map(instance => ({
        id: instance.id,
        name: instance.name,
        version: instance.version,
        host: instance.host,
        port: instance.port,
        protocol: instance.protocol,
        metadata: instance.metadata
      }))
  }

  // 列出所有服务
  async listServices() {
    const serviceList = []
    
    for (const [serviceKey, instances] of this.services) {
      for (const instance of instances.values()) {
        serviceList.push(instance)
      }
    }

    return serviceList
  }

  // 发送心跳
  async heartbeat(instanceId) {
    const instance = this.findServiceInstance(instanceId)
    if (instance) {
      instance.lastHeartbeat = new Date()
      instance.status = 'healthy'
      
      // 更新持久化数据
      await this.persistenceLayer.updateService(instance)
      
      // 同步到集群
      await this.clusterManager.syncHeartbeat(instance)
    }
  }

  // 启动健康检查
  startHealthCheck(serviceInstance) {
    const checkInterval = setInterval(async () => {
      try {
        const isHealthy = await this.performHealthCheck(serviceInstance)
        const newStatus = isHealthy ? 'healthy' : 'unhealthy'
        
        if (serviceInstance.status !== newStatus) {
          serviceInstance.status = newStatus
          serviceInstance.lastStatusChange = new Date()
          
          // 更新持久化数据
          await this.persistenceLayer.updateService(serviceInstance)
          
          // 通知状态变化
          this.notifyWatchers('service-status-changed', serviceInstance)
          this.eventEmitter.emit('service-status-changed', serviceInstance)
        }
      } catch (error) {
        console.error(`Health check failed for ${serviceInstance.id}:`, error)
        serviceInstance.status = 'unhealthy'
      }
    }, 10000) // 每10秒检查一次

    this.healthCheckers.set(serviceInstance.id, checkInterval)
  }

  // 停止健康检查
  stopHealthCheck(instanceId) {
    const checkInterval = this.healthCheckers.get(instanceId)
    if (checkInterval) {
      clearInterval(checkInterval)
      this.healthCheckers.delete(instanceId)
    }
  }

  // 执行健康检查
  async performHealthCheck(serviceInstance) {
    const healthUrl = `${serviceInstance.protocol}://${serviceInstance.host}:${serviceInstance.port}${serviceInstance.healthCheck}`
    
    try {
      const response = await fetch(healthUrl, {
        method: 'GET',
        timeout: 5000
      })
      return response.ok
    } catch (error) {
      return false
    }
  }

  // 添加服务监听器
  watch(callback) {
    this.watchers.add(callback)
    
    // 返回取消监听的函数
    return () => {
      this.watchers.delete(callback)
    }
  }

  // 通知监听器
  notifyWatchers(event, data) {
    this.watchers.forEach(callback => {
      try {
        callback(event, data)
      } catch (error) {
        console.error('Watcher callback failed:', error)
      }
    })
  }

  // 查找服务实例
  findServiceInstance(instanceId) {
    for (const instances of this.services.values()) {
      if (instances.has(instanceId)) {
        return instances.get(instanceId)
      }
    }
    return null
  }

  // 生成实例ID
  generateInstanceId(name, version, host, port) {
    return `${name}-${version}-${host}-${port}-${Date.now()}`
  }

  // 从持久化层加载服务
  async loadPersistedServices() {
    try {
      const services = await this.persistenceLayer.loadAllServices()
      
      services.forEach(service => {
        const serviceKey = `${service.name}:${service.version}`
        if (!this.services.has(serviceKey)) {
          this.services.set(serviceKey, new Map())
        }
        this.services.get(serviceKey).set(service.id, service)
        
        // 重启健康检查
        this.startHealthCheck(service)
      })
      
      console.log(`Loaded ${services.length} persisted services`)
    } catch (error) {
      console.error('Failed to load persisted services:', error)
    }
  }

  // 设置集群事件处理器
  setupClusterEventHandlers() {
    this.clusterManager.on('service-registered', (service) => {
      // 处理来自其他节点的服务注册
      const serviceKey = `${service.name}:${service.version}`
      if (!this.services.has(serviceKey)) {
        this.services.set(serviceKey, new Map())
      }
      this.services.get(serviceKey).set(service.id, service)
      
      this.notifyWatchers('service-registered', service)
    })

    this.clusterManager.on('service-unregistered', (service) => {
      // 处理来自其他节点的服务注销
      const serviceKey = `${service.name}:${service.version}`
      const instances = this.services.get(serviceKey)
      if (instances) {
        instances.delete(service.id)
        if (instances.size === 0) {
          this.services.delete(serviceKey)
        }
      }
      
      this.notifyWatchers('service-unregistered', service)
    })
  }

  // 获取注册中心统计信息
  getStats() {
    let totalInstances = 0
    let healthyInstances = 0
    let unhealthyInstances = 0
    
    for (const instances of this.services.values()) {
      for (const instance of instances.values()) {
        totalInstances++
        if (instance.status === 'healthy') {
          healthyInstances++
        } else {
          unhealthyInstances++
        }
      }
    }

    return {
      totalServices: this.services.size,
      totalInstances,
      healthyInstances,
      unhealthyInstances,
      watchers: this.watchers.size
    }
  }

  // 停止注册中心
  async stop() {
    // 停止所有健康检查
    for (const checkInterval of this.healthCheckers.values()) {
      clearInterval(checkInterval)
    }
    this.healthCheckers.clear()

    // 停止集群管理器
    await this.clusterManager.stop()

    // 清理监听器
    this.watchers.clear()

    console.log('Service registry stopped')
  }
}

三、服务间通信与API网关

3.1 API网关实现

API网关作为服务化架构的统一入口,负责请求路由、协议转换、安全认证和流量控制:

javascript 复制代码
// API网关核心实现
class APIGateway {
  constructor() {
    this.routes = new Map()
    this.middlewares = []
    this.serviceRegistry = new ServiceRegistry()
    this.loadBalancer = new LoadBalancer()
    this.rateLimiter = new RateLimiter()
    this.authenticator = new Authenticator()
    this.circuitBreaker = new CircuitBreaker()
    this.requestCache = new RequestCache()
    
    this.httpServer = null
    this.wsServer = null
  }

  // 启动网关
  async start(port = 8080) {
    // 创建HTTP服务器
    this.httpServer = http.createServer(this.handleRequest.bind(this))
    
    // 创建WebSocket服务器
    this.wsServer = new WebSocketServer({ server: this.httpServer })
    this.wsServer.on('connection', this.handleWebSocketConnection.bind(this))

    // 应用全局中间件
    this.applyGlobalMiddleware()

    // 启动服务器
    await new Promise((resolve) => {
      this.httpServer.listen(port, () => {
        console.log(`API Gateway started on port ${port}`)
        resolve()
      })
    })

    // 启动健康检查
    this.startHealthCheck()
  }

  // 处理HTTP请求
  async handleRequest(req, res) {
    const startTime = Date.now()
    
    try {
      // 设置CORS头
      this.setCorsHeaders(res)
      
      if (req.method === 'OPTIONS') {
        res.writeHead(200)
        res.end()
        return
      }

      // 解析请求
      const requestContext = await this.parseRequest(req)
      
      // 应用请求中间件
      await this.applyRequestMiddleware(requestContext, res)

      // 路由匹配
      const route = this.matchRoute(requestContext.path, requestContext.method)
      if (!route) {
        this.sendError(res, 404, 'Route not found')
        return
      }

      // 执行路由处理
      const response = await this.executeRoute(route, requestContext)
      
      // 发送响应
      this.sendResponse(res, response)

      // 记录请求指标
      this.recordMetrics(requestContext, response, Date.now() - startTime)

    } catch (error) {
      console.error('Gateway request error:', error)
      this.sendError(res, 500, 'Internal server error')
    }
  }

  // 添加路由
  addRoute(path, method, target, options = {}) {
    const {
      timeout = 30000,
      retries = 3,
      cache = false,
      rateLimit = null,
      auth = false,
      circuitBreaker = true
    } = options

    const route = {
      path,
      method: method.toUpperCase(),
      target,
      timeout,
      retries,
      cache,
      rateLimit,
      auth,
      circuitBreaker,
      pattern: this.createRoutePattern(path)
    }

    const routeKey = `${method.toUpperCase()}:${path}`
    this.routes.set(routeKey, route)
    
    console.log(`Route added: ${routeKey} -> ${target}`)
    return route
  }

  // 匹配路由
  matchRoute(path, method) {
    const methodRoutes = Array.from(this.routes.values())
      .filter(route => route.method === method.toUpperCase())

    for (const route of methodRoutes) {
      if (route.pattern.test(path)) {
        return route
      }
    }

    return null
  }

  // 执行路由
  async executeRoute(route, context) {
    const { target, timeout, retries, cache, rateLimit, auth, circuitBreaker } = route

    try {
      // 身份认证
      if (auth && !await this.authenticateRequest(context)) {
        return { status: 401, body: { error: 'Unauthorized' } }
      }

      // 限流检查
      if (rateLimit && !await this.checkRateLimit(context, rateLimit)) {
        return { status: 429, body: { error: 'Too many requests' } }
      }

      // 缓存检查
      if (cache) {
        const cachedResponse = await this.requestCache.get(this.generateCacheKey(context))
        if (cachedResponse) {
          return cachedResponse
        }
      }

      // 服务发现和负载均衡
      const serviceInstance = await this.selectServiceInstance(target)
      if (!serviceInstance) {
        return { status: 503, body: { error: 'Service unavailable' } }
      }

      // 执行服务调用
      let response
      if (circuitBreaker) {
        response = await this.circuitBreaker.call(
          serviceInstance.id,
          () => this.invokeService(serviceInstance, context, { timeout, retries })
        )
      } else {
        response = await this.invokeService(serviceInstance, context, { timeout, retries })
      }

      // 缓存响应
      if (cache && response.status === 200) {
        await this.requestCache.set(this.generateCacheKey(context), response, cache.ttl)
      }

      return response

    } catch (error) {
      console.error('Route execution error:', error)
      return { status: 500, body: { error: 'Service execution failed' } }
    }
  }

  // 调用服务
  async invokeService(serviceInstance, context, options = {}) {
    const { timeout, retries } = options
    const targetUrl = `${serviceInstance.protocol}://${serviceInstance.host}:${serviceInstance.port}`
    
    let lastError
    for (let attempt = 0; attempt <= retries; attempt++) {
      try {
        const requestOptions = {
          method: context.method,
          url: `${targetUrl}${context.path}`,
          headers: {
            ...context.headers,
            'X-Forwarded-For': context.clientIP,
            'X-Forwarded-Proto': context.protocol,
            'X-Gateway-Request-Id': context.requestId
          },
          timeout,
          data: context.body
        }

        const response = await this.httpClient.request(requestOptions)
        
        return {
          status: response.status,
          headers: response.headers,
          body: response.data
        }

      } catch (error) {
        lastError = error
        
        if (attempt < retries) {
          // 指数退避重试
          await this.delay(Math.pow(2, attempt) * 1000)
          continue
        }
      }
    }

    throw lastError
  }

  // 选择服务实例
  async selectServiceInstance(serviceName) {
    try {
      const services = await this.serviceRegistry.discover(serviceName)
      if (services.length === 0) {
        return null
      }

      return this.loadBalancer.select(services)
    } catch (error) {
      console.error('Service selection error:', error)
      return null
    }
  }

  // 应用全局中间件
  applyGlobalMiddleware() {
    // 请求ID中间件
    this.use(async (context, next) => {
      context.requestId = this.generateRequestId()
      await next()
    })

    // 请求日志中间件
    this.use(async (context, next) => {
      const startTime = Date.now()
      await next()
      const duration = Date.now() - startTime
      console.log(`${context.method} ${context.path} ${context.status} ${duration}ms`)
    })

    // 错误处理中间件
    this.use(async (context, next) => {
      try {
        await next()
      } catch (error) {
        console.error('Middleware error:', error)
        context.status = 500
        context.body = { error: 'Internal server error' }
      }
    })
  }

  // 添加中间件
  use(middleware) {
    this.middlewares.push(middleware)
  }

  // 应用请求中间件
  async applyRequestMiddleware(context, res) {
    let index = 0
    
    const next = async () => {
      if (index < this.middlewares.length) {
        const middleware = this.middlewares[index++]
        await middleware(context, next)
      }
    }

    await next()
  }

  // 解析请求
  async parseRequest(req) {
    const url = new URL(req.url, `http://${req.headers.host}`)
    
    return {
      method: req.method,
      path: url.pathname,
      query: Object.fromEntries(url.searchParams),
      headers: req.headers,
      body: await this.parseRequestBody(req),
      clientIP: this.getClientIP(req),
      protocol: req.connection.encrypted ? 'https' : 'http'
    }
  }

  // 解析请求体
  async parseRequestBody(req) {
    return new Promise((resolve) => {
      let body = ''
      req.on('data', chunk => {
        body += chunk.toString()
      })
      req.on('end', () => {
        try {
          resolve(body ? JSON.parse(body) : null)
        } catch (error) {
          resolve(body)
        }
      })
    })
  }

  // 发送响应
  sendResponse(res, response) {
    res.writeHead(response.status, response.headers || {})
    
    if (response.body) {
      if (typeof response.body === 'object') {
        res.end(JSON.stringify(response.body))
      } else {
        res.end(response.body)
      }
    } else {
      res.end()
    }
  }

  // 发送错误响应
  sendError(res, status, message) {
    res.writeHead(status, { 'Content-Type': 'application/json' })
    res.end(JSON.stringify({ error: message }))
  }

  // 设置CORS头
  setCorsHeaders(res) {
    res.setHeader('Access-Control-Allow-Origin', '*')
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization')
  }

  // 身份认证
  async authenticateRequest(context) {
    const token = context.headers.authorization?.replace('Bearer ', '')
    if (!token) {
      return false
    }

    return await this.authenticator.verify(token)
  }

  // 限流检查
  async checkRateLimit(context, rateLimit) {
    const key = `${context.clientIP}:${context.path}`
    return await this.rateLimiter.allow(key, rateLimit.requests, rateLimit.window)
  }

  // 生成缓存键
  generateCacheKey(context) {
    return `${context.method}:${context.path}:${JSON.stringify(context.query)}:${JSON.stringify(context.body)}`
  }

  // 创建路由模式
  createRoutePattern(path) {
    // 转换路径参数 :id -> ([^/]+)
    const pattern = path.replace(/:([^/]+)/g, '([^/]+)')
    return new RegExp(`^${pattern}$`)
  }

  // 获取客户端IP
  getClientIP(req) {
    return req.headers['x-forwarded-for'] ||
           req.headers['x-real-ip'] ||
           req.connection.remoteAddress ||
           'unknown'
  }

  // 生成请求ID
  generateRequestId() {
    return 'req_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
  }

  // 记录指标
  recordMetrics(context, response, duration) {
    // 实现指标收集逻辑
    const metrics = {
      path: context.path,
      method: context.method,
      status: response.status,
      duration,
      timestamp: Date.now()
    }
    
    // 发送到监控系统
    this.sendMetrics(metrics)
  }

  // 发送指标
  sendMetrics(metrics) {
    // 实现指标发送逻辑
    console.log('Metrics:', metrics)
  }

  // 延迟函数
  delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }

  // 启动健康检查
  startHealthCheck() {
    setInterval(() => {
      this.performHealthCheck()
    }, 30000) // 每30秒检查一次
  }

  // 执行健康检查
  async performHealthCheck() {
    try {
      // 检查依赖服务状态
      const services = await this.serviceRegistry.listServices()
      const healthyServices = services.filter(s => s.status === 'healthy')
      
      console.log(`Health check: ${healthyServices.length}/${services.length} services healthy`)
    } catch (error) {
      console.error('Health check error:', error)
    }
  }

  // 处理WebSocket连接
  handleWebSocketConnection(ws) {
    console.log('WebSocket connection established')
    
    ws.on('message', async (message) => {
      try {
        const data = JSON.parse(message)
        const response = await this.handleWebSocketMessage(data)
        ws.send(JSON.stringify(response))
      } catch (error) {
        console.error('WebSocket message error:', error)
        ws.send(JSON.stringify({ error: 'Message processing failed' }))
      }
    })

    ws.on('close', () => {
      console.log('WebSocket connection closed')
    })
  }

  // 处理WebSocket消息
  async handleWebSocketMessage(data) {
    const { type, payload } = data
    
    switch (type) {
      case 'service-call':
        return await this.handleWebSocketServiceCall(payload)
      case 'subscribe':
        return await this.handleWebSocketSubscribe(payload)
      default:
        return { error: 'Unknown message type' }
    }
  }

  // 处理WebSocket服务调用
  async handleWebSocketServiceCall(payload) {
    const { service, method, params } = payload
    
    try {
      const serviceInstance = await this.selectServiceInstance(service)
      if (!serviceInstance) {
        return { error: 'Service not available' }
      }

      const context = {
        method: 'POST',
        path: `/api/${method}`,
        headers: { 'content-type': 'application/json' },
        body: params
      }

      const response = await this.invokeService(serviceInstance, context)
      return { success: true, data: response.body }
      
    } catch (error) {
      return { error: error.message }
    }
  }

  // 停止网关
  async stop() {
    if (this.httpServer) {
      await new Promise((resolve) => {
        this.httpServer.close(resolve)
      })
    }

    if (this.wsServer) {
      this.wsServer.close()
    }

    console.log('API Gateway stopped')
  }
}

3.2 负载均衡器实现

javascript 复制代码
// 负载均衡器
class LoadBalancer {
  constructor() {
    this.services = new Map()
    this.strategies = new Map()
    
    // 注册负载均衡策略
    this.registerStrategy('round-robin', new RoundRobinStrategy())
    this.registerStrategy('weighted-round-robin', new WeightedRoundRobinStrategy())
    this.registerStrategy('least-connections', new LeastConnectionsStrategy())
    this.registerStrategy('random', new RandomStrategy())
    this.registerStrategy('hash', new HashStrategy())
  }

  // 添加服务实例
  addService(serviceInstance) {
    const serviceName = serviceInstance.name
    
    if (!this.services.has(serviceName)) {
      this.services.set(serviceName, {
        instances: [],
        strategy: 'round-robin',
        config: {}
      })
    }

    const service = this.services.get(serviceName)
    service.instances.push({
      ...serviceInstance,
      connections: 0,
      totalRequests: 0,
      failedRequests: 0,
      avgResponseTime: 0,
      lastUsed: 0
    })

    console.log(`Service instance added to load balancer: ${serviceInstance.id}`)
  }

  // 移除服务实例
  removeService(serviceInstance) {
    const serviceName = serviceInstance.name
    const service = this.services.get(serviceName)
    
    if (service) {
      service.instances = service.instances.filter(
        instance => instance.id !== serviceInstance.id
      )
      
      if (service.instances.length === 0) {
        this.services.delete(serviceName)
      }
    }

    console.log(`Service instance removed from load balancer: ${serviceInstance.id}`)
  }

  // 选择服务实例
  select(serviceName, context = {}) {
    const service = this.services.get(serviceName)
    if (!service || service.instances.length === 0) {
      return null
    }

    // 过滤健康的实例
    const healthyInstances = service.instances.filter(
      instance => instance.status === 'healthy'
    )

    if (healthyInstances.length === 0) {
      return null
    }

    // 使用负载均衡策略选择实例
    const strategy = this.strategies.get(service.strategy)
    if (!strategy) {
      throw new Error(`Unknown load balancing strategy: ${service.strategy}`)
    }

    const selectedInstance = strategy.select(healthyInstances, service.config, context)
    
    // 更新实例统计信息
    selectedInstance.connections++
    selectedInstance.totalRequests++
    selectedInstance.lastUsed = Date.now()

    return selectedInstance
  }

  // 设置负载均衡策略
  setStrategy(serviceName, strategy, config = {}) {
    const service = this.services.get(serviceName)
    if (service) {
      service.strategy = strategy
      service.config = config
    }
  }

  // 注册负载均衡策略
  registerStrategy(name, strategy) {
    this.strategies.set(name, strategy)
  }

  // 更新实例统计
  updateInstanceStats(instanceId, stats) {
    for (const service of this.services.values()) {
      const instance = service.instances.find(i => i.id === instanceId)
      if (instance) {
        Object.assign(instance, stats)
        break
      }
    }
  }

  // 获取服务统计信息
  getServiceStats(serviceName) {
    const service = this.services.get(serviceName)
    if (!service) {
      return null
    }

    return {
      totalInstances: service.instances.length,
      healthyInstances: service.instances.filter(i => i.status === 'healthy').length,
      strategy: service.strategy,
      instances: service.instances.map(instance => ({
        id: instance.id,
        host: instance.host,
        port: instance.port,
        status: instance.status,
        connections: instance.connections,
        totalRequests: instance.totalRequests,
        failedRequests: instance.failedRequests,
        avgResponseTime: instance.avgResponseTime
      }))
    }
  }
}

// 轮询策略
class RoundRobinStrategy {
  constructor() {
    this.counters = new Map()
  }

  select(instances, config, context) {
    const serviceKey = instances[0]?.name || 'default'
    let counter = this.counters.get(serviceKey) || 0
    
    const selectedInstance = instances[counter % instances.length]
    this.counters.set(serviceKey, counter + 1)
    
    return selectedInstance
  }
}

// 加权轮询策略
class WeightedRoundRobinStrategy {
  constructor() {
    this.counters = new Map()
  }

  select(instances, config, context) {
    const serviceKey = instances[0]?.name || 'default'
    
    // 计算权重总和
    const totalWeight = instances.reduce((sum, instance) => {
      return sum + (instance.weight || 1)
    }, 0)

    let counter = this.counters.get(serviceKey) || 0
    counter = (counter + 1) % totalWeight
    this.counters.set(serviceKey, counter)

    // 根据权重选择实例
    let currentWeight = 0
    for (const instance of instances) {
      currentWeight += (instance.weight || 1)
      if (counter < currentWeight) {
        return instance
      }
    }

    return instances[0]
  }
}

// 最少连接策略
class LeastConnectionsStrategy {
  select(instances, config, context) {
    return instances.reduce((min, current) => {
      return current.connections < min.connections ? current : min
    })
  }
}

// 随机策略
class RandomStrategy {
  select(instances, config, context) {
    const randomIndex = Math.floor(Math.random() * instances.length)
    return instances[randomIndex]
  }
}

// 哈希策略
class HashStrategy {
  select(instances, config, context) {
    const hashKey = config.key || context.clientIP || 'default'
    const hash = this.simpleHash(hashKey)
    const index = hash % instances.length
    return instances[index]
  }

  simpleHash(str) {
    let hash = 0
    for (let i = 0; i < str.length; i++) {
      const char = str.charCodeAt(i)
      hash = ((hash << 5) - hash) + char
      hash = hash & hash // 转换为32位整数
    }
    return Math.abs(hash)
  }
}

3.3 服务通信协议实现

javascript 复制代码
// 服务通信管理器
class ServiceCommunicator {
  constructor() {
    this.protocols = new Map()
    this.serializers = new Map()
    this.interceptors = []
    
    // 注册协议处理器
    this.registerProtocol('http', new HTTPProtocolHandler())
    this.registerProtocol('grpc', new GRPCProtocolHandler())
    this.registerProtocol('websocket', new WebSocketProtocolHandler())
    this.registerProtocol('message-queue', new MessageQueueProtocolHandler())

    // 注册序列化器
    this.registerSerializer('json', new JSONSerializer())
    this.registerSerializer('protobuf', new ProtobufSerializer())
    this.registerSerializer('msgpack', new MessagePackSerializer())
  }

  // 注册协议处理器
  registerProtocol(name, handler) {
    this.protocols.set(name, handler)
  }

  // 注册序列化器
  registerSerializer(name, serializer) {
    this.serializers.set(name, serializer)
  }

  // 添加拦截器
  addInterceptor(interceptor) {
    this.interceptors.push(interceptor)
  }

  // 发送请求
  async sendRequest(target, request, options = {}) {
    const {
      protocol = 'http',
      serializer = 'json',
      timeout = 30000,
      retries = 3
    } = options

    // 获取协议处理器
    const protocolHandler = this.protocols.get(protocol)
    if (!protocolHandler) {
      throw new Error(`Unsupported protocol: ${protocol}`)
    }

    // 获取序列化器
    const serializerInstance = this.serializers.get(serializer)
    if (!serializerInstance) {
      throw new Error(`Unsupported serializer: ${serializer}`)
    }

    // 序列化请求
    const serializedRequest = await serializerInstance.serialize(request)

    // 应用请求拦截器
    const processedRequest = await this.applyRequestInterceptors({
      target,
      data: serializedRequest,
      protocol,
      serializer,
      options
    })

    let lastError
    for (let attempt = 0; attempt <= retries; attempt++) {
      try {
        // 发送请求
        const response = await protocolHandler.send(
          processedRequest.target,
          processedRequest.data,
          { ...options, timeout }
        )

        // 反序列化响应
        const deserializedResponse = await serializerInstance.deserialize(response)

        // 应用响应拦截器
        const processedResponse = await this.applyResponseInterceptors({
          target,
          request: processedRequest,
          response: deserializedResponse,
          attempt: attempt + 1
        })

        return processedResponse

      } catch (error) {
        lastError = error
        
        if (attempt < retries && this.isRetryableError(error)) {
          // 指数退避
          await this.delay(Math.pow(2, attempt) * 1000)
          continue
        }
        
        break
      }
    }

    throw lastError
  }

  // 应用请求拦截器
  async applyRequestInterceptors(context) {
    let processedContext = { ...context }
    
    for (const interceptor of this.interceptors) {
      if (interceptor.request) {
        processedContext = await interceptor.request(processedContext)
      }
    }
    
    return processedContext
  }

  // 应用响应拦截器
  async applyResponseInterceptors(context) {
    let processedResponse = context.response
    
    for (const interceptor of this.interceptors) {
      if (interceptor.response) {
        processedResponse = await interceptor.response({
          ...context,
          response: processedResponse
        })
      }
    }
    
    return processedResponse
  }

  // 判断是否为可重试错误
  isRetryableError(error) {
    return error.code === 'ECONNRESET' ||
           error.code === 'ETIMEDOUT' ||
           error.code === 'ENOTFOUND' ||
           (error.status >= 500 && error.status < 600)
  }

  // 延迟函数
  delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }
}

// HTTP协议处理器
class HTTPProtocolHandler {
  constructor() {
    this.httpClient = axios.create({
      timeout: 30000,
      validateStatus: () => true
    })
  }

  async send(target, data, options) {
    const response = await this.httpClient.request({
      method: options.method || 'POST',
      url: target,
      data,
      headers: options.headers || {},
      timeout: options.timeout
    })

    if (response.status >= 400) {
      const error = new Error(`HTTP ${response.status}: ${response.statusText}`)
      error.status = response.status
      error.response = response.data
      throw error
    }

    return response.data
  }
}

// gRPC协议处理器
class GRPCProtocolHandler {
  constructor() {
    this.clients = new Map()
  }

  async send(target, data, options) {
    const client = await this.getClient(target, options)
    
    return new Promise((resolve, reject) => {
      const call = client[options.method](data, (error, response) => {
        if (error) {
          reject(error)
        } else {
          resolve(response)
        }
      })

      // 设置超时
      setTimeout(() => {
        call.cancel()
        reject(new Error('gRPC call timeout'))
      }, options.timeout)
    })
  }

  async getClient(target, options) {
    if (this.clients.has(target)) {
      return this.clients.get(target)
    }

    const grpc = require('@grpc/grpc-js')
    const protoLoader = require('@grpc/proto-loader')
    
    const packageDefinition = protoLoader.loadSync(options.protoFile)
    const proto = grpc.loadPackageDefinition(packageDefinition)
    
    const client = new proto[options.service](target, grpc.credentials.createInsecure())
    this.clients.set(target, client)
    
    return client
  }
}

// JSON序列化器
class JSONSerializer {
  async serialize(data) {
    return JSON.stringify(data)
  }

  async deserialize(data) {
    return JSON.parse(data)
  }
}

四、服务化架构流程与生命周期管理

4.1 服务生命周期流程图

sequenceDiagram participant Dev as 开发者 participant CI as CI/CD系统 participant Registry as 服务注册中心 participant Gateway as API网关 participant LB as 负载均衡器 participant Monitor as 监控系统 participant Service as 微服务实例 Dev->>CI: 提交代码 CI->>CI: 构建&测试 CI->>CI: 打包镜像 CI->>Service: 部署服务实例 Service->>Registry: 服务注册 Registry->>Gateway: 服务发现通知 Gateway->>LB: 更新负载均衡 Service->>Monitor: 发送心跳&指标 Monitor->>Monitor: 健康检查 alt 服务健康 Monitor->>Registry: 更新状态为健康 Registry->>Gateway: 状态变更通知 Gateway->>LB: 启用服务实例 else 服务异常 Monitor->>Registry: 更新状态为异常 Registry->>Gateway: 状态变更通知 Gateway->>LB: 禁用服务实例 Monitor->>CI: 触发自动恢复 end Dev->>CI: 服务升级 CI->>Service: 滚动更新 Service->>Registry: 注销旧版本 Service->>Registry: 注册新版本 note over Dev,Service: 服务完整生命周期管理

4.2 服务生命周期管理器

javascript 复制代码
// 服务生命周期管理器
class ServiceLifecycleManager {
  constructor() {
    this.services = new Map()
    this.deploymentStrategies = new Map()
    this.orchestrator = new ServiceOrchestrator()
    this.scaler = new ServiceScaler()
    this.updater = new ServiceUpdater()
    this.monitor = new ServiceMonitor()
    
    // 注册部署策略
    this.registerDeploymentStrategy('rolling', new RollingDeploymentStrategy())
    this.registerDeploymentStrategy('blue-green', new BlueGreenDeploymentStrategy())
    this.registerDeploymentStrategy('canary', new CanaryDeploymentStrategy())
  }

  // 部署服务
  async deployService(serviceConfig) {
    const {
      name,
      version,
      image,
      replicas = 1,
      resources = {},
      strategy = 'rolling',
      healthCheck = {},
      env = {}
    } = serviceConfig

    try {
      console.log(`Starting deployment for ${name}:${version}`)

      // 创建服务部署对象
      const deployment = new ServiceDeployment({
        name,
        version,
        image,
        replicas,
        resources,
        strategy,
        healthCheck,
        env,
        manager: this
      })

      // 获取部署策略
      const deploymentStrategy = this.deploymentStrategies.get(strategy)
      if (!deploymentStrategy) {
        throw new Error(`Unknown deployment strategy: ${strategy}`)
      }

      // 执行部署
      const result = await deploymentStrategy.deploy(deployment)

      // 注册服务
      this.services.set(`${name}:${version}`, deployment)

      // 启动监控
      this.monitor.startMonitoring(deployment)

      console.log(`Service deployed successfully: ${name}:${version}`)
      return result

    } catch (error) {
      console.error(`Service deployment failed: ${name}:${version}`, error)
      throw error
    }
  }

  // 更新服务
  async updateService(serviceName, newConfig) {
    const currentService = this.findService(serviceName)
    if (!currentService) {
      throw new Error(`Service not found: ${serviceName}`)
    }

    try {
      console.log(`Starting update for ${serviceName}`)

      // 执行服务更新
      const result = await this.updater.update(currentService, newConfig)

      console.log(`Service updated successfully: ${serviceName}`)
      return result

    } catch (error) {
      console.error(`Service update failed: ${serviceName}`, error)
      throw error
    }
  }

  // 扩缩容服务
  async scaleService(serviceName, targetReplicas) {
    const service = this.findService(serviceName)
    if (!service) {
      throw new Error(`Service not found: ${serviceName}`)
    }

    try {
      console.log(`Scaling ${serviceName} to ${targetReplicas} replicas`)

      const result = await this.scaler.scale(service, targetReplicas)

      console.log(`Service scaled successfully: ${serviceName}`)
      return result

    } catch (error) {
      console.error(`Service scaling failed: ${serviceName}`, error)
      throw error
    }
  }

  // 停止服务
  async stopService(serviceName) {
    const service = this.findService(serviceName)
    if (!service) {
      throw new Error(`Service not found: ${serviceName}`)
    }

    try {
      console.log(`Stopping service: ${serviceName}`)

      // 优雅关闭服务实例
      await this.orchestrator.gracefulShutdown(service)

      // 从注册中心注销
      await this.orchestrator.unregisterService(service)

      // 停止监控
      this.monitor.stopMonitoring(service)

      // 从本地列表移除
      this.services.delete(`${service.name}:${service.version}`)

      console.log(`Service stopped successfully: ${serviceName}`)

    } catch (error) {
      console.error(`Service stop failed: ${serviceName}`, error)
      throw error
    }
  }

  // 重启服务
  async restartService(serviceName) {
    const service = this.findService(serviceName)
    if (!service) {
      throw new Error(`Service not found: ${serviceName}`)
    }

    try {
      console.log(`Restarting service: ${serviceName}`)

      // 优雅重启
      await this.orchestrator.gracefulRestart(service)

      console.log(`Service restarted successfully: ${serviceName}`)

    } catch (error) {
      console.error(`Service restart failed: ${serviceName}`, error)
      throw error
    }
  }

  // 回滚服务
  async rollbackService(serviceName, targetVersion) {
    const service = this.findService(serviceName)
    if (!service) {
      throw new Error(`Service not found: ${serviceName}`)
    }

    try {
      console.log(`Rolling back ${serviceName} to version ${targetVersion}`)

      const result = await this.updater.rollback(service, targetVersion)

      console.log(`Service rolled back successfully: ${serviceName}`)
      return result

    } catch (error) {
      console.error(`Service rollback failed: ${serviceName}`, error)
      throw error
    }
  }

  // 自动扩缩容
  enableAutoScaling(serviceName, config) {
    const service = this.findService(serviceName)
    if (!service) {
      throw new Error(`Service not found: ${serviceName}`)
    }

    this.scaler.enableAutoScaling(service, config)
    console.log(`Auto scaling enabled for ${serviceName}`)
  }

  // 禁用自动扩缩容
  disableAutoScaling(serviceName) {
    const service = this.findService(serviceName)
    if (!service) {
      throw new Error(`Service not found: ${serviceName}`)
    }

    this.scaler.disableAutoScaling(service)
    console.log(`Auto scaling disabled for ${serviceName}`)
  }

  // 注册部署策略
  registerDeploymentStrategy(name, strategy) {
    this.deploymentStrategies.set(name, strategy)
  }

  // 查找服务
  findService(serviceName) {
    // 如果包含版本号
    if (serviceName.includes(':')) {
      return this.services.get(serviceName)
    }

    // 查找最新版本
    for (const [key, service] of this.services) {
      if (key.startsWith(serviceName + ':')) {
        return service
      }
    }

    return null
  }

  // 列出所有服务
  listServices() {
    const services = []
    for (const [key, service] of this.services) {
      services.push({
        name: service.name,
        version: service.version,
        status: service.status,
        replicas: service.currentReplicas,
        createdAt: service.createdAt,
        updatedAt: service.updatedAt
      })
    }
    return services
  }

  // 获取服务状态
  getServiceStatus(serviceName) {
    const service = this.findService(serviceName)
    if (!service) {
      return null
    }

    return {
      name: service.name,
      version: service.version,
      status: service.status,
      replicas: {
        current: service.currentReplicas,
        desired: service.desiredReplicas
      },
      health: service.healthStatus,
      resources: service.resourceUsage,
      uptime: Date.now() - service.createdAt.getTime()
    }
  }
}

// 服务部署对象
class ServiceDeployment {
  constructor(config) {
    this.name = config.name
    this.version = config.version
    this.image = config.image
    this.desiredReplicas = config.replicas
    this.currentReplicas = 0
    this.resources = config.resources
    this.strategy = config.strategy
    this.healthCheck = config.healthCheck
    this.env = config.env
    this.manager = config.manager
    
    this.status = 'pending'
    this.instances = new Map()
    this.createdAt = new Date()
    this.updatedAt = new Date()
    this.healthStatus = 'unknown'
    this.resourceUsage = {}
  }

  // 添加服务实例
  addInstance(instance) {
    this.instances.set(instance.id, instance)
    this.currentReplicas = this.instances.size
    this.updatedAt = new Date()
  }

  // 移除服务实例
  removeInstance(instanceId) {
    this.instances.delete(instanceId)
    this.currentReplicas = this.instances.size
    this.updatedAt = new Date()
  }

  // 获取健康实例
  getHealthyInstances() {
    return Array.from(this.instances.values())
      .filter(instance => instance.status === 'healthy')
  }

  // 更新状态
  updateStatus(status) {
    this.status = status
    this.updatedAt = new Date()
  }

  // 获取部署信息
  getInfo() {
    return {
      name: this.name,
      version: this.version,
      image: this.image,
      status: this.status,
      replicas: {
        desired: this.desiredReplicas,
        current: this.currentReplicas,
        healthy: this.getHealthyInstances().length
      },
      instances: Array.from(this.instances.values()).map(instance => ({
        id: instance.id,
        status: instance.status,
        host: instance.host,
        port: instance.port
      })),
      createdAt: this.createdAt,
      updatedAt: this.updatedAt
    }
  }
}

4.3 滚动部署策略

javascript 复制代码
// 滚动部署策略
class RollingDeploymentStrategy {
  constructor() {
    this.maxUnavailable = 1
    this.maxSurge = 1
  }

  async deploy(deployment) {
    const { name, version, image, desiredReplicas } = deployment
    
    console.log(`Starting rolling deployment for ${name}:${version}`)

    try {
      // 创建服务实例
      for (let i = 0; i < desiredReplicas; i++) {
        const instance = await this.createServiceInstance({
          name,
          version,
          image,
          index: i,
          env: deployment.env
        })

        deployment.addInstance(instance)

        // 等待实例就绪
        await this.waitForInstanceReady(instance)

        // 注册到服务注册中心
        await this.registerInstance(instance)

        console.log(`Instance ${instance.id} deployed and registered`)
      }

      // 更新部署状态
      deployment.updateStatus('running')

      return deployment.getInfo()

    } catch (error) {
      deployment.updateStatus('failed')
      throw error
    }
  }

  async update(deployment, newConfig) {
    const { image: newImage, version: newVersion } = newConfig
    const instances = Array.from(deployment.instances.values())

    console.log(`Rolling update from ${deployment.version} to ${newVersion}`)

    for (let i = 0; i < instances.length; i++) {
      const oldInstance = instances[i]

      try {
        // 创建新实例
        const newInstance = await this.createServiceInstance({
          name: deployment.name,
          version: newVersion,
          image: newImage,
          index: i,
          env: deployment.env
        })

        // 等待新实例就绪
        await this.waitForInstanceReady(newInstance)

        // 注册新实例
        await this.registerInstance(newInstance)

        // 添加到部署中
        deployment.addInstance(newInstance)

        // 等待流量切换
        await this.waitForTrafficSwitch(newInstance)

        // 停止旧实例
        await this.stopInstance(oldInstance)

        // 从部署中移除旧实例
        deployment.removeInstance(oldInstance.id)

        console.log(`Instance ${oldInstance.id} replaced with ${newInstance.id}`)

        // 滚动间隔
        if (i < instances.length - 1) {
          await this.delay(5000)
        }

      } catch (error) {
        console.error(`Failed to update instance ${oldInstance.id}:`, error)
        throw error
      }
    }

    // 更新部署版本
    deployment.version = newVersion
    deployment.image = newImage
    deployment.updateStatus('running')

    return deployment.getInfo()
  }

  async createServiceInstance(config) {
    const instanceId = `${config.name}-${config.version}-${config.index}-${Date.now()}`
    
    // 模拟容器创建
    const instance = {
      id: instanceId,
      name: config.name,
      version: config.version,
      image: config.image,
      host: this.generateHost(),
      port: this.generatePort(),
      status: 'starting',
      createdAt: new Date()
    }

    // 启动容器(模拟)
    await this.startContainer(instance, config.env)

    return instance
  }

  async startContainer(instance, env) {
    // 模拟容器启动过程
    console.log(`Starting container for instance ${instance.id}`)
    
    // 模拟启动时间
    await this.delay(3000)
    
    instance.status = 'running'
    console.log(`Container started for instance ${instance.id}`)
  }

  async waitForInstanceReady(instance) {
    const maxAttempts = 30
    const checkInterval = 2000

    for (let attempt = 0; attempt < maxAttempts; attempt++) {
      try {
        const isHealthy = await this.performHealthCheck(instance)
        if (isHealthy) {
          instance.status = 'healthy'
          console.log(`Instance ${instance.id} is ready`)
          return
        }
      } catch (error) {
        console.warn(`Health check failed for ${instance.id}:`, error.message)
      }

      if (attempt < maxAttempts - 1) {
        await this.delay(checkInterval)
      }
    }

    throw new Error(`Instance ${instance.id} failed to become ready`)
  }

  async performHealthCheck(instance) {
    // 模拟健康检查
    const healthUrl = `http://${instance.host}:${instance.port}/health`
    
    try {
      // 模拟HTTP请求
      await this.delay(100)
      
      // 90%的成功率
      return Math.random() > 0.1
    } catch (error) {
      return false
    }
  }

  async registerInstance(instance) {
    // 模拟服务注册
    console.log(`Registering instance ${instance.id} with service registry`)
    await this.delay(500)
  }

  async waitForTrafficSwitch(instance) {
    // 等待负载均衡器将流量切换到新实例
    console.log(`Waiting for traffic to switch to ${instance.id}`)
    await this.delay(2000)
  }

  async stopInstance(instance) {
    console.log(`Stopping instance ${instance.id}`)
    
    // 优雅关闭
    instance.status = 'stopping'
    
    // 等待当前请求处理完成
    await this.delay(5000)
    
    instance.status = 'stopped'
    console.log(`Instance ${instance.id} stopped`)
  }

  generateHost() {
    return `10.0.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`
  }

  generatePort() {
    return 8000 + Math.floor(Math.random() * 1000)
  }

  delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }
}

// 蓝绿部署策略
class BlueGreenDeploymentStrategy {
  async deploy(deployment) {
    console.log(`Starting blue-green deployment for ${deployment.name}:${deployment.version}`)

    // 创建绿色环境
    const greenInstances = await this.createGreenEnvironment(deployment)
    
    // 验证绿色环境
    await this.validateGreenEnvironment(greenInstances)
    
    // 切换流量
    await this.switchTraffic(deployment, greenInstances)
    
    // 清理蓝色环境
    await this.cleanupBlueEnvironment(deployment)

    console.log(`Blue-green deployment completed for ${deployment.name}`)
    return deployment.getInfo()
  }

  async createGreenEnvironment(deployment) {
    const instances = []
    
    for (let i = 0; i < deployment.desiredReplicas; i++) {
      const instance = await this.createServiceInstance({
        name: deployment.name,
        version: deployment.version,
        image: deployment.image,
        index: i,
        env: { ...deployment.env, ENVIRONMENT: 'green' }
      })

      instances.push(instance)
      await this.waitForInstanceReady(instance)
    }

    return instances
  }

  async validateGreenEnvironment(instances) {
    console.log('Validating green environment')
    
    // 执行集成测试
    for (const instance of instances) {
      await this.runIntegrationTests(instance)
    }

    console.log('Green environment validation passed')
  }

  async switchTraffic(deployment, greenInstances) {
    console.log('Switching traffic to green environment')
    
    // 更新负载均衡器配置
    await this.updateLoadBalancer(greenInstances)
    
    // 更新部署实例
    deployment.instances.clear()
    greenInstances.forEach(instance => {
      deployment.addInstance(instance)
    })

    console.log('Traffic switched to green environment')
  }

  async cleanupBlueEnvironment(deployment) {
    console.log('Cleaning up blue environment')
    // 清理逻辑
    await this.delay(5000)
    console.log('Blue environment cleanup completed')
  }

  // 其他方法与滚动部署策略类似...
}

4.4 服务自动扩缩容

javascript 复制代码
// 服务自动扩缩容器
class ServiceScaler {
  constructor() {
    this.scalingPolicies = new Map()
    this.metrics = new MetricsCollector()
    this.scalingCooldown = 5 * 60 * 1000 // 5分钟冷却期
  }

  // 启用自动扩缩容
  enableAutoScaling(service, config) {
    const {
      minReplicas = 1,
      maxReplicas = 10,
      targetCPUUtilization = 70,
      targetMemoryUtilization = 80,
      scaleUpCooldown = 3 * 60 * 1000,
      scaleDownCooldown = 5 * 60 * 1000
    } = config

    const policy = {
      service,
      minReplicas,
      maxReplicas,
      targetCPUUtilization,
      targetMemoryUtilization,
      scaleUpCooldown,
      scaleDownCooldown,
      lastScaleTime: 0,
      enabled: true
    }

    this.scalingPolicies.set(service.name, policy)

    // 启动监控
    this.startAutoScalingMonitor(policy)

    console.log(`Auto scaling enabled for ${service.name}`)
  }

  // 禁用自动扩缩容
  disableAutoScaling(service) {
    const policy = this.scalingPolicies.get(service.name)
    if (policy) {
      policy.enabled = false
      this.scalingPolicies.delete(service.name)
    }

    console.log(`Auto scaling disabled for ${service.name}`)
  }

  // 启动自动扩缩容监控
  startAutoScalingMonitor(policy) {
    const monitorInterval = setInterval(async () => {
      if (!policy.enabled) {
        clearInterval(monitorInterval)
        return
      }

      try {
        await this.evaluateScaling(policy)
      } catch (error) {
        console.error(`Auto scaling evaluation failed for ${policy.service.name}:`, error)
      }
    }, 30000) // 每30秒评估一次
  }

  // 评估扩缩容
  async evaluateScaling(policy) {
    const { service, minReplicas, maxReplicas } = policy
    const currentReplicas = service.currentReplicas

    // 获取服务指标
    const metrics = await this.metrics.getServiceMetrics(service.name)
    if (!metrics) {
      return
    }

    const { avgCPUUtilization, avgMemoryUtilization } = metrics

    // 计算期望副本数
    const cpuDesiredReplicas = this.calculateDesiredReplicas(
      currentReplicas,
      avgCPUUtilization,
      policy.targetCPUUtilization
    )

    const memoryDesiredReplicas = this.calculateDesiredReplicas(
      currentReplicas,
      avgMemoryUtilization,
      policy.targetMemoryUtilization
    )

    // 取较高的值
    let desiredReplicas = Math.max(cpuDesiredReplicas, memoryDesiredReplicas)

    // 应用边界限制
    desiredReplicas = Math.max(minReplicas, Math.min(maxReplicas, desiredReplicas))

    // 检查是否需要扩缩容
    if (desiredReplicas !== currentReplicas) {
      const isScaleUp = desiredReplicas > currentReplicas
      const cooldownPeriod = isScaleUp ? policy.scaleUpCooldown : policy.scaleDownCooldown
      
      if (Date.now() - policy.lastScaleTime > cooldownPeriod) {
        await this.scale(service, desiredReplicas)
        policy.lastScaleTime = Date.now()
        
        console.log(
          `Auto scaled ${service.name} from ${currentReplicas} to ${desiredReplicas} replicas ` +
          `(CPU: ${avgCPUUtilization}%, Memory: ${avgMemoryUtilization}%)`
        )
      }
    }
  }

  // 计算期望副本数
  calculateDesiredReplicas(currentReplicas, currentUtilization, targetUtilization) {
    if (currentUtilization === 0) {
      return currentReplicas
    }

    const utilizationRatio = currentUtilization / targetUtilization
    const desiredReplicas = Math.ceil(currentReplicas * utilizationRatio)
    
    return desiredReplicas
  }

  // 执行扩缩容
  async scale(service, targetReplicas) {
    const currentReplicas = service.currentReplicas
    
    if (targetReplicas > currentReplicas) {
      // 扩容
      await this.scaleUp(service, targetReplicas - currentReplicas)
    } else if (targetReplicas < currentReplicas) {
      // 缩容
      await this.scaleDown(service, currentReplicas - targetReplicas)
    }

    service.desiredReplicas = targetReplicas
  }

  // 扩容
  async scaleUp(service, additionalReplicas) {
    console.log(`Scaling up ${service.name} by ${additionalReplicas} replicas`)

    for (let i = 0; i < additionalReplicas; i++) {
      try {
        const instance = await this.createServiceInstance(service, service.currentReplicas + i)
        service.addInstance(instance)
        
        // 等待实例就绪
        await this.waitForInstanceReady(instance)
        
        // 注册实例
        await this.registerInstance(instance)
        
        console.log(`Scaled up instance: ${instance.id}`)
      } catch (error) {
        console.error(`Failed to scale up instance:`, error)
      }
    }
  }

  // 缩容
  async scaleDown(service, removeReplicas) {
    console.log(`Scaling down ${service.name} by ${removeReplicas} replicas`)

    const instances = Array.from(service.instances.values())
    
    // 选择要移除的实例(选择最老的实例)
    const instancesToRemove = instances
      .sort((a, b) => a.createdAt - b.createdAt)
      .slice(-removeReplicas)

    for (const instance of instancesToRemove) {
      try {
        // 优雅关闭实例
        await this.gracefulShutdown(instance)
        
        // 从服务中移除
        service.removeInstance(instance.id)
        
        console.log(`Scaled down instance: ${instance.id}`)
      } catch (error) {
        console.error(`Failed to scale down instance ${instance.id}:`, error)
      }
    }
  }

  // 创建服务实例
  async createServiceInstance(service, index) {
    const instanceId = `${service.name}-${service.version}-${index}-${Date.now()}`
    
    const instance = {
      id: instanceId,
      name: service.name,
      version: service.version,
      image: service.image,
      host: this.generateHost(),
      port: this.generatePort(),
      status: 'starting',
      createdAt: new Date()
    }

    // 启动容器
    await this.startContainer(instance)

    return instance
  }

  // 优雅关闭实例
  async gracefulShutdown(instance) {
    console.log(`Gracefully shutting down instance ${instance.id}`)
    
    // 从负载均衡器移除
    await this.removeFromLoadBalancer(instance)
    
    // 等待当前请求完成
    await this.waitForRequestsToComplete(instance)
    
    // 停止容器
    await this.stopContainer(instance)
  }

  // 其他辅助方法...
  generateHost() {
    return `10.0.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`
  }

  generatePort() {
    return 8000 + Math.floor(Math.random() * 1000)
  }

  delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }
}

五、服务隔离、熔断降级与监控治理

5.1 熔断器实现

熔断器模式是服务化架构中防止级联故障的关键机制:

javascript 复制代码
// 熔断器实现
class CircuitBreaker {
  constructor(options = {}) {
    this.failureThreshold = options.failureThreshold || 5
    this.recoveryTimeout = options.recoveryTimeout || 60000
    this.monitoringPeriod = options.monitoringPeriod || 10000
    this.expectedBehavior = options.expectedBehavior
    
    this.state = 'CLOSED'
    this.failureCount = 0
    this.lastFailureTime = null
    this.nextAttempt = null
    this.monitor = this.setupMonitor()
    this.metrics = {
      totalCalls: 0,
      successCalls: 0,
      failureCalls: 0,
      timeouts: 0,
      rejectedCalls: 0
    }
  }

  // 执行受保护的操作
  async call(operation, timeout = 30000) {
    this.metrics.totalCalls++

    // 如果熔断器打开,直接拒绝
    if (this.state === 'OPEN') {
      if (this.canAttemptReset()) {
        this.state = 'HALF_OPEN'
        console.log('Circuit breaker entering HALF_OPEN state')
      } else {
        this.metrics.rejectedCalls++
        throw new Error('Circuit breaker is OPEN - request rejected')
      }
    }

    try {
      // 执行操作并设置超时
      const result = await this.executeWithTimeout(operation, timeout)
      
      // 操作成功
      this.onSuccess()
      return result

    } catch (error) {
      // 操作失败
      this.onFailure(error)
      throw error
    }
  }

  // 带超时的操作执行
  async executeWithTimeout(operation, timeout) {
    return new Promise(async (resolve, reject) => {
      let timeoutId
      let isCompleted = false

      // 设置超时
      timeoutId = setTimeout(() => {
        if (!isCompleted) {
          isCompleted = true
          this.metrics.timeouts++
          reject(new Error('Operation timeout'))
        }
      }, timeout)

      try {
        const result = await operation()
        if (!isCompleted) {
          isCompleted = true
          clearTimeout(timeoutId)
          resolve(result)
        }
      } catch (error) {
        if (!isCompleted) {
          isCompleted = true
          clearTimeout(timeoutId)
          reject(error)
        }
      }
    })
  }

  // 成功回调
  onSuccess() {
    this.metrics.successCalls++
    
    if (this.state === 'HALF_OPEN') {
      console.log('Circuit breaker transitioning to CLOSED state')
      this.state = 'CLOSED'
    }

    this.failureCount = 0
  }

  // 失败回调
  onFailure(error) {
    this.metrics.failureCalls++
    this.failureCount++
    this.lastFailureTime = Date.now()

    console.log(`Circuit breaker failure ${this.failureCount}/${this.failureThreshold}: ${error.message}`)

    // 检查是否需要打开熔断器
    if (this.state === 'CLOSED' && this.failureCount >= this.failureThreshold) {
      this.state = 'OPEN'
      this.nextAttempt = Date.now() + this.recoveryTimeout
      console.log(`Circuit breaker OPEN - next attempt at ${new Date(this.nextAttempt)}`)
    } else if (this.state === 'HALF_OPEN') {
      this.state = 'OPEN'
      this.nextAttempt = Date.now() + this.recoveryTimeout
      console.log('Circuit breaker returning to OPEN state after half-open failure')
    }
  }

  // 检查是否可以尝试重置
  canAttemptReset() {
    return Date.now() >= this.nextAttempt
  }

  // 设置监控
  setupMonitor() {
    return setInterval(() => {
      this.reportMetrics()
    }, this.monitoringPeriod)
  }

  // 报告指标
  reportMetrics() {
    const metrics = this.getMetrics()
    console.log(`Circuit Breaker Metrics:`, metrics)
  }

  // 获取指标
  getMetrics() {
    const { totalCalls, successCalls, failureCalls, timeouts, rejectedCalls } = this.metrics
    
    return {
      state: this.state,
      totalCalls,
      successCalls,
      failureCalls,
      timeouts,
      rejectedCalls,
      successRate: totalCalls > 0 ? (successCalls / totalCalls * 100).toFixed(2) + '%' : '0%',
      failureRate: totalCalls > 0 ? (failureCalls / totalCalls * 100).toFixed(2) + '%' : '0%',
      currentFailureCount: this.failureCount,
      lastFailureTime: this.lastFailureTime ? new Date(this.lastFailureTime).toISOString() : null
    }
  }

  // 手动打开熔断器
  open() {
    this.state = 'OPEN'
    this.nextAttempt = Date.now() + this.recoveryTimeout
    console.log('Circuit breaker manually opened')
  }

  // 手动关闭熔断器
  close() {
    this.state = 'CLOSED'
    this.failureCount = 0
    this.lastFailureTime = null
    console.log('Circuit breaker manually closed')
  }

  // 重置熔断器
  reset() {
    this.state = 'CLOSED'
    this.failureCount = 0
    this.lastFailureTime = null
    this.metrics = {
      totalCalls: 0,
      successCalls: 0,
      failureCalls: 0,
      timeouts: 0,
      rejectedCalls: 0
    }
    console.log('Circuit breaker reset')
  }

  // 销毁熔断器
  destroy() {
    if (this.monitor) {
      clearInterval(this.monitor)
    }
  }
}

// 熔断器管理器
class CircuitBreakerManager {
  constructor() {
    this.breakers = new Map()
    this.defaultOptions = {
      failureThreshold: 5,
      recoveryTimeout: 60000,
      monitoringPeriod: 10000
    }
  }

  // 获取或创建熔断器
  getBreaker(serviceName, options = {}) {
    if (this.breakers.has(serviceName)) {
      return this.breakers.get(serviceName)
    }

    const breakerOptions = { ...this.defaultOptions, ...options }
    const breaker = new CircuitBreaker(breakerOptions)
    this.breakers.set(serviceName, breaker)

    console.log(`Circuit breaker created for service: ${serviceName}`)
    return breaker
  }

  // 移除熔断器
  removeBreaker(serviceName) {
    const breaker = this.breakers.get(serviceName)
    if (breaker) {
      breaker.destroy()
      this.breakers.delete(serviceName)
      console.log(`Circuit breaker removed for service: ${serviceName}`)
    }
  }

  // 获取所有熔断器状态
  getAllBreakerStatus() {
    const status = {}
    for (const [serviceName, breaker] of this.breakers) {
      status[serviceName] = breaker.getMetrics()
    }
    return status
  }

  // 重置所有熔断器
  resetAllBreakers() {
    for (const breaker of this.breakers.values()) {
      breaker.reset()
    }
    console.log('All circuit breakers reset')
  }
}

5.2 服务限流与隔离

javascript 复制代码
// 令牌桶限流器
class TokenBucketRateLimiter {
  constructor(capacity, refillRate, refillPeriod = 1000) {
    this.capacity = capacity
    this.tokens = capacity
    this.refillRate = refillRate
    this.refillPeriod = refillPeriod
    this.lastRefill = Date.now()
    
    this.startRefilling()
  }

  // 尝试获取令牌
  async tryAcquire(tokens = 1) {
    this.refill()
    
    if (this.tokens >= tokens) {
      this.tokens -= tokens
      return true
    }
    
    return false
  }

  // 等待并获取令牌
  async acquire(tokens = 1, timeout = 5000) {
    const startTime = Date.now()
    
    while (Date.now() - startTime < timeout) {
      if (await this.tryAcquire(tokens)) {
        return true
      }
      
      // 等待一段时间再次尝试
      await this.delay(Math.min(100, timeout / 10))
    }
    
    throw new Error('Rate limiter timeout - could not acquire tokens')
  }

  // 填充令牌
  refill() {
    const now = Date.now()
    const timePassed = now - this.lastRefill
    
    if (timePassed >= this.refillPeriod) {
      const tokensToAdd = Math.floor(timePassed / this.refillPeriod) * this.refillRate
      this.tokens = Math.min(this.capacity, this.tokens + tokensToAdd)
      this.lastRefill = now
    }
  }

  // 开始自动填充
  startRefilling() {
    setInterval(() => {
      this.refill()
    }, this.refillPeriod)
  }

  // 获取当前状态
  getStatus() {
    this.refill()
    return {
      tokens: this.tokens,
      capacity: this.capacity,
      refillRate: this.refillRate,
      utilization: ((this.capacity - this.tokens) / this.capacity * 100).toFixed(2) + '%'
    }
  }

  delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms))
  }
}

// 滑动窗口限流器
class SlidingWindowRateLimiter {
  constructor(windowSize, maxRequests) {
    this.windowSize = windowSize
    this.maxRequests = maxRequests
    this.requests = []
  }

  // 检查是否允许请求
  allowRequest() {
    const now = Date.now()
    
    // 清理过期请求
    this.cleanup(now)
    
    // 检查请求数量
    if (this.requests.length < this.maxRequests) {
      this.requests.push(now)
      return true
    }
    
    return false
  }

  // 清理过期请求
  cleanup(now) {
    const cutoff = now - this.windowSize
    this.requests = this.requests.filter(time => time > cutoff)
  }

  // 获取当前状态
  getStatus() {
    const now = Date.now()
    this.cleanup(now)
    
    return {
      currentRequests: this.requests.length,
      maxRequests: this.maxRequests,
      windowSize: this.windowSize,
      utilization: (this.requests.length / this.maxRequests * 100).toFixed(2) + '%'
    }
  }
}

// 服务隔离管理器
class ServiceIsolationManager {
  constructor() {
    this.rateLimiters = new Map()
    this.resourcePools = new Map()
    this.bulkheads = new Map()
  }

  // 设置服务限流
  setRateLimit(serviceName, config) {
    const { type = 'token-bucket', ...options } = config
    
    let limiter
    switch (type) {
      case 'token-bucket':
        limiter = new TokenBucketRateLimiter(
          options.capacity || 100,
          options.refillRate || 10,
          options.refillPeriod || 1000
        )
        break
      case 'sliding-window':
        limiter = new SlidingWindowRateLimiter(
          options.windowSize || 60000,
          options.maxRequests || 100
        )
        break
      default:
        throw new Error(`Unknown rate limiter type: ${type}`)
    }

    this.rateLimiters.set(serviceName, limiter)
    console.log(`Rate limiter set for service: ${serviceName}`)
  }

  // 检查请求是否被限流
  async checkRateLimit(serviceName, tokens = 1) {
    const limiter = this.rateLimiters.get(serviceName)
    if (!limiter) {
      return true
    }

    if (limiter instanceof TokenBucketRateLimiter) {
      return await limiter.tryAcquire(tokens)
    } else if (limiter instanceof SlidingWindowRateLimiter) {
      return limiter.allowRequest()
    }

    return true
  }

  // 设置资源池隔离
  createResourcePool(serviceName, config) {
    const {
      maxConnections = 10,
      maxQueue = 50,
      acquireTimeout = 5000
    } = config

    const pool = new ResourcePool({
      maxConnections,
      maxQueue,
      acquireTimeout
    })

    this.resourcePools.set(serviceName, pool)
    console.log(`Resource pool created for service: ${serviceName}`)
  }

  // 获取资源池连接
  async acquireConnection(serviceName) {
    const pool = this.resourcePools.get(serviceName)
    if (!pool) {
      throw new Error(`No resource pool found for service: ${serviceName}`)
    }

    return await pool.acquire()
  }

  // 释放资源池连接
  releaseConnection(serviceName, connection) {
    const pool = this.resourcePools.get(serviceName)
    if (pool) {
      pool.release(connection)
    }
  }

  // 创建舱壁隔离
  createBulkhead(serviceName, config) {
    const bulkhead = new Bulkhead(config)
    this.bulkheads.set(serviceName, bulkhead)
    console.log(`Bulkhead created for service: ${serviceName}`)
  }

  // 在舱壁中执行操作
  async executeInBulkhead(serviceName, operation) {
    const bulkhead = this.bulkheads.get(serviceName)
    if (!bulkhead) {
      return await operation()
    }

    return await bulkhead.execute(operation)
  }

  // 获取所有服务的隔离状态
  getAllIsolationStatus() {
    const status = {}

    for (const [serviceName, limiter] of this.rateLimiters) {
      status[serviceName] = {
        rateLimit: limiter.getStatus()
      }
    }

    for (const [serviceName, pool] of this.resourcePools) {
      if (!status[serviceName]) status[serviceName] = {}
      status[serviceName].resourcePool = pool.getStatus()
    }

    for (const [serviceName, bulkhead] of this.bulkheads) {
      if (!status[serviceName]) status[serviceName] = {}
      status[serviceName].bulkhead = bulkhead.getStatus()
    }

    return status
  }
}

// 资源池实现
class ResourcePool {
  constructor(options) {
    this.maxConnections = options.maxConnections
    this.maxQueue = options.maxQueue
    this.acquireTimeout = options.acquireTimeout
    
    this.activeConnections = 0
    this.queue = []
    this.connections = new Set()
  }

  // 获取连接
  async acquire() {
    return new Promise((resolve, reject) => {
      const request = { resolve, reject, timestamp: Date.now() }

      // 如果有可用连接,直接分配
      if (this.activeConnections < this.maxConnections) {
        this.activeConnections++
        const connection = this.createConnection()
        this.connections.add(connection)
        resolve(connection)
        return
      }

      // 如果队列未满,加入队列
      if (this.queue.length < this.maxQueue) {
        this.queue.push(request)
        
        // 设置超时
        setTimeout(() => {
          const index = this.queue.indexOf(request)
          if (index !== -1) {
            this.queue.splice(index, 1)
            reject(new Error('Resource pool acquire timeout'))
          }
        }, this.acquireTimeout)
      } else {
        reject(new Error('Resource pool queue full'))
      }
    })
  }

  // 释放连接
  release(connection) {
    if (!this.connections.has(connection)) {
      return
    }

    this.connections.delete(connection)
    this.activeConnections--

    // 处理队列中的请求
    if (this.queue.length > 0) {
      const request = this.queue.shift()
      this.activeConnections++
      const newConnection = this.createConnection()
      this.connections.add(newConnection)
      request.resolve(newConnection)
    }
  }

  // 创建连接对象
  createConnection() {
    return {
      id: `conn_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
      createdAt: Date.now()
    }
  }

  // 获取状态
  getStatus() {
    return {
      activeConnections: this.activeConnections,
      maxConnections: this.maxConnections,
      queueLength: this.queue.length,
      maxQueue: this.maxQueue,
      utilization: (this.activeConnections / this.maxConnections * 100).toFixed(2) + '%'
    }
  }
}

// 舱壁隔离实现
class Bulkhead {
  constructor(options) {
    this.maxConcurrent = options.maxConcurrent || 10
    this.maxQueue = options.maxQueue || 50
    this.timeout = options.timeout || 30000
    
    this.activeRequests = 0
    this.queue = []
  }

  // 执行操作
  async execute(operation) {
    return new Promise((resolve, reject) => {
      const request = { 
        operation, 
        resolve, 
        reject, 
        timestamp: Date.now() 
      }

      // 如果可以直接执行
      if (this.activeRequests < this.maxConcurrent) {
        this.executeRequest(request)
        return
      }

      // 如果队列未满,加入队列
      if (this.queue.length < this.maxQueue) {
        this.queue.push(request)
        
        // 设置超时
        setTimeout(() => {
          const index = this.queue.indexOf(request)
          if (index !== -1) {
            this.queue.splice(index, 1)
            reject(new Error('Bulkhead execution timeout'))
          }
        }, this.timeout)
      } else {
        reject(new Error('Bulkhead queue full'))
      }
    })
  }

  // 执行请求
  async executeRequest(request) {
    this.activeRequests++
    
    try {
      const result = await request.operation()
      request.resolve(result)
    } catch (error) {
      request.reject(error)
    } finally {
      this.activeRequests--
      this.processQueue()
    }
  }

  // 处理队列
  processQueue() {
    if (this.queue.length > 0 && this.activeRequests < this.maxConcurrent) {
      const request = this.queue.shift()
      this.executeRequest(request)
    }
  }

  // 获取状态
  getStatus() {
    return {
      activeRequests: this.activeRequests,
      maxConcurrent: this.maxConcurrent,
      queueLength: this.queue.length,
      maxQueue: this.maxQueue,
      utilization: (this.activeRequests / this.maxConcurrent * 100).toFixed(2) + '%'
    }
  }
}

5.3 分布式监控与可观察性

javascript 复制代码
// 监控数据收集器
class MetricsCollector {
  constructor() {
    this.metrics = new Map()
    this.gauges = new Map()
    this.counters = new Map()
    this.histograms = new Map()
    this.timers = new Map()
    
    this.exporters = []
    this.collectInterval = 10000 // 10秒
    
    this.startCollection()
  }

  // 记录计数器
  incrementCounter(name, labels = {}, value = 1) {
    const key = this.createMetricKey(name, labels)
    
    if (!this.counters.has(key)) {
      this.counters.set(key, {
        name,
        labels,
        value: 0,
        timestamp: Date.now()
      })
    }

    const counter = this.counters.get(key)
    counter.value += value
    counter.timestamp = Date.now()
  }

  // 设置仪表盘
  setGauge(name, labels = {}, value) {
    const key = this.createMetricKey(name, labels)
    
    this.gauges.set(key, {
      name,
      labels,
      value,
      timestamp: Date.now()
    })
  }

  // 记录直方图
  recordHistogram(name, labels = {}, value) {
    const key = this.createMetricKey(name, labels)
    
    if (!this.histograms.has(key)) {
      this.histograms.set(key, {
        name,
        labels,
        values: [],
        count: 0,
        sum: 0
      })
    }

    const histogram = this.histograms.get(key)
    histogram.values.push(value)
    histogram.count++
    histogram.sum += value

    // 保持最近1000个值
    if (histogram.values.length > 1000) {
      histogram.values = histogram.values.slice(-1000)
    }
  }

  // 开始计时
  startTimer(name, labels = {}) {
    const key = this.createMetricKey(name, labels)
    
    this.timers.set(key, {
      name,
      labels,
      startTime: Date.now()
    })

    return {
      end: () => this.endTimer(key)
    }
  }

  // 结束计时
  endTimer(key) {
    const timer = this.timers.get(key)
    if (!timer) return

    const duration = Date.now() - timer.startTime
    this.recordHistogram(timer.name, timer.labels, duration)
    this.timers.delete(key)

    return duration
  }

  // 获取服务指标
  async getServiceMetrics(serviceName) {
    const metrics = {
      requests: this.getCounterValue(`service_requests_total`, { service: serviceName }),
      errors: this.getCounterValue(`service_errors_total`, { service: serviceName }),
      duration: this.getHistogramStats(`service_request_duration`, { service: serviceName }),
      cpu: this.getGaugeValue(`service_cpu_usage`, { service: serviceName }),
      memory: this.getGaugeValue(`service_memory_usage`, { service: serviceName })
    }

    // 计算派生指标
    metrics.errorRate = metrics.requests > 0 ? 
      (metrics.errors / metrics.requests * 100).toFixed(2) + '%' : '0%'
    
    metrics.avgCPUUtilization = metrics.cpu || 0
    metrics.avgMemoryUtilization = metrics.memory || 0

    return metrics
  }

  // 获取计数器值
  getCounterValue(name, labels = {}) {
    const key = this.createMetricKey(name, labels)
    const counter = this.counters.get(key)
    return counter ? counter.value : 0
  }

  // 获取仪表盘值
  getGaugeValue(name, labels = {}) {
    const key = this.createMetricKey(name, labels)
    const gauge = this.gauges.get(key)
    return gauge ? gauge.value : null
  }

  // 获取直方图统计
  getHistogramStats(name, labels = {}) {
    const key = this.createMetricKey(name, labels)
    const histogram = this.histograms.get(key)
    
    if (!histogram || histogram.values.length === 0) {
      return null
    }

    const sortedValues = [...histogram.values].sort((a, b) => a - b)
    
    return {
      count: histogram.count,
      sum: histogram.sum,
      avg: histogram.sum / histogram.count,
      min: sortedValues[0],
      max: sortedValues[sortedValues.length - 1],
      p50: this.percentile(sortedValues, 0.5),
      p90: this.percentile(sortedValues, 0.9),
      p95: this.percentile(sortedValues, 0.95),
      p99: this.percentile(sortedValues, 0.99)
    }
  }

  // 计算百分位数
  percentile(sortedValues, p) {
    const index = Math.ceil(sortedValues.length * p) - 1
    return sortedValues[Math.max(0, index)]
  }

  // 创建指标键
  createMetricKey(name, labels) {
    const labelStr = Object.entries(labels)
      .sort(([a], [b]) => a.localeCompare(b))
      .map(([k, v]) => `${k}="${v}"`)
      .join(',')
    
    return `${name}{${labelStr}}`
  }

  // 添加导出器
  addExporter(exporter) {
    this.exporters.push(exporter)
  }

  // 开始收集
  startCollection() {
    setInterval(async () => {
      await this.collectAndExport()
    }, this.collectInterval)
  }

  // 收集并导出指标
  async collectAndExport() {
    const allMetrics = {
      counters: Array.from(this.counters.values()),
      gauges: Array.from(this.gauges.values()),
      histograms: Array.from(this.histograms.entries()).map(([key, histogram]) => ({
        key,
        ...histogram,
        stats: this.getHistogramStats(histogram.name, histogram.labels)
      })),
      timestamp: Date.now()
    }

    // 导出到所有注册的导出器
    for (const exporter of this.exporters) {
      try {
        await exporter.export(allMetrics)
      } catch (error) {
        console.error('Metrics export failed:', error)
      }
    }
  }
}

// 分布式链路追踪
class DistributedTracer {
  constructor() {
    this.traces = new Map()
    this.activeSpans = new Map()
    this.samplingRate = 0.1 // 10%采样率
  }

  // 开始新的链路追踪
  startTrace(operationName, parentSpan = null) {
    // 采样决策
    if (Math.random() > this.samplingRate && !parentSpan) {
      return new NoOpSpan()
    }

    const traceId = parentSpan ? parentSpan.traceId : this.generateTraceId()
    const spanId = this.generateSpanId()
    
    const span = new TraceSpan({
      traceId,
      spanId,
      parentSpanId: parentSpan ? parentSpan.spanId : null,
      operationName,
      startTime: Date.now(),
      tracer: this
    })

    // 如果是根Span,创建Trace
    if (!parentSpan) {
      this.traces.set(traceId, {
        traceId,
        spans: new Map(),
        startTime: Date.now(),
        status: 'active'
      })
    }

    // 添加Span到Trace
    const trace = this.traces.get(traceId)
    if (trace) {
      trace.spans.set(spanId, span)
    }

    this.activeSpans.set(spanId, span)

    return span
  }

  // 完成Span
  finishSpan(span) {
    if (!span || span.finished) return

    span.finished = true
    span.endTime = Date.now()
    span.duration = span.endTime - span.startTime

    this.activeSpans.delete(span.spanId)

    // 检查Trace是否完成
    const trace = this.traces.get(span.traceId)
    if (trace) {
      const activeSpans = Array.from(trace.spans.values())
        .filter(s => !s.finished)
      
      if (activeSpans.length === 0) {
        trace.status = 'completed'
        trace.endTime = Date.now()
        this.exportTrace(trace)
      }
    }
  }

  // 注入追踪上下文
  inject(span, carrier) {
    if (!span || span instanceof NoOpSpan) {
      return
    }

    carrier['X-Trace-Id'] = span.traceId
    carrier['X-Span-Id'] = span.spanId
  }

  // 提取追踪上下文
  extract(carrier) {
    const traceId = carrier['X-Trace-Id']
    const spanId = carrier['X-Span-Id']

    if (!traceId || !spanId) {
      return null
    }

    return {
      traceId,
      spanId
    }
  }

  // 导出链路数据
  async exportTrace(trace) {
    const traceData = {
      traceId: trace.traceId,
      startTime: trace.startTime,
      endTime: trace.endTime,
      duration: trace.endTime - trace.startTime,
      spans: Array.from(trace.spans.values()).map(span => ({
        spanId: span.spanId,
        parentSpanId: span.parentSpanId,
        operationName: span.operationName,
        startTime: span.startTime,
        endTime: span.endTime,
        duration: span.duration,
        tags: span.tags,
        logs: span.logs,
        status: span.status
      }))
    }

    console.log('Trace exported:', JSON.stringify(traceData, null, 2))
  }

  // 生成Trace ID
  generateTraceId() {
    return 'trace_' + Date.now() + '_' + Math.random().toString(36).substr(2, 16)
  }

  // 生成Span ID
  generateSpanId() {
    return 'span_' + Math.random().toString(36).substr(2, 16)
  }
}

// 追踪Span
class TraceSpan {
  constructor(options) {
    this.traceId = options.traceId
    this.spanId = options.spanId
    this.parentSpanId = options.parentSpanId
    this.operationName = options.operationName
    this.startTime = options.startTime
    this.tracer = options.tracer
    
    this.endTime = null
    this.duration = null
    this.finished = false
    this.tags = new Map()
    this.logs = []
    this.status = 'active'
  }

  // 设置标签
  setTag(key, value) {
    this.tags.set(key, value)
    return this
  }

  // 记录日志
  log(fields) {
    this.logs.push({
      timestamp: Date.now(),
      fields
    })
    return this
  }

  // 设置状态
  setStatus(status) {
    this.status = status
    return this
  }

  // 完成Span
  finish() {
    this.tracer.finishSpan(this)
  }

  // 创建子Span
  startChild(operationName) {
    return this.tracer.startTrace(operationName, this)
  }
}

// 空操作Span(未采样)
class NoOpSpan {
  setTag() { return this }
  log() { return this }
  setStatus() { return this }
  finish() { }
  startChild(operationName) { 
    return new NoOpSpan() 
  }
}
相关推荐
崔庆才丨静觅5 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60616 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了6 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅6 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅7 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅7 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment7 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅7 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊7 小时前
jwt介绍
前端
爱敲代码的小鱼7 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax