欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring-gateway網(wǎng)關(guān)聚合swagger實(shí)現(xiàn)多個服務(wù)接口切換的示例代碼

 更新時間:2022年03月06日 11:09:15   作者:ZmyCoder  
這篇文章主要介紹了spring-gateway網(wǎng)關(guān)聚合swagger實(shí)現(xiàn)多個服務(wù)接口切換的示例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前提條件

微服務(wù)已經(jīng)集成了swagger,并且注冊進(jìn)了nacos。

gateway配置

package com.zmy.springcloud.config;

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.*;

/**
 * 聚合各個服務(wù)的swagger接口
 */
@Component
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
    /**
     * swagger2默認(rèn)的url后綴
     */
    private static final String SWAGGER2URL = "/v2/api-docs";

    /**
     * 網(wǎng)關(guān)路由
     */
    private final RouteLocator routeLocator;

    /**
     * 網(wǎng)關(guān)應(yīng)用名稱
     */
    @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()));

        // 記錄已經(jīng)添加過的server
        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.zmy.springcloud.config.swagger.controller;

import com.zmy.springcloud.config.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);
    }
}

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--swagger生成API文檔-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
    <exclusions>
        <exclusion>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-models</artifactId>
    <version>1.5.22</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<!--nacos-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
server:
  port: 9527
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: seata-storage-service
          uri: lb://seata-storage-service
          predicates:
            - Path=/seata-storage-service/**         # 斷言,相匹配的進(jìn)行路由
          filters:
            - StripPrefix=1
        - id: seata-account-service
          uri: lb://seata-account-service
            - Path=/seata-account-service/**
      discovery:
        locator:
          enabled: true #開啟從注冊中心動態(tài)創(chuàng)建路由的功能,利用微服務(wù)名進(jìn)行路由
    nacos:
        server-addr: localhost:8848

- StripPrefix=1是必須配置的,跳過- Path的第一段路徑。

http://localhost:2003/v2/api-docs 這個是正確的swagger數(shù)據(jù)請求地址。不加- StripPrefix=1的話,swagger在請求數(shù)據(jù)時候會請求http://localhost:2003/seata-account-service/v2/api-docs,這樣就會請求不到數(shù)據(jù)。

在這里插入圖片描述

如果不加- StripPrefix=1,也有其他的解決方案,可以在微服務(wù)提供者中配置服務(wù)上下文路徑

server:
  servlet:
    context-path: /seata-order-service

注意網(wǎng)關(guān)的攔截器,不要將swagger請求攔截掉。

到此這篇關(guān)于spring-gateway網(wǎng)關(guān)聚合swagger實(shí)現(xiàn)多個服務(wù)接口切換的文章就介紹到這了,更多相關(guān)spring-gateway網(wǎng)關(guān)聚合swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論