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: