Spring cloud Gateway 基于使用Netty作为内嵌服务器,而Netty基于WebFlux实现,因此如果想要springdoc的 Swagger UI 显示在网关微服务中,需要用到它的WebFlux UI库。
现有三个微服务项目,分别是gateway-service、shop-service、user-service,各依赖库版本如下:
| 依赖 | 版本 |
|---|---|
| spring boot | 2.6.1 |
| spring cloud | 2021.0.0 |
| spring cloud alibaba | 2021.1 |
| springdoc openapi | 1.5.13 |
由于只在gateway-service网关微服务上查看 Swagger UI 页面,因此另外两个业务微服务中只需要添加springdoc openapi的核心依赖库即可:
...
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
<version>${springdoc.version}</version>
</dependency>
...
而gateway-service中除了spring cloud gateway之外,还需要添加springdoc openapi webflux依赖:
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- spring cloud 2020之后的版本中不再自动引入Ribbon依赖,需要手动引入loadbalancer 否则会出现503错误 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webflux-ui</artifactId

本文介绍了如何使用Spring Cloud Gateway结合Nacos和Springdoc,实现API的集中管理。通过在网关微服务上配置Swagger UI,可以展示并访问其他微服务的API接口。文章详细讲解了各个微服务的依赖配置,Swagger的自定义配置,以及如何通过服务名对API进行分组。最后,针对跨域问题提供了相应的解决方案。
最低0.47元/天 解锁文章
1983

被折叠的 条评论
为什么被折叠?



