spring-gateway網(wǎng)關(guān)聚合swagger實(shí)現(xiàn)多個(gè)服務(wù)接口切換的示例代碼
前提條件
微服務(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.*;
/**
* 聚合各個(gè)服務(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聚合接口,三個(gè)接口都是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 #開啟從注冊中心動(dòng)態(tài)創(chuàng)建路由的功能,利用微服務(wù)名進(jìn)行路由
nacos:
server-addr: localhost:8848- StripPrefix=1是必須配置的,跳過- Path的第一段路徑。
http://localhost:2003/v2/api-docs 這個(gè)是正確的swagger數(shù)據(jù)請求地址。不加- StripPrefix=1的話,swagger在請求數(shù)據(jù)時(shí)候會(huì)請求http://localhost:2003/seata-account-service/v2/api-docs,這樣就會(huì)請求不到數(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)多個(gè)服務(wù)接口切換的文章就介紹到這了,更多相關(guān)spring-gateway網(wǎng)關(guān)聚合swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Springboot實(shí)現(xiàn)送水公司信息管理系統(tǒng)
這篇文章主要介紹了基于Springboot實(shí)現(xiàn)送水公司信息管理,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
java基礎(chǔ)之 “==”與“equals”區(qū)別詳解
這篇文章主要介紹了java基礎(chǔ)之 “==”與“equals”區(qū)別詳解,需要的朋友可以參考下2020-02-02
java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort)
這篇文章主要介紹了java實(shí)現(xiàn)6種字符串?dāng)?shù)組的排序(String array sort),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Spring Boot環(huán)境屬性占位符解析及類型轉(zhuǎn)換詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot環(huán)境屬性占位符解析及類型轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
java 枚舉類定義靜態(tài)valueOf(java.lang.String)方法的問題及解決
這篇文章主要介紹了java 枚舉類定義靜態(tài)valueOf(java.lang.String)方法的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
El表達(dá)式使用問題javax.el.ELException:Failed to parse the expression
今天小編就為大家分享一篇關(guān)于Jsp El表達(dá)式使用問題javax.el.ELException:Failed to parse the expression的解決方式,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
java實(shí)現(xiàn)輸出任意整數(shù)的每一位
這篇文章主要介紹了java實(shí)現(xiàn)輸出任意整數(shù)的每一位,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

