Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of realizing the switching of multiple Service interfaces by aggregating swagger in spring-gateway Gateway

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The editor of this article introduces in detail "the method of spring-gateway gateway aggregation swagger to achieve multiple service interface switching". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "spring-gateway gateway aggregation swagger to achieve multiple service interface switching methods" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

prerequisite

Microservices have been integrated with swagger and registered with nacos.

Gateway configuration 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.* / * aggregate the swagger interfaces of various services * / @ Componentpublic class MySwaggerResourceProvider implements SwaggerResourcesProvider {/ * swagger2 default url suffix * / private static final String SWAGGER2URL = "/ v2/api-docs"; / * * Gateway routing * / private final RouteLocator routeLocator / * * Gateway application name * / @ 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 () / / get all available host:serviceId routeLocator.getRoutes () .filter (route-> route.getUri (). GetHost ()! = null) .filter (route->! self.equals (route.getUri (). GetHost ()) .subscribe (route-> routeHosts.add (route.getUri (). GetHost (); / / record the added server Set dealed = new HashSet () RouteHosts.forEach (instance-> {/ / stitching 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 aggregation interface, all three interfaces are interfaces that swagger-ui.html needs to access * / @ 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-discoveryserver: port: 9527spring: application: Name: cloud-gateway cloud: gateway: routes:-id: seata-storage-service uri: lb://seata-storage-service predicates:-Path=/seata-storage-service/** # assertion Matching routing filters:-StripPrefix=1-id: seata-account-service uri: lb://seata-account-service-Path=/seata-account-service/** discovery: locator: enabled: true # enables the function of dynamically creating routes from the registry, using the micro service name to route nacos: server-addr: localhost:8848

-StripPrefix=1 must be configured. Skip the first section of the path of-Path.

Http://localhost:2003/v2/api-docs this is the correct swagger data request address. Without-StripPrefix=1, swagger will request http://localhost:2003/seata-account-service/v2/api-docs when requesting data, so the data will not be requested.

If you do not add-StripPrefix=1, there are other solutions that can configure the service context path in the microservice provider

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

Pay attention to the interceptor of the gateway and do not block the swagger request.

After reading this, the article "how to realize the switching of multiple service interfaces by spring-gateway gateway aggregation swagger" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to learn more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report