原因
分别访问整合了swagger2的/v2/api-doc的微服务 与 整合了swagger3的/v3/api-doc的微服务后,我发现swagger3响应缺失了bathPath,故此我们通过网关swagger3接口文档访问对应微服务中的接口时,提示404
假设通过网关访问某一微服务的接口URL为:http://网关IP:网关端口/app-user/name/张三
该服务使用了swagger3时,且通过网关访问对应接口文档,此时URL则会变为:http://网关IP:网关端口/name/张三
请求会缺失bathPath(如果使用服务名动态路由的话,则实际缺失的是对应微服务的applicationName)
解决方案
网关请求对应微服务接口文档资源时(/v3/api-doc),使用响应拦截,为其响应JSON中添加上对应的bathPath
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vcolco.gateway.config.MyGateWayProperties;
import com.vcolco.gateway.config.SwaggerProvider;
import lombok.extern.log4j.Log4j2;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @author lei
* @create 2022-05-26 14:36
* @desc swagger过滤器
* 解决swagger3 响应缺少bathPath,请求无法动态路由到服务的问题
**/
@Log4j2
@Component
public class SwaggerGlobalFilter implements GlobalFilter, Ordered {
public static final String BASE_PATH = "basePath";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String path = exchange.getRequest().getPath().toString();
if (!path.endsWith("/v3/api-docs")) {
return chain.filter(exchange);
}
String[] pathArray = path.split("/");
String basePath = pathArray[1];
ServerHttpResponse originalResponse = exchange.getResponse();
ServerHttpResponseDecorator newResponse = new ServerHttpResponseDecorator(originalResponse) {
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (!Objects.equals(getStatusCode(), HttpStatus.OK)) {
return super.writeWith(body);
}
if (body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = Flux.from(body);
return super.writeWith(fluxBody.buffer().map(dataBuffers -> {
List<String> list = new ArrayList<>();
dataBuffers.forEach(dataBuffer -> {
byte[] content = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(content);
DataBufferUtils.release(dataBuffer);
list.add(new String(content, StandardCharsets.UTF_8));
});
String jsonStr = listToString(list);
JSONObject jsonObject = JSON.parseObject(jsonStr);
// 填充bathPath
jsonObject.put(BASE_PATH,basePath);
jsonStr = jsonObject.toString();
return bufferFactory().wrap(jsonStr.getBytes());
}));
}
return super.writeWith(body);
}
private String listToString(List<String> list) {
StringBuilder stringBuilder = new StringBuilder();
for (String s : list) {
stringBuilder.append(s);
}
return stringBuilder.toString();
}
};
return chain.filter(exchange.mutate().response(newResponse).build());
}
@Override
public int getOrder() {
return -2;
}
}
效果展示
如此,我们的问题便是解决了,后续前端可以根据服务名动态的路由到对应微服务了