From c3591578260fa82c605c4e49d50a6b30ec3e72e9 Mon Sep 17 00:00:00 2001 From: zhangli <123879394@qq.com> Date: Wed, 9 Jul 2025 00:07:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=97=A8=E5=BA=97=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E8=B7=AF=E7=94=B1=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 19 +++++++++--------- .../com/sczx/gateway/GatewayApplication.java | 4 ++++ .../component/DynamicRouteScheduler.java | 20 +++++++++++++++++++ .../gateway/config/CustomRouteConfig.java | 14 +++++++++++++ src/main/resources/application.yml | 6 ++++++ 5 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/sczx/gateway/component/DynamicRouteScheduler.java diff --git a/pom.xml b/pom.xml index 17f81c9..3af5481 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,11 @@ spring-cloud-starter-alibaba-nacos-discovery + + org.springframework.cloud + spring-cloud-starter-loadbalancer + + @@ -78,10 +83,10 @@ - - - - + + org.springframework.boot + spring-boot-starter-data-redis-reactive + @@ -91,12 +96,6 @@ 1.18.30 - - - - - - diff --git a/src/main/java/com/sczx/gateway/GatewayApplication.java b/src/main/java/com/sczx/gateway/GatewayApplication.java index f2f47b1..4aa35bf 100644 --- a/src/main/java/com/sczx/gateway/GatewayApplication.java +++ b/src/main/java/com/sczx/gateway/GatewayApplication.java @@ -2,7 +2,11 @@ package com.sczx.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.scheduling.annotation.EnableScheduling; +@EnableDiscoveryClient +@EnableScheduling // 启用定时任务 @SpringBootApplication public class GatewayApplication { public static void main(String[] args) { diff --git a/src/main/java/com/sczx/gateway/component/DynamicRouteScheduler.java b/src/main/java/com/sczx/gateway/component/DynamicRouteScheduler.java new file mode 100644 index 0000000..3889dca --- /dev/null +++ b/src/main/java/com/sczx/gateway/component/DynamicRouteScheduler.java @@ -0,0 +1,20 @@ +package com.sczx.gateway.component; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gateway.event.RefreshRoutesEvent; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class DynamicRouteScheduler { + + @Autowired + private ApplicationEventPublisher publisher; + + // 每30秒刷新一次路由(可按需调整) + @Scheduled(fixedRate = 30_000) + public void refreshRoutes() { + publisher.publishEvent(new RefreshRoutesEvent(this)); + } +} diff --git a/src/main/java/com/sczx/gateway/config/CustomRouteConfig.java b/src/main/java/com/sczx/gateway/config/CustomRouteConfig.java index ab51f8b..504db30 100644 --- a/src/main/java/com/sczx/gateway/config/CustomRouteConfig.java +++ b/src/main/java/com/sczx/gateway/config/CustomRouteConfig.java @@ -1,5 +1,6 @@ package com.sczx.gateway.config; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; @@ -12,6 +13,7 @@ import java.net.URI; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +@Slf4j @Configuration public class CustomRouteConfig { @Autowired @@ -25,9 +27,19 @@ public class CustomRouteConfig { .route("sczx_user", r -> r.path("/zc/user/**") .filters(f -> f.rewritePath("/zc/user/(?.*)", "/${segment}")) .uri(uriWithCustomLoadBalance("sczx_user"))) + .route("sczx_store", r -> r.path("/zc/store/**") + .filters(f -> f.rewritePath("/zc/store/(?.*)", "/${segment}")) + .uri(uriWithCustomLoadBalance("sczx_store"))) .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(); } + + private URI uriWithCustomLoadBalance(String serviceName) { List instances = discoveryClient.getInstances(serviceName); if (instances == null || instances.isEmpty()) { @@ -35,6 +47,8 @@ public class CustomRouteConfig { } ServiceInstance selected = instances.get(Math.abs(index.getAndIncrement()) % instances.size()); + log.info("Selected instance: {}", selected); return URI.create("http://" + selected.getHost() + ":" + selected.getPort()); } } + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 89fb6fe..145b4e2 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -18,6 +18,12 @@ spring: # predicates: # - Path=/zc/user/** # filters: +# - StripPrefix=2 +# - id: sczx_store +# uri: lb://sczx_store +# predicates: +# - Path=/zc/store/** +# filters: # - StripPrefix=2 nacos: discovery: