SpringCloud网关聚合knife4j方案

embedded/2024/10/18 0:04:13/

微服务开发中想将Spring-Cloud-Gateway网关聚合knife4j,形成一个统一入口方便查阅的开发辅助接口文档,并且将Swagger抽取成一个公共模块,那么我们可以参考以下的做法

约定:

Java Version:11.0.24

Spring Boot:2.7.18

knife4j:4.4.0

Swagger公共模块抽取

依赖

java"><dependencies><!-- SpringBoot Web (支持构建Web应用程序,包括Spring MVC和内嵌的Servlet容器) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Knife4j API 小刀注解依赖 --><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi2-spring-boot-starter</artifactId></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version></dependency><!-- SpringBoot Actuator (支持集成Actuator,用于监控和管理应用程序) --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Java Bean Validation API --><dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId><version>2.0.1.Final</version></dependency><!-- Lombok 辅助器 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
</dependencies>

定义配置Swagger Properties

java">package com.if010.common.swagger.properties;import lombok.*;
import org.springframework.boot.context.properties.ConfigurationProperties;import java.io.Serializable;/*** Swagger Properties配置信息实体类* @author Kim同学*/@Data
@NoArgsConstructor
@ToString
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfigProperties implements Serializable {//配置 API (com.xxx.controller) 扫描路径public String controllerPath;//配置 API 文档标题public String title;//配置 API 版本信息public String version;//配置 API 文档描述public String description;//配置 API 文档许可 描述 或 协议public String license;//配置 API 文档许可 描述 或 协议 的 URLpublic String licenseUrl;//配置 API 文档 联系人 信息public String contactName;//配置 API 文档 联系人 主页 URLpublic String contactUrl;//配置 API 文档 联系人 邮箱public String contactEmail;
}

定义配置Swagger Config

