springcloud通过网关来集成swagger接口api

1:引入依赖

<!--由于使用网关统一处理微服务中的所有的接口文档,那么服务肯定需要注册到注册中心上,我这里选用的是eureka作为注册中心 -->

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入网关依赖,我这里选用的是zuul网关 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
</dependency>

 

2:在各个微服务中加入swagger配置文件(见我的上一篇博客)

3:在网关服务中同样需要加入swagger配置文件,但是比其他微服务需多注册一个bean对象,来管理整个网关api文档的样式

@Bean
UiConfiguration uiConfig() {
    return new UiConfiguration(null, "list", "alpha", "schema",
            UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
}

4:在网关服务中,需要管理各个微服务的文档,那么需要获取到其他微服务的资源信息(该配置文件中的name,location,version等参数可配置再配置中心中)。location参数徐比对着各个服务注册到网关的路径进行匹配

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        resources.add(swaggerResource("通用接口服务Api", "/lemo/common/v2/api-docs", "1.0"));
        resources.add(swaggerResource("文章服务Api", "/lemo/health_knowledge/v2/api-docs", "1.0"));
        resources.add(swaggerResource("疾病Api", "/lemo/sickness/v2/api-docs", "1.0"));
        resources.add(swaggerResource("广告Api", "/lemo/adversiting/v2/api-docs", "1.0"));
        resources.add(swaggerResource("图片服务Api", "/lemo/images/v2/api-docs", "1.0"));
        resources.add(swaggerResource("学校服务Api", "/lemo/school/v2/api-docs", "1.0"));
        resources.add(swaggerResource("用户服务服务Api", "/lemo/user/v2/api-docs", "1.0"));
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}

5:网关配置文件部分

zuul:
#  #  隐藏所有微服务名称(即使用微服务名称无法访问到服务)
  ignored-services: "*"
#  #  服务前缀名,想要访问项目之前要加上此路径
  prefix: /lemo
  routes:
    article-server:
      path: /health_knowledge/**
    sickness-server:
      path: /sickness/**
    common-server:
      path: /common/**
    adversiting-server:
      path: /adversiting/**
    images-server:
      path: /images/**
    saas-school:
      path: /school/**
    user-server:
      path: /user/**

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway是一个轻量级的API网关,用于微服务架构中路由、过滤和增强流量控制。为了将Swagger(一种常用的API文档生成工具)集成到Spring Cloud Gateway中,你可以按照以下步骤操作: 1. 添加依赖:首先,你需要在你的`pom.xml`或`build.gradle`文件中添加Spring Cloud Gateway和Swagger相关的依赖。例如,如果你使用的是Maven,可能会包含这些依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.x.x</version> <!-- 使用最新版本 --> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.x.x</version> </dependency> ``` 2. 配置 Swagger UI:在Spring Boot应用中,配置Swagger UI作为默认的UI界面。这通常通过修改`application.yml`或`application.properties`文件来完成: ```yaml spring: application: name: your-app-name springfox: ui: enabled: true path: /api-docs ``` `path`属性定义了Swagger UI的访问路径。 3. 注解支持:为了使Spring Cloud Gateway中的路由能够被Swagger识别,你可能需要在你的API控制器上添加`@EnableWebMvcEndpoint`注解(如果尚未添加),以便启用对Web MVC端点的支持。 4. 定义API接口:创建你的API接口,并用`@ApiOperation`、`@ApiParam`等注解来提供必要的元数据信息。这样Swagger才能正确解析并生成文档。 5. 自动扫描:确保你的API控制器在Spring的自动扫描范围内,Spring Cloud Gateway会自动发现并处理它们。 6. 启动应用:启动你的Spring Boot应用,然后访问`http://localhost:your-port/api-docs`(根据配置替换`your-port`),你应该能看到Swagger UI界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值