Spring Cloud LoadBalancer--指定负载均衡策略--方法/实例

濮阳景天
2023-12-01

原文网址:Spring Cloud LoadBalancer--指定负载均衡策略--方法/实例_IT利刃出鞘的博客-CSDN博客

简介

        本文介绍Spring Cloud LoadBalancer如何使用某个指定的负载均衡策略。

        目标:使用Spring Cloud LoadBalance提供的RandomLoadBalancer作为负载均衡策略。其对应的类为:org.springframework.cloud.loadbalancer.core.RandomLoadBalancer。

法1:启动类加@LoadBalancerClients(全局)

第1步:添加配置类(不要加@Configuration)

package com.knife.router4j.example.order.config;
 
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
 
public class MyLoadBalancerClientConfiguration {
 
	@Bean
	public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
																				   LoadBalancerClientFactory loadBalancerClientFactory) {
		String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
		return new RandomLoadBalancer(
				loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
	}
}

第2步:@LoadBalancerClients导入配置

法1:放到启动类上

package com.knife.router4j.example.order;

import com.knife.common.annotation.CommonApplication;
import com.knife.router4j.example.order.config.MyLoadBalancerClientConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("com.knife.**.api")
@LoadBalancerClients(defaultConfiguration = {MyLoadBalancerClientConfiguration.class})
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

}

法2:放到@Configuration类上 

package com.knife.router4j.example.order.config;

import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@LoadBalancerClients(defaultConfiguration = MyLoadBalancerConfiguration.class)
public class DefaultLoadBalancerConfiguration {
}

法2:@LoadBalancerClient(局部)

第1步:添加配置类(不要加@Configuration)

package com.knife.router4j.example.order.config;
 
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
 
public class MyLoadBalancerClientConfiguration {
 
	@Bean
	public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
																				   LoadBalancerClientFactory loadBalancerClientFactory) {
		String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
		return new RandomLoadBalancer(
				loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
	}
}

第2步:在启动类上添加@LoadBalancerClient

指定哪个服务(本示例为“storage”)使用新的负载均衡策略:

package com.knife.router4j.example.order;

import com.knife.common.annotation.CommonApplication;
import com.knife.router4j.example.order.config.MyLoadBalancerClientConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("com.knife.**.api")
@LoadBalancerClient(name = "storage", configuration = MyLoadBalancerClientConfiguration.class)
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }

}

错误的方法

将负载均衡策略注册为bean

        这样是不行的。因为这样会导致environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);返回为null,后边调用feign时会失败。

        官网也说不能直接用@Configuration注册。见:这里

package com.knife.router4j.example.order.config;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
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;

@Configuration
public class LoadBalancerClientConfiguration {

	@Bean
	public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
																				   LoadBalancerClientFactory loadBalancerClientFactory) {
		String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
		return new RandomLoadBalancer(
				loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
	}
}
 类似资料: