增加门店服务路由配置

This commit is contained in:
2025-07-09 00:07:36 +08:00
parent 040a57187f
commit c359157826
5 changed files with 53 additions and 10 deletions

19
pom.xml
View File

@ -70,6 +70,11 @@
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- Actuator 监控 -->
<dependency>
@ -78,10 +83,10 @@
</dependency>
<!-- Redis 限流支持 -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-redis-reactive</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!-- Lombok -->
@ -91,12 +96,6 @@
<version> 1.18.30</version>
</dependency>
<!-- Swagger 文档 -->
<!-- <dependency>-->
<!-- <groupId>org.springdoc</groupId>-->
<!-- <artifactId>springdoc-openapi-ui</artifactId>-->
<!-- <version>1.6.14</version>-->
<!-- </dependency>-->
</dependencies>
<!-- Build Configuration -->

View File

@ -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) {

View File

@ -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));
}
}

View File

@ -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>.*)", "/${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")))
.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<ServiceInstance> 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());
}
}

View File

@ -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: