JavaScript微服务架构详解 🏗️
今天,让我们深入了解JavaScript的微服务架构,这是构建大规模分布式系统的关键技术。
微服务基础概念 🌟
基本微服务实现 📊
javascript">// 1. 服务注册中心
class ServiceRegistry {constructor() {this.services = new Map();this.healthChecks = new Map();}register(serviceName, instance) {if (!this.services.has(serviceName)) {this.services.set(serviceName, new Set());}this.services.get(serviceName).add(instance);this.setupHealthCheck(serviceName, instance);console.log(`Service ${serviceName} registered: ${instance.url}`);}deregister(serviceName, instance) {if (this.services.has(serviceName)) {this.services.get(serviceName).delete(instance);this.healthChecks.delete(instance.url);console.log(`Service ${serviceName} deregistered: ${instance.url}`);}}getInstances(serviceName) {return Array.from(this.services.get(serviceName) || []);}setupHealthCheck(serviceName, instance) {const healthCheck = setInterval(async () => {try {const response = await fetch(`${instance.url}/health`);if (!response.ok) {throw new Error('Health check failed');}} catch (error) {console.error(`Health check failed for ${instance.url}:`, error);this.deregister(serviceName, instance);}}, 30000); // 每30秒检查一次this.healthChecks.set(instance.url, healthCheck);}
}// 2. 负载均衡器
class LoadBalancer {constructor() {this.algorithms = {'round-robin': this.roundRobin.bind(this),'random': this.random.bind(this),'least-connections': this.leastConnections.bind(this)};this.currentIndex = 0;this.connectionCounts = new Map();}roundRobin(instances) {if (instances.length === 0) return null;const instance = instances[this.currentIndex];this.currentIndex = (this.currentIndex + 1) % instances.length;return instance;}random(instances) {if (instances.length === 0) return null;const randomIndex = Math.floor(Math.random() * instances.length);return instances[randomIndex];}leastConnections(instances) {if (instances.length === 0) return null;let minConnections = Infinity;let selectedInstance = null;for (const instance of instances) {const connections = this.connectionCounts.get(instance.url) || 0;if (connections < minConnections) {minConnections = connections;selectedInstance = instance;}}return selectedInstance;}incrementConnections(instance) {const current = this.connectionCounts.get(instance.url) || 0;this.connectionCounts.set(instance.url, current + 1);}decrementConnections(instance) {const current = this.connectionCounts.get(instance.url) || 0;if (current > 0) {this.connectionCounts.set(instance.url, current - 1);}}
}// 3. API网关
class APIGateway {constructor(serviceRegistry, loadBalancer) {this.serviceRegistry = serviceRegistry;this.loadBalancer = loadBalancer;this.middlewares = [];}use(middleware) {this.middlewares.push(middleware);}async handleRequest(req) {// 执行中间件for (const middleware of this.middlewares) {await middleware(req);}const serviceName = this.extractServiceName(req.path);const instances = this.serviceRegistry.getInstances(serviceName);if (instances.length === 0) {throw new Error(`No instances available for service: ${serviceName}`);}const instance = this.loadBalancer.roundRobin(instances);return this.forwardRequest(instance, req);}extractServiceName(path) {// 从路径中提取服务名return path.split('/')[1];}async forwardRequest(instance, req) {this.loadBalancer.incrementConnections(instance);try {const response = await fetch(`${instance.url}${req.path}`, {method: req.method,headers: req.headers,body: req.body});return response;} finally {this.loadBalancer.decrementConnections(instance);}}
}
高级微服务模式 🚀
javascript">// 1. 断路器模式
class CircuitBreaker {constructor(service, options = {}) {this.service = service;this.options = {failureThreshold: 5,resetTimeout: 60000,...options};this.state = 'CLOSED';this.failures = 0;this.lastFailureTime = null;}async execute(request) {if (this.state === 'OPEN') {if (this.shouldReset()) {this.halfOpen();} else {throw new Error('Circuit breaker is OPEN');}}try {const response = await this.service.execute(request);this.onSuccess();return response;} catch (error) {this.onFailure();throw error;}}onSuccess() {this.failures = 0;this.state = 'CLOSED';}onFailure() {this.failures++;this.lastFailureTime = Date.now();if (this.failures >= this.options.failureThreshold) {this.state = 'OPEN';}}shouldReset() {return Date.now() - this.lastFailureTime >= this.options.resetTimeout;}halfOpen() {this.state = 'HALF-OPEN';this.failures = 0;}
}// 2. 服务网格
class ServiceMesh {constructor() {this.proxies = new Map();this.metrics = new Map();}addProxy(serviceName, instance) {const proxy = new ServiceProxy(instance);if (!this.proxies.has(serviceName)) {this.proxies.set(serviceName, new Set());}this.proxies.get(serviceName).add(proxy);this.initializeMetrics(proxy);return proxy;}initializeMetrics(proxy) {this.metrics.set(proxy, {requests: 0,errors: 0,latency: []});}async routeRequest(serviceName, request) {const proxySet = this.proxies.get(serviceName);if (!proxySet || proxySet.size === 0) {throw new Error(`No proxies available for service: ${serviceName}`);}const proxy = Array.from(proxySet)[0]; // 简单选择第一个代理const metrics = this.metrics.get(proxy);const startTime = Date.now();metrics.requests++;try {const response = await proxy.forward(request);metrics.latency.push(Date.now() - startTime);return response;} catch (error) {metrics.errors++;throw error;}}getMetrics(serviceName) {const proxySet = this.proxies.get(serviceName);if (!proxySet) return null;const serviceMetrics = {totalRequests: 0,totalErrors: 0,averageLatency: 0};for (const proxy of proxySet) {const metrics = this.metrics.get(proxy);serviceMetrics.totalRequests += metrics.requests;serviceMetrics.totalErrors += metrics.errors;if (metrics.latency.length > 0) {const sum = metrics.latency.reduce((a, b) => a + b, 0);serviceMetrics.averageLatency += sum / metrics.latency.length;}}return serviceMetrics;}
}// 3. 分布式追踪
class DistributedTracer {constructor() {this.traces = new Map();this.spans = new Map();}startTrace(traceId = this.generateTraceId()) {const trace = {id: traceId,startTime: Date.now(),spans: []};this.traces.set(traceId, trace);return traceId;}startSpan(traceId, spanId = this.generateSpanId(), parentSpanId = null) {const span = {id: spanId,traceId,parentSpanId,startTime: Date.now(),events: []};this.spans.set(spanId, span);this.traces.get(traceId).spans.push(span);return spanId;}addEvent(spanId, event) {const span = this.spans.get(spanId);if (span) {span.events.push({timestamp: Date.now(),...event});}}endSpan(spanId) {const span = this.spans.get(spanId);if (span) {span.endTime = Date.now();span.duration = span.endTime - span.startTime;}}endTrace(traceId) {const trace = this.traces.get(traceId);if (trace) {trace.endTime = Date.now();trace.duration = trace.endTime - trace.startTime;return this.generateTraceReport(trace);}}generateTraceReport(trace) {return {traceId: trace.id,duration: trace.duration,spans: trace.spans.map(span => ({id: span.id,parentId: span.parentSpanId,duration: span.duration,events: span.events}))};}generateTraceId() {return `trace-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;}generateSpanId() {return `span-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;}
}
性能优化技巧 ⚡
javascript">// 1. 缓存管理器
class CacheManager {constructor(options = {}) {this.options = {maxSize: 1000,ttl: 3600000, // 1小时...options};this.cache = new Map();this.accessLog = new Map();}set(key, value, ttl = this.options.ttl) {this.ensureCapacity();const entry = {value,expires: Date.now() + ttl};this.cache.set(key, entry);this.accessLog.set(key, Date.now());}get(key) {const entry = this.cache.get(key);if (!entry) return null;if (Date.now() > entry.expires) {this.cache.delete(key);this.accessLog.delete(key);return null;}this.accessLog.set(key, Date.now());return entry.value;}ensureCapacity() {if (this.cache.size >= this.options.maxSize) {// 删除最少访问的条目const entries = Array.from(this.accessLog.entries());entries.sort((a, b) => a[1] - b[1]);const keyToDelete = entries[0][0];this.cache.delete(keyToDelete);this.accessLog.delete(keyToDelete);}}clear() {this.cache.clear();this.accessLog.clear();}
}// 2. 请求合并器
class RequestBatcher {constructor(options = {}) {this.options = {maxBatchSize: 100,maxDelay: 50,...options};this.batch = [];this.timer = null;}async add(request) {return new Promise((resolve, reject) => {this.batch.push({request,resolve,reject});if (this.batch.length >= this.options.maxBatchSize) {this.flush();} else if (!this.timer) {this.timer = setTimeout(() => this.flush(), this.options.maxDelay);}});}async flush() {if (this.batch.length === 0) return;const currentBatch = this.batch;this.batch = [];if (this.timer) {clearTimeout(this.timer);this.timer = null;}try {const results = await this.processBatch(currentBatch);currentBatch.forEach((item, index) => {item.resolve(results[index]);});} catch (error) {currentBatch.forEach(item => {item.reject(error);});}}async processBatch(batch) {// 实现批处理逻辑return Promise.all(batch.map(item => this.processRequest(item.request)));}async processRequest(request) {// 实现单个请求处理逻辑return request;}
}// 3. 性能监控器
class PerformanceMonitor {constructor() {this.metrics = {requestCount: 0,errorCount: 0,responseTime: [],cpuUsage: [],memoryUsage: []};this.startMonitoring();}startMonitoring() {setInterval(() => {this.collectMetrics();}, 5000); // 每5秒收集一次}collectMetrics() {const metrics = process.metrics();this.metrics.cpuUsage.push({timestamp: Date.now(),value: metrics.cpu.usage});this.metrics.memoryUsage.push({timestamp: Date.now(),value: process.memoryUsage().heapUsed});// 保持最近1小时的数据this.pruneMetrics();}recordRequest(duration, isError = false) {this.metrics.requestCount++;if (isError) this.metrics.errorCount++;this.metrics.responseTime.push({timestamp: Date.now(),value: duration});}pruneMetrics() {const oneHourAgo = Date.now() - 3600000;['cpuUsage', 'memoryUsage', 'responseTime'].forEach(metric => {this.metrics[metric] = this.metrics[metric].filter(item => item.timestamp > oneHourAgo);});}getMetrics() {return {requestCount: this.metrics.requestCount,errorCount: this.metrics.errorCount,errorRate: this.metrics.errorCount / this.metrics.requestCount,averageResponseTime: this.calculateAverage(this.metrics.responseTime),averageCpuUsage: this.calculateAverage(this.metrics.cpuUsage),averageMemoryUsage: this.calculateAverage(this.metrics.memoryUsage)};}calculateAverage(metrics) {if (metrics.length === 0) return 0;const sum = metrics.reduce((acc, item) => acc + item.value, 0);return sum / metrics.length;}
}
最佳实践建议 💡
- 服务设计原则
javascript">// 1. 服务隔离
class ServiceIsolation {constructor(service) {this.service = service;this.circuitBreaker = new CircuitBreaker(service);this.bulkhead = new Bulkhead(10); // 限制并发请求数}async execute(request) {return this.bulkhead.execute(() => this.circuitBreaker.execute(request));}
}// 2. 服务发现
class ServiceDiscovery {constructor(registry) {this.registry = registry;this.cache = new Map();this.cacheTimeout = 30000; // 30秒缓存}async getService(name) {const cached = this.cache.get(name);if (cached && Date.now() - cached.timestamp < this.cacheTimeout) {return cached.service;}const service = await this.registry.lookup(name);this.cache.set(name, {service,timestamp: Date.now()});return service;}
}// 3. 配置管理
class ConfigurationManager {constructor() {this.configs = new Map();this.watchers = new Map();}set(key, value) {this.configs.set(key, value);this.notifyWatchers(key, value);}get(key) {return this.configs.get(key);}watch(key, callback) {if (!this.watchers.has(key)) {this.watchers.set(key, new Set());}this.watchers.get(key).add(callback);}notifyWatchers(key, value) {const watchers = this.watchers.get(key);if (watchers) {watchers.forEach(callback => callback(value));}}
}
结语 📝
微服务架构为构建大规模分布式系统提供了强大的支持。通过本文,我们学习了:
💡 学习建议:在实践微服务架构时,要特别注意服务的隔离性和容错性。合理使用断路器、服务发现等模式,可以显著提升系统的可靠性和可维护性。
如果你觉得这篇文章有帮助,欢迎点赞收藏,也期待在评论区看到你的想法和建议!👇
终身学习,共同成长。
咱们下一期见
💻