spring-gateway网关聚合swagger实现多个服务接口切换的方法
发表于:2025-11-19 作者:千家信息网编辑
千家信息网最后更新 2025年11月19日,本文小编为大家详细介绍"spring-gateway网关聚合swagger实现多个服务接口切换的方法",内容详细,步骤清晰,细节处理妥当,希望这篇"spring-gateway网关聚合swagger实
千家信息网最后更新 2025年11月19日spring-gateway网关聚合swagger实现多个服务接口切换的方法
本文小编为大家详细介绍"spring-gateway网关聚合swagger实现多个服务接口切换的方法",内容详细,步骤清晰,细节处理妥当,希望这篇"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接口 */@Componentpublic 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 get() { List resources = new ArrayList<>(); List 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 Set 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() { return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK); } @RequestMapping(value = "/configuration/ui") public ResponseEntity uiConfiguration() { return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK); } @RequestMapping public ResponseEntity> swaggerResources() { return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK); }}
org.springframework.cloud spring-cloud-starter-gateway io.springfox springfox-swagger2 2.9.2 io.swagger swagger-models io.swagger swagger-models 1.5.22 io.springfox springfox-swagger-ui 2.9.2 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
server: port: 9527spring: application: name: cloud-gateway cloud: gateway: routes: - id: seata-storage-service uri: lb://seata-storage-service predicates: - Path=/seata-storage-service/** # 断言,相匹配的进行路由 filters: - StripPrefix=1 - id: seata-account-service uri: lb://seata-account-service - Path=/seata-account-service/** discovery: locator: enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由 nacos: 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请求拦截掉。
读到这里,这篇"spring-gateway网关聚合swagger实现多个服务接口切换的方法"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
服务
接口
网关
路由
多个
方法
切换
数据
文章
配置
内容
路径
妥当
三个
上下
上下文
前提
功能
动态
名称
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
手机主服务器访问状态
网络安全维护是否有必要
云南gps校时服务器云空间
irmck341软件开发
云丁网络技术官网
数据库服务宕了
手机服务器无反应是什么原因
存取json格式的数据库
未装变者服务器怎么设置地图
软件开发技术附件
中文知网学术数据库
畅捷通数据库服务密码
合肥太壹互联网科技有限公司
dj导入excel数据库
关于音乐行业利用大数据库
护苗网络安全课观后感150字
时序数据库 tsdb
长三角新华互联网科技
软件开发能开信息服务费吗
北京聚客互联网科技
数据库回滚宕机
充分发挥互联网和科技的作用
扬州网络安全应急指挥中心
四川智慧城管软件开发哪儿好
网络安全产业基础
北京橙了网络技术有限公司
软件开发编写文档总结
金东区软件开发计划
数据库模式设计
万方数据库网址