当前位置: 首页 > news >正文

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

前提条件

微服务已经集成了swagger,并且注册进了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.*;/*** 聚合各个服务的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;@Autowiredpublic MySwaggerResourceProvider(RouteLocator routeLocator) {this.routeLocator = routeLocator;}@Overridepublic List<SwaggerResource> get() {List<SwaggerResource> resources = new ArrayList<>();List<String> routeHosts = new ArrayList<>();// 获取所有可用的host:serviceIdrouteLocator.getRoutes().filter(route -> route.getUri().getHost() != null).filter(route -> !self.equals(route.getUri().getHost())).subscribe(route -> routeHosts.add(route.getUri().getHost()));// 记录已经添加过的serverSet<String> dealed = new HashSet<>();routeHosts.forEach(instance -> {// 拼接urlString 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;@Autowiredpublic 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);}@RequestMappingpublic 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: 9527spring:application:name: cloud-gatewaycloud:gateway:routes:- id: seata-storage-serviceuri: lb://seata-storage-servicepredicates:- Path=/seata-storage-service/**         # 断言,相匹配的进行路由filters:- StripPrefix=1- id: seata-account-serviceuri: lb://seata-account-servicepredicates:- Path=/seata-account-service/**filters:- StripPrefix=1discovery:locator:enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由nacos:discovery:server-addr: localhost:8848
  • - StripPrefix=1是必须配置的,跳过- Path的第一段路径。
    • http://localhost:2003/v2/api-docs 这个是正确的swagger数据请求地址。不加- StripPrefix=1的话,swagger在请求数据时候会请求http://localhost:2003/seata-account-service/v2/api-docs,这样就会请求不到数据。

在这里插入图片描述

如果不加- StripPrefix=1,也有其他的解决方案,可以在微服务提供者中配置服务上下文路径

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

注意网关的拦截器,不要将swagger请求拦截掉。

http://www.lryc.cn/news/485497.html

相关文章:

  • 关于 Oracle Database Express Edition 的功能和安装
  • 领夹麦克风哪个品牌好,手机领夹麦克风哪个牌子好,选购推荐
  • 什么是 Go 语言?
  • AI 大模型重塑软件开发流程:定义、应用、优势与挑战
  • 微服务即时通讯系统的实现(客户端)----(1)
  • 【freertos】FreeRTOS时间管理
  • 台式电脑没有声音怎么办?台式电脑没有声音解决详解
  • 机器学习基础02
  • element plus的表格内容自动滚动
  • 哈佛商业评论 | 未来商业的技术趋势:百度李彦宏谈技术如何变革商业
  • Pytorch如何将嵌套的dict类型数据加载到GPU
  • Shell基础2
  • 7z 解压器手机版与解压专家:安卓解压工具对决
  • C++清除所有输出【DEV-C++】所有编辑器通用 | 算法基础NO.1
  • 【Android、IOS、Flutter、鸿蒙、ReactNative 】启动页
  • SpringBoot 2.2.10 无法执行Test单元测试
  • 聊天服务器(8)用户登录业务
  • stm32在linux环境下的开发与调试
  • flinkOnYarn并配置prometheus+grafana监控告警
  • 麒麟系统下docker搭建jenkins
  • 论文阅读 - Causally Regularized Learning with Agnostic Data Selection
  • 计算机网络之会话层
  • blind-watermark - 水印绑定
  • reduce-scatter:适合分布式计算;Reduce、LayerNorm和Broadcast算子的执行顺序对计算结果的影响,以及它们对资源消耗的影响
  • DAY64||dijkstra(堆优化版)精讲 ||Bellman_ford 算法精讲
  • 使用Git工具在GitHub的仓库中上传文件夹(超详细)
  • Python酷库之旅-第三方库Pandas(218)
  • 斗鱼大数据面试题及参考答案
  • 后仿真中的GLS测试用例的选取规则
  • 对接阿里云实人认证