Knife4j微服务聚合-入门级教程

205阅读模式

项目分为UserService、ShopService和OrderService,现在需要将其API接口文档进行整合。文章源自懂站帝-http://www.sfdkj.com/12811.html

目的:将ShopService和OrderService中的API接口文档整合到UserService项目中。文章源自懂站帝-http://www.sfdkj.com/12811.html

核心:采用cloud模式聚合文章源自懂站帝-http://www.sfdkj.com/12811.html

技术架构文章源自懂站帝-http://www.sfdkj.com/12811.html

  • SpringBoot
  • Knife4j

这里仅介绍Cloud模式聚合的代码演示,对于Knife4j的学习,请移步:knife4j文章源自懂站帝-http://www.sfdkj.com/12811.html

基础信息

UserService项目

核心依赖

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.7</version>
</dependency>

API接口文档配置类

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean("user-service")
    public Docket userService() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("01.UserService")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ijunfu.user.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

application.yml

server: 
 port: 8080

ShopService项目

核心依赖

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.7</version>
</dependency>

API接口文档配置类

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean("shop-service")
    public Docket userService() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("02.ShopService")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ijunfu.shop.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

application.yml

server: 
 port: 8081

OrderService项目

核心依赖

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.7</version>
</dependency>

API接口文档配置类

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {

    @Bean("order-service")
    public Docket userService() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("03.OrderService")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ijunfu.order.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

application.yml

server: 
 port: 8082

重点来了

UserService

引入依赖

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-aggregation-spring-boot-starter</artifactId>
    <version>2.0.8</version>
</dependency>

添加配置

knife4j:
  enableAggregation: true
  cloud:
    enable: true
    routes:
      - name: 用户服务
        uri: http://localhost:8080
        location: /v2/api-docs?group=01.UserService
      - name: 店铺&商户服务
        uri: http://localhost:8081
        location: /v2/api-docs?group=02.ShopService
      - name: 订单服务
        uri: http://localhost:8082
        location: /v2/api-docs?group=03.OrderService

打完收工,赶紧启动你的UserService项目文章源自懂站帝-http://www.sfdkj.com/12811.html

懂站帝
  • 本文由 发表于 2022年5月10日 22:23:48
  • 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至395045033@qq.com举报,一经查实,本站将立刻删除。