java">package com.if010.common.swagger.config;import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.if010.common.swagger.properties.SwaggerConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;import java.util.ArrayList;
import java.util.Collection;
import java.util.List;/*** Swagger 配置类* @author Kim同学*/@Slf4j
@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j
@ConditionalOnClass({Docket.class, ApiInfoBuilder.class})
@EnableConfigurationProperties(SwaggerConfigProperties.class)
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {@AutowiredSwaggerConfigProperties swaggerConfigProperties;private ApiInfo apiInfoBuilder() {ApiInfo apiInfoBuild = new ApiInfoBuilder()// api文档名称.title(swaggerConfigProperties.getTitle())// api文档描述.description(swaggerConfigProperties.getDescription())// api文档版本.version(swaggerConfigProperties.getVersion())// api作者信息.contact(new Contact(swaggerConfigProperties.getContactName(),swaggerConfigProperties.getContactUrl(),swaggerConfigProperties.getContactEmail()))// api文档许信息.license(swaggerConfigProperties.getLicense())// api文档许信息链接.licenseUrl(swaggerConfigProperties.getLicenseUrl()).build();return apiInfoBuild;}@Bean(value = "defaultApi2")@ConditionalOnClass(SwaggerConfigProperties.class)public Docket defaultApi2() {log.info("swaggerInfo:{}", swaggerConfigProperties.getControllerPath());// 构建API文档  文档类型为swagger2return new Docket(DocumentationType.SWAGGER_2).select()// 配置 API 扫描路径 以及 过滤规则(any代表所有路径).apis(RequestHandlerSelectors.basePackage(swaggerConfigProperties.getControllerPath())).paths(PathSelectors.any())// 配置 API 的基本信息.build().apiInfo(apiInfoBuilder());}/*** 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题**/@Beanpublic WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,ServletEndpointsSupplier servletEndpointsSupplier,ControllerEndpointsSupplier controllerEndpointsSupplier,EndpointMediaTypes endpointMediaTypes,CorsEndpointProperties corsProperties,WebEndpointProperties webEndpointProperties,Environment environment) {List<ExposableEndpoint<?>> allEndpoints = new ArrayList();Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();allEndpoints.addAll(webEndpoints);allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());String basePath = webEndpointProperties.getBasePath();EndpointMapping endpointMapping = new EndpointMapping(basePath);boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(),new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);}private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));}
}

因为是starter模块,可能他人的项目目录和starter模块的目录不一致,导致加载不到SwaggerConfig类,我们需要使用spring.factories把SwaggerConfig类装载到Spring容器,在resources/META-INF/spring添加org.springframework.boot.autoconfigure.AutoConfiguration.imports文件

SwaggerAutoConfiguration

到目前为止,Swagger的抽取工作就已经算是全部完成了,接下来我们就可以在业务模块中引入进行测试使用了

引用

首先我们需要将Swagger公共依赖添加到业务模块的pom文件当中

java"><dependencies><!-- If010 Common Swagger --><dependency><groupId>com.if010</groupId><artifactId>if010-common-swagger</artifactId></dependency>
</dependencies>

接下来,在application.yml定义Swagger的配置信息

swagger:# 配置 API 扫描路径controllerPath: com.if010.system.controller# 配置 API 文档标题title: 系统管理模块接口文档# 配置 API 版本信息version: 1.0.0# 配置 API 文档描述description: 系统管理模块# 配置 API 文档许可 描述license: Copyright © 2018 If010工作室™# 配置 API 文档许可 描述 URLlicenseUrl: https://if010.com# 配置 API 文档 联系人contact-name: Kim同学# 配置 API 文档 联系人 主页 URLcontact-url: www.if010.com# 配置 API 文档 联系人 邮箱contact-email: kim@if010.com

到此结束,紧接着我们就可以启动业务服务模块,访问:http://{service.host}:{service.port}/doc.html,看看效果啦~~

接口文档页面结果

网关聚合knife4j

依赖

java"><dependencies># 网关所需要的依赖这里就不进行展示啦,给你们节省一点流量.....<!-- Knife4j API 小刀注解依赖 --><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-gateway-spring-boot-starter</artifactId></dependency>
</dependencies>

定义application.yml文件

spring: application:# 应用名称name: if010-gatewayprofiles:# 环境配置active: devcloud:# Nacos注册中心配置nacos:# Nacos的认证登录用户账户username: admin# Nacos的认证登录用户密码password: 123456discovery:# 服务注册地址server-addr: 127.0.0.1:8848config:# 配置中心地址server-addr: 127.0.0.1:8848# 配置文件格式file-extension: yml# 共享配置shared-configs:- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}- application-${spring.profiles.active}-nacos.${spring.cloud.nacos.config.file-extension}- ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}# 超时时间timeout: 3000# 路由配置gateway:routes:- id: system-serviceuri: lb://if010-systempredicates:- Path=/system-service/**filters:# StripPrefix 是 url 过滤器,1表示请求转发给后端业务服务时,去掉上面Path里从左往右的第1个路径,即system-service- StripPrefix=1# knife4j的网关聚合配置 文档地址:http://{gateway.host}:{gateway.port}/doc.html
knife4j:# 聚合swagger文档gateway:# 是否开启Knife4j网关聚合功能(生产环境不建议开启)enabled: true# 排序规则(tag/operation排序自4.2.0版本新增)# 取值:alpha-默认排序规则,官方swagger-ui默认实现,order-Knife4j提供的增强排序规则,开发者可扩展x-order,根据数值来自定义排序tags-sorter: orderoperations-sorter: order# 指定聚合的策略(默认手动配置(manual),服务发现(discover))strategy: manualdiscover:# 是否开启服务发现模式的配置enabled: false# 指定版本号(swagger2|openapi3)version: swagger2# 需要排除的微服务(eg:网关服务)excluded-services:- if010-gateway# Swagger2个性化配置swagger2:url: /v2/api-docs?group=default# 个性化定制的部分子服务分组情况routes:- name: 系统管理模块# 服务名service-name: system-service# 真实子服务访问url地址-提供OpenAPI的文档url: /system-service/v2/api-docs?group=default# 路由前缀,兼容OpenAPI3规范在聚合时丢失contextPath属性的异常情况,由开发者自己配置contextPath,Knife4j的前端Ui做兼容处理,与url属性独立不冲突,仅OpenAPI3规范聚合需要,OpenAPI2规范不需要设置此属性,默认为(apiPathPrefix)context-path: /# 排序order: 1

到此为止,网关聚合knife4j工作就结束了,操作不涉及代码,全都是配置相关,因为个人严重的洁癖问题discover自动发现并没有开启,想省事偷懒的可以开起来,可以大大减少手动配置的工作

访问:http://{gateway.host}:{gateway.port}/doc.html,看看效果啦~~

接口文档页面结果


http://www.ppmy.cn/embedded/126904.html

相关文章

YoloV8改进策略:BackBone改进|CAFormer在YoloV8中的创新应用,显著提升目标检测性能

摘要 在目标检测领域,模型性能的提升一直是研究者和开发者们关注的重点。近期,我们尝试将CAFormer模块引入YoloV8模型中,以替换其原有的主干网络,这一创新性的改进带来了显著的性能提升。 CAFormer,作为MetaFormer框架下的一个变体,结合了深度可分离卷积和普通自注意力…

解决ubuntu 下 VS code 无法打开点击没反应问题

从Ubuntu 22.04 升级到ubuntu 24.04 后&#xff0c;发现Vsode无法打开&#xff0c;不论是点击图标&#xff0c;还是terminator里面运行code 可执行程序&#xff0c;均没有反应。debug如下: 提示权限不够。 解决方案&#xff1a; sudo sysctl -w kernel.apparmor_restrict_unp…

Mysql(1)—简介及Windows环境下载安装

一、关于Mysql 1.1 简介 MySQL是一个流行的关系型数据库管理系统&#xff08;RDBMS&#xff09;&#xff0c;它基于结构化查询语言&#xff08;SQL&#xff09;进行操作。MySQL由瑞典MySQL AB公司开发&#xff0c;后来被Sun Microsystems收购&#xff0c;最终成为Oracle公司的…

题目:1297. 子串的最大出现次数

> Problem: 1297. 子串的最大出现次数 题目&#xff1a;1297. 子串的最大出现次数 题目描述 给定一个字符串 s&#xff0c;要求找到满足以下条件的任意子串的出现次数&#xff0c;并返回该子串的最大出现次数&#xff1a; 子串中不同字母的数目必须小于等于 maxLetters。…

FLASK 全局模板函数创建以及使用方法来构建资源链接器

代码位置: app.py 函数模板from common.libs.UrlManager import UrlManager app.add_template_global(UrlManager.buildStaticUrl, buildStaticUrl) app.add_template_global(UrlManager.buildUrl, buildUrl) app.add_template_global(UrlManager.buildImageUrl, buildImageUr…

计算机毕业设计 基于Hadoop的租房数据分析系统的设计与实现 Python毕业设计 Python毕业设计选题 数据分析【附源码+安装调试】

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

centos系列,yum部署jenkins2.479.1,2024年长期支持版本

centos系列&#xff0c;yum部署jenkins2.479.1&#xff0c;2024年长期支持版本 0、介绍 注意&#xff1a;jenkins建议安装LTS长期支持版本&#xff0c;而不是安装每周更新版本&#xff0c;jenkins安装指定版本 openjdk官网下载 Index of /jenkins/redhat-stable/ | 清华大学开…

基于SpringBoot问卷调查系统小程序【附源码】

基于SpringBoot问卷调查系统小程序 效果如下&#xff1a; 管理员登录界面 管理员功能界面 调查人管理界面 问卷调查管理界面 问卷题目管理界面 用户登录界面 APP首页界面 公告信息界面 研究背景 随着科学技术的飞速发展&#xff0c;各行各业都在努力与现代先进技术接轨&…