背景
Spring Cloud Gateway
使用Netty
作為嵌入式服務器,并基于響應式Spring WebFlux
。做為微服務網(wǎng)關,多個微服務把API
掛在Gateway
上,如果查看某個API
的Swagger
還要去各個子微服務中去查看,就很不方便,如果能在Gateway
上直接查看各個微服務的API
文檔,會方便很多,本文以截至目前最新的版本為示例,講解如何在Spring Cloud Gateway
中集成SpringDoc
。
SpringBoot 3.x
需要SpringDoc 2.x
。
本地開發(fā)環(huán)境介紹
開發(fā)依賴 | 版本 |
---|---|
Spring Boot | 3.0.6 |
Spring Cloud | 2022.0.2 |
Spring Cloud Alibaba | 2022.0.0.0-RC2 |
Spring Doc | 2.1.0 |
JDK | 20 |
pom.xml主要依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--引?webflux-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- webflux+SpringDoc -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-common</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
</dependency>
</dependencies>
application.yml
spring:
application:
name: demo-gateway
cloud:
gateway:
api-prefix: /
#路由配置
routes:
- id: wen3-framework-apidoc-springdoc-demo
uri: http://localhost:7001
predicates:
- Path=/demo7001/**
filters:
- RewritePath=/demo7001/(?<path>.*), /$\{path}
- id: openapi
uri: http://localhost:${server.port}
predicates:
- Path=/v3/api-docs/**
filters:
- RewritePath=/v3/api-docs/(?<path>.*), /$\{path}/api-docs
######## swagger configuration ###############
springdoc:
swagger-ui:
urls:
- name: demo7001
url: /v3/api-docs/demo7001
- demo7001是另一個SpringDoc演示服務,可以通過/api-docs獲取swagger JSON數(shù)據(jù)即可
- 如果有多個服務,就需要添加多個routes
- 原理是通過調用微服務本身文檔接口獲取JSON數(shù)據(jù),然后在網(wǎng)關的swagger-ui頁面上展示
效果預覽
瀏覽器訪問http://localhost:8081/swagger-ui.html
文章來源:http://www.zghlxwxcb.cn/news/detail-630362.html
動態(tài)生成swagger文檔分組
package com.wen3.demo.gateway.springdoc;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.properties.AbstractSwaggerUiConfigProperties;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.HashSet;
@Component
@Slf4j
public class SwaggerUiConfigPropertiesPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(SwaggerUiConfigProperties.class.isAssignableFrom(bean.getClass())) {
SwaggerUiConfigProperties obj = (SwaggerUiConfigProperties) bean;
if(CollectionUtils.isEmpty(obj.getUrls())) {
obj.setUrls(new HashSet<>());
}
obj.getUrls().add(new AbstractSwaggerUiConfigProperties.SwaggerUrl("demo7001", "/v3/api-docs/demo7001", "demo7001xx"));
}
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
}
- 可以根據(jù)需要改造這個功能,可以在從routes配置中解析各個微服務,然后添加每個微服務的swagger文檔
- 也可以從數(shù)據(jù)庫或其它地方獲取需要開啟的swagger文檔
效果預覽
文章來源地址http://www.zghlxwxcb.cn/news/detail-630362.html
在線文檔
- 官網(wǎng):
https://springdoc.org/#getting-started
到了這里,關于【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!