diff --git a/fund-common/src/main/java/com/fundplatform/common/loadbalancer/TenantLoadBalancerAutoConfig.java b/fund-common/src/main/java/com/fundplatform/common/loadbalancer/TenantLoadBalancerAutoConfig.java new file mode 100644 index 0000000..78619e9 --- /dev/null +++ b/fund-common/src/main/java/com/fundplatform/common/loadbalancer/TenantLoadBalancerAutoConfig.java @@ -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; + +/** + * 多租户负载均衡器自动配置 + * + *
当 tenant.routing.enabled=true 时启用租户感知的负载均衡器
+ * + *配置示例:
+ *+ * tenant: + * routing: + * enabled: true + *+ */ +@Configuration +@ConditionalOnProperty(name = "tenant.routing.enabled", havingValue = "true", matchIfMissing = true) +public class TenantLoadBalancerAutoConfig { + + /** + * 注册租户感知负载均衡器 + * + *
替换默认的 RoundRobinLoadBalancer,根据租户组路由实例
+ */ + @Bean + public ReactorServiceInstanceLoadBalancer tenantAwareLoadBalancer( + Environment environment, + LoadBalancerClientFactory loadBalancerClientFactory) { + + String serviceId = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); + + return new TenantAwareLoadBalancer( + serviceId, + loadBalancerClientFactory.getLazyProvider(serviceId, ServiceInstanceListSupplier.class) + ); + } +} diff --git a/fund-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/fund-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..0f45aab --- /dev/null +++ b/fund-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,3 @@ +com.fundplatform.common.loadbalancer.TenantLoadBalancerAutoConfig +com.fundplatform.common.nacos.NacosMetadataConfig +com.fundplatform.common.mybatis.MybatisTenantAutoConfig