在 Debian 上跑 Swagger(无论是 Swagger UI、Swagger Editor,还是基于 Springfox / springdoc-openapi 的后端接口)变慢,通常可以从网络、资源、配置、运行环境几个方向排查和优化。下面按常见场景给你一套实用方案。
先区分是:
/v2/api-docs 或 /v3/api-docs 生成慢# 看接口生成耗时
curl -w "%{time_total}\n" http://localhost:8080/v3/api-docs
Swagger UI 默认从 unpkg、jsdelivr 加载 JS/CSS。
✅ 解决方案:本地化 Swagger UI
# 下载 swagger-ui
wget https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.x.x.zip
然后在 Nginx 或 Spring 中指向本地静态文件。
Nginx 示例:
location /swagger-ui/ {
alias /opt/swagger-ui/dist/;
}
Swagger UI 会请求外部资源,DNS 慢会卡死。
cat /etc/resolv.conf
✅ 建议:
nameserver 223.5.5.5
nameserver 8.8.8.8
然后:
systemctl restart networking
Springfox / springdoc 会扫描全部 Bean。
✅ 限制扫描路径:
springdoc:
packages-to-scan: com.example.controller
或 Springfox:
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example"))
.build();
}
}
Swagger 会深度反射,非常慢。
❌ 避免:
Map<String, Object>
List<?>
✅ 用明确 DTO
Debian 默认 JVM 参数保守。
java -Xms512m -Xmx2g -jar app.jar
查看:
free -h
top
ulimit -n
修改:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
sysctl -w net.ipv6.conf.all.disable_ipv6=1
location /v3/api-docs {
proxy_pass http://127.0.0.1:8080;
proxy_cache swagger_cache;
}
✅ 生产环境建议直接关闭 Swagger
springdoc:
swagger-ui:
enabled: false
或:
@Profile("!prod")
如果你能告诉我:
我可以直接给你一份针对性配置。