在Debian上利用Swagger进行API监控的完整流程
确保系统已更新并安装基础工具:
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl git build-essential
Swagger UI及配套工具需Node.js环境,通过NodeSource仓库安装稳定版:
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
node -v # 验证安装(需输出v18.x及以上版本)
npm -v
mkdir debian-swagger-api && cd debian-swagger-api
npm init -y
npm install --save express body-parser
创建app.js作为主应用文件:
const express = require('express');
const bodyParser = require('body-parser');
const swaggerUi = require('swagger-ui-express');
const swaggerJsdoc = require('swagger-jsdoc');
const app = express();
app.use(bodyParser.json());
// Swagger配置(定义API元数据)
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Debian Swagger API',
version: '1.0.0',
description: 'API监控演示项目'
},
servers: [{ url: 'http://localhost:3000' }]
},
apis: ['./routes/*.js'] // 指向API路由文件(需自定义)
};
const specs = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
// 示例API路由(需在routes目录下创建userRoutes.js)
app.listen(3000, () => console.log('API运行于 http://localhost:3000'));
创建routes/userRoutes.js,用JSDoc格式描述接口:
/**
* @swagger
* /users:
* get:
* summary: 获取所有用户
* description: 返回用户列表
* responses:
* 200:
* description: 成功返回用户数组
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/User'
* components:
* schemas:
* User:
* type: object
* properties:
* id:
* type: integer
* example: 1
* name:
* type: string
* example: John Doe
*/
const express = require('express');
const router = express.Router();
router.get('/users', (req, res) => {
res.json([{ id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Smith' }]);
});
module.exports = router;
在app.js中引入路由:
const userRoutes = require('./routes/userRoutes');
app.use('/api', userRoutes); // 接口前缀为/api
node app.js
打开浏览器访问http://localhost:3000/api-docs,即可看到Swagger UI界面,包含接口文档、测试功能(可直接发送请求验证接口)。
wget https://github.com/prometheus/prometheus/releases/download/v2.48.1/prometheus-2.48.1.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*
./prometheus --config.file=prometheus.yml # 默认监听9090端口
sudo apt install -y apt-transport-https software-properties-common wget
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt update
sudo apt install -y grafana
sudo systemctl start grafana-server # 默认监听3000端口
sudo systemctl enable grafana-server
prometheus.yml,添加应用端点:scrape_configs:
- job_name: 'debian_swagger_api'
static_configs:
- targets: ['localhost:3000'] # 应用暴露的/metrics端点(需自定义)
注:需在Express应用中集成
prom-client库,暴露/metrics端点(参考Prometheus Node.js客户端文档)。
若需定期扫描API健康状态,可使用APIDetector工具:
git clone https://github.com/brinhosa/apidetector.git
cd apidetector
pip3 install -r requirements.txt
# 扫描单域名(输出到result.txt)
python3 apidetector.py -d example.com -o result.txt
扫描结果包含接口状态码、响应时间等指标,可用于后续分析。
morgan中间件记录请求日志(添加到app.js):const morgan = require('morgan');
app.use(morgan('combined')); // 日志格式为Apache标准格式
日志默认输出到终端,可通过pm2或syslog转发到集中式日志服务器(如ELK Stack)。MiniProfiler监控接口性能(适用于Node.js项目):npm install --save mini-profiler
参考MiniProfiler Node.js文档配置,生成性能报告。通过以上步骤,可在Debian上完成Swagger API的部署、文档生成及监控,覆盖从开发到运维的全流程需求。