Springcloud网关zuul集成Swagger2并分组展示接口

效果图

注册中心

swagger效果

实现思路

要达到分组效果需要修改gateway中的swagger配置,改成按照分组获取swagger。

通过属性文件配置需要分组的微服务,属性文件key是微服务名称,value是分组的groupName(注意需要和微服务中的分组名称相同),配置需要分组的微服务按照分组处理,否则按照整个服务处理。

gateway服务中SwaggerConfig代码:

package com.okcloud.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Configuration
@EnableSwagger2
@Slf4j
public class SwaggerConfig {

    @Autowired
    ZuulProperties properties;

    //http://localhost:5555/swagger-ui.html
    private final String version = "1.0";


    @Primary
    @Bean
    public SwaggerResourcesProvider swaggerResourcesProvider() {
        //读取属性文件
        Properties p = readProperties();
        return () -> {
            List<SwaggerResource> resources = new ArrayList<>();
            properties.getRoutes().values().stream()
                    .forEach(
                            (route) -> {
                                String serviceId = route.getServiceId();
                                String property = p.getProperty(serviceId);
                                if (StringUtils.isNotBlank(property)) {
                                    //在这里处理分组的swagger,可以通过配置文件的方式配置需要分组的微服务,然后单独处理
                                    String[] groupNames = property.split(",");
                                    for (String groupName : groupNames) {
                                        resources.add(createResource(route.getServiceId(), route.getServiceId(), version, groupName));
                                    }
                                } else {
                                    //正常的处理逻辑
                                    resources.add(createResource(route.getServiceId(), route.getServiceId(), version));
                                }

                            }
                    );
            return resources;
        };
    }

    private Properties readProperties() {
        Properties p = new Properties();
        InputStreamReader in = null;
        try {
            in = new InputStreamReader(SwaggerConfig.class.getResourceAsStream("/swagger.properties"), "UTF-8");
            p.load(in);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("加载 swagger.properties 属性文件出错,错误信息:" + e.getMessage());
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return p;
    }

    private SwaggerResource createResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation("/" + location + "/v2/api-docs");
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }

    private SwaggerResource createResource(String name, String location, String version, String groupName) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation("/" + location + "/v2/api-docs?group=" + groupName);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }

}

属性文件swagger.properties代码:

yd-cloudshop=消息模块,商家模块,订单模块,系统模块,基础模块,菜品模块,菜品相关模块

微服务中的配置

微服务的配置和springboot项目配置相同,略......

swagger2版本

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
</dependency>
<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
</dependency>
发布了35 篇原创文章 · 获赞 13 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/cs373616511/article/details/100100785