因为之前的技术架构的问题写了固定路由 并且 这个固定路由也是没有规则的匹配 头大,导致swagger-config 获取到地址不是服务名 是路由前缀 ,所以改成了getResources
直接贴方法把2个代码类
package com.tansun.risk.gateway.routes;
import com.github.xiaoymin.knife4j.spring.gateway.Knife4jGatewayProperties;
import com.github.xiaoymin.knife4j.spring.gateway.discover.ServiceDiscoverHandler;
import com.github.xiaoymin.knife4j.spring.gateway.spec.v2.OpenAPI2Resource;
import com.github.xiaoymin.knife4j.spring.gateway.utils.PathUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Slf4j
public class CustomServiceDiscoverHandler extends ServiceDiscoverHandler {
public CustomServiceDiscoverHandler(Knife4jGatewayProperties knife4jGatewayProperties) {
super(knife4jGatewayProperties);
}
@Override
public List<OpenAPI2Resource> getResources(String forwardPath) {
List<OpenAPI2Resource> resourceList = new ArrayList<>();
Set<OpenAPI2Resource> resources = getGatewayResources();
if (!CollectionUtils.isEmpty(resources)) {
for (OpenAPI2Resource resource : resources) {
// 创建 resource 的副本
OpenAPI2Resource copy = resource.copy();
// 处理 contextPath
copy.setContextPath(PathUtils.processContextPath(PathUtils.append(forwardPath, copy.getContextPath())));
// 获取服务名,并设置 URL 为服务名
String serviceName = copy.getServiceName();
if (serviceName != null) {
// 设置 URL 为服务名
copy.setUrl(copy.getUrl().replace(copy.getContextPath(),"/"+ serviceName));
}
// 记录日志
log.debug("api-resources:{}", copy);
// 将修改后的副本加入到资源列表
resourceList.add(copy);
}
return resourceList;
}
return resourceList;
}
}
package com.tansun.risk.gateway.routes;
import com.github.xiaoymin.knife4j.spring.gateway.Knife4jGatewayProperties;
import com.github.xiaoymin.knife4j.spring.gateway.discover.ServiceDiscoverHandler;
import com.github.xiaoymin.knife4j.spring.gateway.utils.PathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GatewaySwaggerRouteConfig {
@Autowired
private DiscoveryClient discoveryClient;
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
RouteLocatorBuilder.Builder routes = builder.routes();
// 获取所有注册的服务
discoveryClient.getServices().forEach(serviceId -> {
// 根据每个服务的名字动态生成 Swagger 路由
routes.route(serviceId + "-swagger", r -> r
.path("/" + serviceId + "/v3/api-docs")
.filters(f -> f.stripPrefix(1))
.uri("lb://" + serviceId)); // lb:// 会使用 Ribbon 来调用服务
});
return routes.build();
}
@Bean
public ServiceDiscoverHandler serviceDiscoverHandler(Knife4jGatewayProperties knife4jGatewayProperties) {
return new CustomServiceDiscoverHandler(knife4jGatewayProperties);
}
}
配置类:
knife4j:
gateway:
enabled: true
tags-sorter: alpha
operations-sorter: alpha
strategy: discover
discover:
enabled: true
version: openapi3
maven:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>