Skip to main content

springcloud

@LoadBalancerClients

是 Spring Cloud Ribbon 提供的注解,它用于在服务调用时进行负载均衡。这个注解用于声明多个服务的负载均衡配置,每个服务都对应着一个@LoadBalancerClient 注解。其中,name 属性表示服务的名称,configuration 属性表示该服务所使用的负载均衡策略。在这个例子中,所有服务都使用的是 GatewayConfig 这个类中定义的负载均衡策略。

@Configuration
@LoadBalancerClients(value = {
//远程服务名称,比如订单服务、商品服务。如果哪个服务想要使用此规则,在这边添加即可
@LoadBalancerClient(name = "th", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "ovisin", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "ivantse", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "id-caon", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "v", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "mprs", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "ectcharge", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "re", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "nan-health", configuration = GatewayConfig.class),
@LoadBalancerClient(name = "artance", configuration = GatewayConfig.class)
}
)
public class GatewayConfig {
@Bean
public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new LockRemoteIpLoadBalancer(
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
}
}

负载均衡策略

image-20240122104857837