优化路由配置

This commit is contained in:
2025-08-22 21:05:13 +08:00
parent 17c259958d
commit 5b443509ba

View File

@ -21,33 +21,64 @@ public class CustomRouteConfig {
private final AtomicInteger index = new AtomicInteger(0);
// @Bean
// public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
// return builder.routes()
// .route("sczx-user", r -> r.path("/zc/user/**")
// .filters(f -> f.rewritePath("/zc/user/(?<segment>.*)", "/${segment}"))
// .uri(uriWithCustomLoadBalance("sczx-user")))
// .route("sczx-store", r -> r.path("/zc/store/**")
// .filters(f -> f.rewritePath("/zc/store/(?<segment>.*)", "/${segment}"))
// .uri(uriWithCustomLoadBalance("sczx-store")))
// .route("sczx-car", r -> r.path("/zc/car/**")
// .filters(f -> f.rewritePath("/zc/car/(?<segment>.*)", "/${segment}"))
// .uri(uriWithCustomLoadBalance("sczx-car")))
// .route("sczx-order", r -> r.path("/zc/order/**")
// .filters(f -> f.rewritePath("/zc/order/(?<segment>.*)", "/${segment}"))
// .uri(uriWithCustomLoadBalance("sczx-order")))
// .route("sczx-sync", r -> r.path("/zc/sync/**")
// .filters(f -> f.rewritePath("/zc/sync/(?<segment>.*)", "/${segment}"))
// .uri(uriWithCustomLoadBalance("sczx-sync")))
// .route("sczx-singlepay", r -> r.path("/zc/singlepay/**")
// .filters(f -> f.rewritePath("/zc/singlepay/(?<segment>.*)", "/${segment}"))
// .uri(uriWithCustomLoadBalance("sczx-singlepay")))
// .build();
//// .route("sczx_user", r -> r.path("/zc/user/**")
//// .uri("lb://sczx_user"))
//// .route("sczx_store", r -> r.path("/zc/store/**")
//// .uri("lb://sczx_store"))
//// .build();
// }
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("sczx-user", r -> r.path("/zc/user/**")
.filters(f -> f.rewritePath("/zc/user/(?<segment>.*)", "/${segment}"))
.uri(uriWithCustomLoadBalance("sczx-user")))
.route("sczx-store", r -> r.path("/zc/store/**")
.filters(f -> f.rewritePath("/zc/store/(?<segment>.*)", "/${segment}"))
.uri(uriWithCustomLoadBalance("sczx-store")))
.route("sczx-car", r -> r.path("/zc/car/**")
.filters(f -> f.rewritePath("/zc/car/(?<segment>.*)", "/${segment}"))
.uri(uriWithCustomLoadBalance("sczx-car")))
.route("sczx-order", r -> r.path("/zc/order/**")
.filters(f -> f.rewritePath("/zc/order/(?<segment>.*)", "/${segment}"))
.uri(uriWithCustomLoadBalance("sczx-order")))
.route("sczx-sync", r -> r.path("/zc/sync/**")
.filters(f -> f.rewritePath("/zc/sync/(?<segment>.*)", "/${segment}"))
.uri(uriWithCustomLoadBalance("sczx-sync")))
.route("sczx-singlepay", r -> r.path("/zc/singlepay/**")
.filters(f -> f.rewritePath("/zc/singlepay/(?<segment>.*)", "/${segment}"))
.uri(uriWithCustomLoadBalance("sczx-singlepay")))
.build();
// .route("sczx_user", r -> r.path("/zc/user/**")
// .uri("lb://sczx_user"))
// .route("sczx_store", r -> r.path("/zc/store/**")
// .uri("lb://sczx_store"))
// .build();
RouteLocatorBuilder.Builder routesBuilder = builder.routes();
// 定义需要路由的服务列表
String[] serviceNames = {"sczx-user", "sczx-store", "sczx-car", "sczx-order", "sczx-sync", "sczx-singlepay"};
// 为每个服务添加路由规则(如果服务存在)
for (String serviceName : serviceNames) {
if (hasServiceInstances(serviceName)) {
routesBuilder.route(serviceName, r -> r.path("/zc/" + serviceName.split("-")[1] + "/**")
.filters(f -> f.rewritePath("/zc/" + serviceName.split("-")[1] + "/(?<segment>.*)", "/${segment}"))
.uri(uriWithCustomLoadBalance(serviceName)));
} else {
log.warn("服务 {} 不存在实例,跳过路由配置", serviceName);
}
}
return routesBuilder.build();
}
/**
* 检查是否有可用的服务实例
* @param serviceName 服务名称
* @return 是否有可用实例
*/
private boolean hasServiceInstances(String serviceName) {
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
return instances != null && !instances.isEmpty();
}