增加门店服务路由配置
This commit is contained in:
19
pom.xml
19
pom.xml
@ -70,6 +70,11 @@
|
|||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- Actuator 监控 -->
|
<!-- Actuator 监控 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -78,10 +83,10 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Redis 限流支持 -->
|
<!-- Redis 限流支持 -->
|
||||||
<!-- <dependency>-->
|
<dependency>
|
||||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
<groupId>org.springframework.boot</groupId>
|
||||||
<!-- <artifactId>spring-boot-starter-data-redis-reactive</artifactId>-->
|
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
|
||||||
<!-- </dependency>-->
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- Lombok -->
|
<!-- Lombok -->
|
||||||
@ -91,12 +96,6 @@
|
|||||||
<version> 1.18.30</version>
|
<version> 1.18.30</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Swagger 文档 -->
|
|
||||||
<!-- <dependency>-->
|
|
||||||
<!-- <groupId>org.springdoc</groupId>-->
|
|
||||||
<!-- <artifactId>springdoc-openapi-ui</artifactId>-->
|
|
||||||
<!-- <version>1.6.14</version>-->
|
|
||||||
<!-- </dependency>-->
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<!-- Build Configuration -->
|
<!-- Build Configuration -->
|
||||||
|
|||||||
@ -2,7 +2,11 @@ package com.sczx.gateway;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
@EnableDiscoveryClient
|
||||||
|
@EnableScheduling // 启用定时任务
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class GatewayApplication {
|
public class GatewayApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package com.sczx.gateway.config;
|
package com.sczx.gateway.config;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
@ -12,6 +13,7 @@ import java.net.URI;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
public class CustomRouteConfig {
|
public class CustomRouteConfig {
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -25,9 +27,19 @@ public class CustomRouteConfig {
|
|||||||
.route("sczx_user", r -> r.path("/zc/user/**")
|
.route("sczx_user", r -> r.path("/zc/user/**")
|
||||||
.filters(f -> f.rewritePath("/zc/user/(?<segment>.*)", "/${segment}"))
|
.filters(f -> f.rewritePath("/zc/user/(?<segment>.*)", "/${segment}"))
|
||||||
.uri(uriWithCustomLoadBalance("sczx_user")))
|
.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();
|
.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) {
|
private URI uriWithCustomLoadBalance(String serviceName) {
|
||||||
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
|
||||||
if (instances == null || instances.isEmpty()) {
|
if (instances == null || instances.isEmpty()) {
|
||||||
@ -35,6 +47,8 @@ public class CustomRouteConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ServiceInstance selected = instances.get(Math.abs(index.getAndIncrement()) % instances.size());
|
ServiceInstance selected = instances.get(Math.abs(index.getAndIncrement()) % instances.size());
|
||||||
|
log.info("Selected instance: {}", selected);
|
||||||
return URI.create("http://" + selected.getHost() + ":" + selected.getPort());
|
return URI.create("http://" + selected.getHost() + ":" + selected.getPort());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,6 +18,12 @@ spring:
|
|||||||
# predicates:
|
# predicates:
|
||||||
# - Path=/zc/user/**
|
# - Path=/zc/user/**
|
||||||
# filters:
|
# filters:
|
||||||
|
# - StripPrefix=2
|
||||||
|
# - id: sczx_store
|
||||||
|
# uri: lb://sczx_store
|
||||||
|
# predicates:
|
||||||
|
# - Path=/zc/store/**
|
||||||
|
# filters:
|
||||||
# - StripPrefix=2
|
# - StripPrefix=2
|
||||||
nacos:
|
nacos:
|
||||||
discovery:
|
discovery:
|
||||||
|
|||||||
Reference in New Issue
Block a user