Spring Cloud Gateway + Knife4j 聚合API文档

我们将采用Nacos作为注册中心,Gateway作为网关,使用Knife4j来生成API文档,该方案是目前来说最合理的微服务文档解决方案了。

应用架构

网关作为API文档的统一入口,网关聚合所有微服务的文档,通过在网关进行切换来实现对其他服务API文档的访问。

相关服务划分:

  • micro-knife4j-gateway:网关服务,作为微服务API文档的访问入口,聚合所有API文档;
  • micro-knife4j-user:用户服务,普通API服务;
  • micro-knife4j-order:订单服务,普通API服务。

具体实现

下面详细介绍下Spring Cloud Gateway + Knife4j 聚合API文档的具体实现,依次搭建用户服务、订单服务和网关服务。

micro-knife4j-user

我们首先来搭建用户服务,一个普通的API服务,很简单,仅需三步即可集成Knife4j。

  • pom.xml中添加相关依赖,一个SpringBoot的web功能依赖,Knife4j的依赖;

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
    </dependency>
</dependencies>

  • application.yml这添加相关配置,由于Knife4j底层使用的是SpringDoc,所以配置一下Nacos注册中心和SpringDoc即可;

server:
  port: 9501
spring:
  profiles:
    active: dev
  application:
    name: micro-knife4j-user
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
management:
  endpoints:
    web:
      exposure:
        include: "*"
springdoc:
  swagger-ui:
    path: /swagger-ui.html
    tags-sorter: alpha
    operations-sorter: alpha
    show-extensions: true
  api-docs:
    path: /v3/api-docs
  group-configs:
    - group: 'default'
      paths-to-match: '/user/**'
      packages-to-scan: com.macro.cloud.controller
  default-flat-param-object: false

  • 添加SpringDoc相关Java配置,非常常规的配置,和我们之前在SpringBoot中使用SpringDoc的配置没什么区别。

/**
 * @auther macrozheng
 * @description SpringDoc API文档相关配置
 * @date 2023/11/29
 * @git
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值