fix: 注册 TenantAwareLoadBalancer 为 Spring Bean

问题:TenantAwareLoadBalancer 类存在但未被注册为 Spring Bean

解决方案:
1. 创建 TenantLoadBalancerAutoConfig 配置类
   - @ConditionalOnProperty 启用条件配置
   - @Bean 注册 ReactorServiceInstanceLoadBalancer

2. 添加 Spring Boot 自动配置注册
   - META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
   - 注册 TenantLoadBalancerAutoConfig、NacosMetadataConfig、MybatisTenantAutoConfig
This commit is contained in:
zhangjf 2026-02-19 20:57:09 +08:00
parent 2c0f2f8952
commit 2825050502
2 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package com.fundplatform.common.loadbalancer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* 多租户负载均衡器自动配置
*
* <p> tenant.routing.enabled=true 时启用租户感知的负载均衡器</p>
*
* <p>配置示例</p>
* <pre>
* tenant:
* routing:
* enabled: true
* </pre>
*/
@Configuration
@ConditionalOnProperty(name = "tenant.routing.enabled", havingValue = "true", matchIfMissing = true)
public class TenantLoadBalancerAutoConfig {
/**
* 注册租户感知负载均衡器
*
* <p>替换默认的 RoundRobinLoadBalancer根据租户组路由实例</p>
*/
@Bean
public ReactorServiceInstanceLoadBalancer tenantAwareLoadBalancer(
Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory) {
String serviceId = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new TenantAwareLoadBalancer(
serviceId,
loadBalancerClientFactory.getLazyProvider(serviceId, ServiceInstanceListSupplier.class)
);
}
}

View File

@ -0,0 +1,3 @@
com.fundplatform.common.loadbalancer.TenantLoadBalancerAutoConfig
com.fundplatform.common.nacos.NacosMetadataConfig
com.fundplatform.common.mybatis.MybatisTenantAutoConfig