SpringCloud学习笔记(五)--Gateway实现多服务实例的
Swagger聚合
概述
Swagger 是⼀个规范和完整的框架,⽤于⽣成、描述、调⽤和可视化 RESTful 风格的 Web 服务。 本⽂主要介绍如何使⽤⽹关Gateway,实现将多个实例的Swagger聚合。
服务实例
pom依赖包配置
<!--Swagger-UI API⽂档⽣产⼯具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
config配置
package fig;
import org.springframework.dition.ConditionalOnProperty; import t.annotation.Bean;
import t.annotation.ComponentScan;
import t.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* Swagger2API⽂档的配置
**/
@Configuration
@ComponentScan("ller")
@ConditionalOnProperty(name = "able", havingValue = "true") public class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.demo.*Controller")) .paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("demo")
.description("demo")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
启动类
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringCloudApplication
@EnableSwagger2
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
⽹关Gateway
pom依赖包配置
<dependency>
<groupId>io.springfox</groupId>
springcloud难学吗<artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId> <version>2.9.2</version>
</dependency>
路由规则过滤配置
routes:
- id: demo-service
uri: lb://demo-service
predicates:
- Path=/demo-service/**
#Demo过滤器
filters:
# Swagger请求头处理
- SwaggerHeaderFilter
- StripPrefix=1 SwaggerHeaderFilter过滤器
package com.demo.gateway.filter;
import com.digicn.prod.fig.SwaggerProvider;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.active.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
/**
* 添加X-Forwarded-Prefix header
*/
@Component
public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory {
private static final String HEADER_NAME = "X-Forwarded-Prefix";
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
ServerHttpRequest request = Request();
String path = URI().getPath();
if (!dsWithIgnoreCase(path, SwaggerProvider.API_URI)) {
return chain.filter(exchange);
}
String basePath = path.substring(0, path.lastIndexOf(SwaggerProvider.API_URI));
ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build(); ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
return chain.filter(newExchange);
};
}
}
SwaggerProvider⽂档注册
import lombok.AllArgsConstructor;
import org.springframework.fig.GatewayProperties;
import org.springframework.ute.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import t.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList;
import java.util.List;
/**
* 聚合接⼝⽂档注册,和zuul实现类似
*/
@Component
@Primary
@AllArgsConstructor
public class SwaggerProvider implements SwaggerResourcesProvider {
public static final String API_URI = "/v2/api-docs";
private final RouteLocator routeLocator;
private final GatewayProperties gatewayProperties;
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
List<String> routes = new ArrayList<>();
// filter过滤器
//.filter(predicateDefinition -> !"demo-service".Id()))
.forEach(predicateDefinition -> resources.add(Id(),
.replace("/**", API_URI)))));
return resources;
}
private SwaggerResource swaggerResource(String name, String location) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion("2.0");
return swaggerResource;
}
}
Handler处理器
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论