飞道的博客

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

308人阅读  评论(0)

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

简介

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

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

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

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


  
  1. package com.knife.router4j.example.order.config;
  2. import org.springframework.cloud.client.ServiceInstance;
  3. import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
  4. import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
  5. import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
  6. import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.core.env.Environment;
  9. public class MyLoadBalancerClientConfiguration {
  10. @Bean
  11. public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer (Environment environment,
  12. LoadBalancerClientFactory loadBalancerClientFactory) {
  13. String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
  14. return new RandomLoadBalancer(
  15. loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
  16. }
  17. }

第2步:@LoadBalancerClients导入配置

法1:放到启动类上


  
  1. package com.knife.router4j.example.order;
  2. import com.knife.common.annotation.CommonApplication;
  3. import com.knife.router4j.example.order.config.MyLoadBalancerClientConfiguration;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
  6. @SpringBootApplication
  7. @EnableDiscoveryClient
  8. @EnableFeignClients("com.knife.**.api")
  9. @LoadBalancerClients(defaultConfiguration = {MyLoadBalancerClientConfiguration.class})
  10. public class OrderApplication {
  11. public static void main (String[] args) {
  12. SpringApplication.run(OrderApplication.class, args);
  13. }
  14. }

法2:放到@Configuration类上 


  
  1. package com.knife.router4j.example.order.config;
  2. import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration(proxyBeanMethods = false)
  5. @LoadBalancerClients(defaultConfiguration = MyLoadBalancerConfiguration.class)
  6. public class DefaultLoadBalancerConfiguration {
  7. }

法2:@LoadBalancerClient(局部)

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


  
  1. package com.knife.router4j.example.order.config;
  2. import org.springframework.cloud.client.ServiceInstance;
  3. import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
  4. import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
  5. import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
  6. import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.core.env.Environment;
  9. public class MyLoadBalancerClientConfiguration {
  10. @Bean
  11. public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer (Environment environment,
  12. LoadBalancerClientFactory loadBalancerClientFactory) {
  13. String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
  14. return new RandomLoadBalancer(
  15. loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
  16. }
  17. }

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

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


  
  1. package com.knife.router4j.example.order;
  2. import com.knife.common.annotation.CommonApplication;
  3. import com.knife.router4j.example.order.config.MyLoadBalancerClientConfiguration;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
  6. @SpringBootApplication
  7. @EnableDiscoveryClient
  8. @EnableFeignClients("com.knife.**.api")
  9. @LoadBalancerClient(name = "storage", configuration = MyLoadBalancerClientConfiguration.class)
  10. public class OrderApplication {
  11. public static void main (String[] args) {
  12. SpringApplication.run(OrderApplication.class, args);
  13. }
  14. }

错误的方法

将负载均衡策略注册为bean

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

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


  
  1. package com.knife.router4j.example.order.config;
  2. import org.springframework.cloud.client.ServiceInstance;
  3. import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
  4. import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
  5. import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
  6. import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.core.env.Environment;
  10. @Configuration
  11. public class LoadBalancerClientConfiguration {
  12. @Bean
  13. public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer (Environment environment,
  14. LoadBalancerClientFactory loadBalancerClientFactory) {
  15. String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
  16. return new RandomLoadBalancer(
  17. loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
  18. }
  19. }

转载:https://blog.csdn.net/feiying0canglang/article/details/126823676
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场