一、Debian上Swagger工具链搭建步骤
首先更新系统并安装必要工具,确保后续步骤顺利执行:
sudo apt update && sudo apt upgrade -y
sudo apt install -y git nodejs npm openjdk-11-jdk maven
验证安装结果:
node -v && npm -v && java -version && mvn -v
全局安装Swagger UI和Swagger命令行工具:
sudo npm install -g swagger-ui-express swagger-jsdoc swagger-editor-cli
在pom.xml中添加Springfox依赖,用于生成Swagger文档:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
在项目根目录创建swagger.yaml(推荐YAML格式,可读性更强):
openapi: 3.0.0
info:
title: Debian Swagger Demo API
version: 1.0.0
description: API for managing Debian-based services
servers:
- url: http://localhost:8080/api
description: Local development server
paths:
/health:
get:
summary: Check service health
responses:
'200':
description: Service is healthy
content:
application/json:
schema:
type: object
properties:
status:
type: string
example: UP
创建Swagger配置类,自动扫描接口并生成文档:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.debian.swagger.controller")) // 替换为你的控制器包路径
.paths(PathSelectors.any())
.build();
}
}
在Express应用中引入Swagger UI,提供文档访问接口:
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const app = express();
const swaggerDocument = YAML.load('./swagger.yaml'); // 加载规范文件
// 集成Swagger UI到/api-docs路径
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
console.log(`Swagger UI available at http://localhost:${PORT}/api-docs`);
});
启动Spring Boot应用,Swagger UI默认在http://localhost:8080/swagger-ui.html可用。
http://localhost:3000/api-docs,查看交互式API文档。http://localhost:8080/swagger-ui.html,验证接口是否正确显示。二、Debian上Swagger工具链优化策略
若使用Spring Boot集成Swagger,优化JVM配置可显著提升性能:
# 在启动脚本中添加以下参数(示例)
export JAVA_OPTS="-Xms512m -Xmx2048m -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
-Xms512m:初始堆内存512MB;-Xmx2048m:最大堆内存2GB;-XX:+UseG1GC:使用G1垃圾回收器(适合大内存应用);-XX:MaxGCPauseMillis=200:设置最大GC停顿时间为200ms。swagger.yaml拆分为多个子文件(如按模块划分),通过$ref引用,减少单个文件复杂度。location /api-docs {
alias /path/to/swagger-ui-dist/;
expires 1h; # 缓存1小时
add_header Cache-Control "public";
}
// Spring Boot示例:使用RedisTemplate缓存文档
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.debian.swagger.controller"))
.paths(PathSelectors.any())
.build()
.cacheResponses(Duration.ofMinutes(10)); // 缓存10分钟
}
worker_processes(CPU核心数)和worker_connections(每个进程的最大连接数):worker_processes auto;
events {
worker_connections 1024;
}
upstream swagger_servers {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
}
server {
listen 80;
server_name api.example.com;
location /api-docs {
proxy_pass http://swagger_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
location /api-docs {
allow 192.168.1.0/24; # 允许内网IP
deny all; # 拒绝其他IP
proxy_pass http://localhost:3000;
}
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d api.example.com