fix: Gateway添加Nacos服务发现依赖,修复负载均衡器配置

1. fund-gateway pom.xml 添加 spring-cloud-starter-alibaba-nacos-discovery 依赖
2. TenantLoadBalancerAutoConfig 使用 @LoadBalancerClients 注解配置
3. TenantAwareLoadBalancer 增加 serviceId null 检查
This commit is contained in:
zhangjf 2026-02-20 18:43:38 +08:00
parent a78ccaeae5
commit 52c926dcd6
20 changed files with 56 additions and 38 deletions

View File

@ -1,4 +1,4 @@
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Fri Feb 20 18:12:34 CST 2026
#Fri Feb 20 18:33:58 CST 2026
fund-common-0.0.1-SNAPSHOT.jar>=
fund-common-0.0.1-SNAPSHOT.pom>=

View File

@ -3,7 +3,7 @@
<groupId>com.fundplatform</groupId>
<artifactId>fund-common</artifactId>
<versioning>
<lastUpdated>20260220101230</lastUpdated>
<lastUpdated>20260220103348</lastUpdated>
<snapshot>
<localCopy>true</localCopy>
</snapshot>
@ -11,12 +11,12 @@
<snapshotVersion>
<extension>pom</extension>
<value>0.0.1-SNAPSHOT</value>
<updated>20260220101230</updated>
<updated>20260220103348</updated>
</snapshotVersion>
<snapshotVersion>
<extension>jar</extension>
<value>0.0.1-SNAPSHOT</value>
<updated>20260220101230</updated>
<updated>20260220103348</updated>
</snapshotVersion>
</snapshotVersions>
</versioning>

View File

@ -6,6 +6,6 @@
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20260220101230</lastUpdated>
<lastUpdated>20260220103348</lastUpdated>
</versioning>
</metadata>

View File

@ -1,3 +1,3 @@
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Fri Feb 20 18:12:31 CST 2026
#Fri Feb 20 18:33:50 CST 2026
fundplatform-0.0.1-SNAPSHOT.pom>=

View File

@ -3,7 +3,7 @@
<groupId>com.fundplatform</groupId>
<artifactId>fundplatform</artifactId>
<versioning>
<lastUpdated>20260220101230</lastUpdated>
<lastUpdated>20260220103348</lastUpdated>
<snapshot>
<localCopy>true</localCopy>
</snapshot>
@ -11,7 +11,7 @@
<snapshotVersion>
<extension>pom</extension>
<value>0.0.1-SNAPSHOT</value>
<updated>20260220101230</updated>
<updated>20260220103348</updated>
</snapshotVersion>
</snapshotVersions>
</versioning>

View File

@ -6,6 +6,6 @@
<versions>
<version>0.0.1-SNAPSHOT</version>
</versions>
<lastUpdated>20260220101230</lastUpdated>
<lastUpdated>20260220103348</lastUpdated>
</versioning>
</metadata>

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699084
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008125

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699091
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008166

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699097
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008178

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699096
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008176

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699093
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008171

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699090
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008165

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699098
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008180

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699092
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008169

View File

@ -1,2 +1,2 @@
#Fri Feb 20 11:21:39 CST 2026
central|https\://repo1.maven.org/maven2|null=1771557699094
#Fri Feb 20 18:40:08 CST 2026
central|https\://repo1.maven.org/maven2|null=1771584008173

View File

@ -62,6 +62,12 @@ public class TenantAwareLoadBalancer implements ReactorServiceInstanceLoadBalanc
@Override
public Mono<Response<ServiceInstance>> choose(Request request) {
// 如果 serviceId null直接返回空响应
if (serviceId == null || serviceId.isEmpty()) {
logger.warn("[TenantLB] serviceId 为空,无法进行负载均衡");
return Mono.just(new EmptyResponse());
}
// 检查是否启用租户路由
if (routingProperties != null && !routingProperties.isEnabled()) {
logger.debug("[TenantLB] 租户路由已禁用,使用默认路由");

View File

@ -3,6 +3,7 @@ package com.fundplatform.common.loadbalancer;
import com.fundplatform.common.config.TenantRoutingProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
@ -30,10 +31,23 @@ import org.springframework.core.env.Environment;
*/
@Configuration
@ConditionalOnProperty(name = "tenant.routing.enabled", havingValue = "true", matchIfMissing = true)
@LoadBalancerClients(defaultConfiguration = TenantLoadBalancerConfig.class)
public class TenantLoadBalancerAutoConfig {
@Autowired(required = false)
private TenantRoutingProperties routingProperties;
}
/**
* 租户感知负载均衡器配置类
*
* <p>为每个服务创建租户感知的负载均衡器实例</p>
*/
@Configuration
class TenantLoadBalancerConfig {
@Autowired(required = false)
private TenantRoutingProperties routingProperties;
/**
* 注册租户感知负载均衡器

View File

@ -35,6 +35,12 @@
<version>4.0.0</version>
</dependency>
<!-- Nacos Service Discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Redis for Rate Limiting & Token Storage -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -71,7 +71,8 @@ spring:
- id: fund-cust
uri: lb://fund-cust
predicates:
- Path=/cust/**
- Path=/cust/**Default Namespace
filters:
- StripPrefix=1
@ -128,16 +129,7 @@ logging:
org.springframework.cloud.gateway: DEBUG
com.fundplatform.common.loadbalancer: DEBUG
# 多租户路由配置Gateway 全局配置)
# 多租户路由配置
tenant:
routing:
enabled: true
tenant-header: X-Tenant-Id
default-tenant-id: "1"
# 共享服务列表(不需要租户路由的服务)
shared-services:
- fund-gateway
- fund-report
- fund-file
# 默认回退策略
fallback-to-shared: true