springcloud-gateway网关聚合swagger实现多个服务接口切换

正经学徒,佛系记录,不搞事情

springcloud是由多个不同的springboot服务组成的,微服务使用swagger有两种方法,如下:

方法一:(不推荐,但是是方法二的前置条件)

对每个需要生成接口的项目集成swagger,具体方法点击查看

然后启动所有的项目,需要查看不同服务的接口时去访问不同的地址:http://{ip}:{port}/swagger-ui.html

缺陷很明显:为记录不同项目的地址而烦恼,一旦服务ip或端口更换后又要重新记录

方法二:使用网关统一入口

访问swagger-ui.html的时候会发现右上角的这个下拉选项

当启动一个springboot项目的时候会发现这个下拉选项毫无用处,不过它的强大是在于这个下拉可以用来切换不同项目的swagger接口地址,这就实现了使用一个网关的url访问所有的项目接口,这里不多说明springcloud的必要组成部分

假定当前已搭好如下项目(例子使用的是springboot2.1.9springcloud Greenwich.SR1

  1. eureka注册中心,端口7000
  2. provider-play服务提供者,端口8002,已集成swagger,集成方法点击查看
  3. provider-test服务提供者,端口8001,已集成swagger,集成方法点击查看
  4. cloud-gateway使用gateway搭建的网关,端口7001

cloud-gateway搭建swagger与服务提供者不同,如下步骤:

  • 创建一个类实现SwaggerResourcesProvider 
package com.meiya.whale.gateway.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.*;

/**
 * 聚合各个服务的swagger接口
 */
@Component
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
    /**
     * swagger2默认的url后缀
     */
    private static final String SWAGGER2URL = "/v2/api-docs";

    /**
     * 网关路由
     */
    private final RouteLocator routeLocator;

    /**
     * 网关应用名称
     */
    @Value("${spring.application.name}")
    private String self;

    @Autowired
    public MySwaggerResourceProvider(RouteLocator routeLocator) {
        this.routeLocator = routeLocator;
    }

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        List<String> routeHosts = new ArrayList<>();
        // 获取所有可用的host:serviceId
        routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
                .filter(route -> !self.equals(route.getUri().getHost()))
                .subscribe(route -> routeHosts.add(route.getUri().getHost()));

        // 记录已经添加过的server,存在同一个应用注册了多个服务在eureka上
        Set<String> dealed = new HashSet<>();
        routeHosts.forEach(instance -> {
            // 拼接url
            String url = "/" + instance.toLowerCase() + SWAGGER2URL;
            if (!dealed.contains(url)) {
                dealed.add(url);
                SwaggerResource swaggerResource = new SwaggerResource();
                swaggerResource.setUrl(url);
                swaggerResource.setName(instance);
                resources.add(swaggerResource);
            }
        });
        return resources;
    }
}
  • 创建一个聚合接口类
package com.meiya.whale.gateway.controller;

import com.meiya.whale.gateway.configuration.MySwaggerResourceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger.web.*;

import java.util.List;

/**
 * swagger聚合接口,三个接口都是swagger-ui.html需要访问的接口
 */
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerResourceController {
    private MySwaggerResourceProvider swaggerResourceProvider;

    @Autowired
    public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) {
        this.swaggerResourceProvider = swaggerResourceProvider;
    }

    @RequestMapping(value = "/configuration/security")
    public ResponseEntity<SecurityConfiguration> securityConfiguration() {
        return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK);
    }

    @RequestMapping(value = "/configuration/ui")
    public ResponseEntity<UiConfiguration> uiConfiguration() {
        return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK);
    }

    @RequestMapping
    public ResponseEntity<List<SwaggerResource>> swaggerResources() {
        return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK);
    }
}

 启动所有的项目,网关项目最后启动:

访问网关的http://{ip}:{port}/swagger-ui.html

到这里已经实现了切换不同服务的swagger接口

猜你喜欢

转载自blog.csdn.net/qq_31748587/article/details/102563155