删除废弃的API和架构文档
This commit is contained in:
108
docs/development/examples/performance-monitor-integration.js
Normal file
108
docs/development/examples/performance-monitor-integration.js
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* 性能监控系统集成示例
|
||||
* @file performance-monitor-integration.js
|
||||
* @description 演示如何在Express应用中集成性能监控系统
|
||||
*/
|
||||
const express = require('express');
|
||||
const { initPerformanceMonitoring, getPerformanceRoutes } = require('../backend/config/performance-config');
|
||||
const logger = require('../backend/utils/logger');
|
||||
|
||||
// 创建Express应用
|
||||
const app = express();
|
||||
|
||||
// 配置中间件
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// 初始化性能监控系统
|
||||
const performanceMonitor = initPerformanceMonitoring(app, {
|
||||
autoStart: true,
|
||||
interval: 30000, // 30秒监控间隔
|
||||
logToConsole: true,
|
||||
thresholds: {
|
||||
system: {
|
||||
cpuUsage: 75, // CPU使用率阈值(百分比)
|
||||
memoryUsage: 80, // 内存使用率阈值(百分比)
|
||||
diskUsage: 85 // 磁盘使用率阈值(百分比)
|
||||
},
|
||||
database: {
|
||||
connectionPoolUtilization: 70, // 连接池利用率阈值(百分比)
|
||||
slowQueryCount: 3, // 慢查询数量阈值
|
||||
errorRate: 2 // 错误率阈值(百分比)
|
||||
},
|
||||
api: {
|
||||
responseTime: 300, // API响应时间阈值(毫秒)
|
||||
errorRate: 1, // API错误率阈值(百分比)
|
||||
requestRate: 500 // 每分钟请求数阈值
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 注册性能监控路由
|
||||
app.use('/api/performance', getPerformanceRoutes());
|
||||
|
||||
// 示例API路由
|
||||
app.get('/api/users', (req, res) => {
|
||||
// 模拟数据库操作延迟
|
||||
setTimeout(() => {
|
||||
res.json({ users: [{ id: 1, name: '张三' }, { id: 2, name: '李四' }] });
|
||||
}, 50);
|
||||
});
|
||||
|
||||
app.get('/api/products', (req, res) => {
|
||||
// 模拟数据库操作延迟
|
||||
setTimeout(() => {
|
||||
res.json({ products: [{ id: 1, name: '产品A' }, { id: 2, name: '产品B' }] });
|
||||
}, 30);
|
||||
});
|
||||
|
||||
// 模拟慢响应API
|
||||
app.get('/api/reports', (req, res) => {
|
||||
// 模拟耗时操作
|
||||
setTimeout(() => {
|
||||
res.json({ report: { id: 1, data: '报表数据...' } });
|
||||
}, 600); // 超过阈值的响应时间
|
||||
});
|
||||
|
||||
// 模拟错误API
|
||||
app.get('/api/error', (req, res) => {
|
||||
res.status(500).json({ error: '服务器内部错误' });
|
||||
});
|
||||
|
||||
// 错误处理中间件
|
||||
app.use((err, req, res, next) => {
|
||||
logger.error('应用错误:', err);
|
||||
res.status(500).json({ error: '服务器内部错误', message: err.message });
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => {
|
||||
logger.info(`服务器已启动,监听端口 ${PORT}`);
|
||||
logger.info('性能监控系统已集成');
|
||||
|
||||
// 输出性能监控访问信息
|
||||
logger.info('性能监控API:');
|
||||
logger.info('- 获取所有指标: GET /api/performance/metrics');
|
||||
logger.info('- 获取系统指标: GET /api/performance/system');
|
||||
logger.info('- 获取数据库指标: GET /api/performance/database');
|
||||
logger.info('- 获取API指标: GET /api/performance/api');
|
||||
logger.info('- 获取监控状态: GET /api/performance/status');
|
||||
});
|
||||
|
||||
// 优雅关闭
|
||||
process.on('SIGTERM', shutdown);
|
||||
process.on('SIGINT', shutdown);
|
||||
|
||||
function shutdown() {
|
||||
logger.info('正在关闭应用...');
|
||||
|
||||
// 停止性能监控
|
||||
performanceMonitor.stopMonitoring();
|
||||
|
||||
// 关闭服务器
|
||||
server.close(() => {
|
||||
logger.info('HTTP服务器已关闭');
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user