您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“spring-gateway網(wǎng)關聚合swagger實現(xiàn)多個服務接口切換的方法”,內容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“spring-gateway網(wǎng)關聚合swagger實現(xiàn)多個服務接口切換的方法”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
微服務已經(jīng)集成了swagger,并且注冊進了nacos。
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"; /** * 網(wǎng)關路由 */ private final RouteLocator routeLocator; /** * 網(wǎ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/** # 斷言,相匹配的進行路由 filters: - StripPrefix=1 - id: seata-account-service uri: lb://seata-account-service - Path=/seata-account-service/** discovery: locator: enabled: true #開啟從注冊中心動態(tài)創(chuàng)建路由的功能,利用微服務名進行路由 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
,也有其他的解決方案,可以在微服務提供者中配置服務上下文路徑
server: servlet: context-path: /seata-order-service
注意網(wǎng)關的攔截器,不要將swagger請求攔截掉。
讀到這里,這篇“spring-gateway網(wǎng)關聚合swagger實現(xiàn)多個服務接口切換的方法”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。