• springcloud的负载均衡两种实现方式


    Ribbon

    Ribbon是什么

    Ribbon是Netflix发布的云中间层服务开源项目,其主要功能是提供客户端实现负载均衡算法。Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,Ribbon是一个客户端负载均衡器,我们可以在配置文件中Load Balancer后面的所有机器,Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器,我们也很容易使用Ribbon实现自定义的负载均衡算法。

    Eureka结合Ribbon的使用

    Ribbon工作流程

    Ribbon工作时分为两步:

    第一步选择EurekaServer,它优先选择在同一个Zone且负载较少的Server;

    第二步再根据用户指定的策略,再从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了很多策略,例如轮询round robin、随机Random、根据响应时间加权等。

    Ribbon的使用

    如何集成Ribbon?

    查看Springcloud官方文档,搜索Ribbon。

    ① 首先引入Ribbon依赖

    按照官方的意思是需要加入以下依赖

    org.springframework.cloud

    spring-cloud-starter-ribbon

    但是其实是不需要的加入这个依赖的,在spring-cloud-starter-eureka依赖中就已经包含了RibbonStarter (上节已知spring-cloud-starter-eureka-server是为编写EurekaServer提供依赖,spring-cloud-starter-eureka是为编写EurekaClient提供依赖),因此只需要EurekaClient具有spring-cloud-starter-eureka依赖即可。即在POM中需要有

    
    org.springframework.cloud
    spring-cloud-starter-eureka
    
    
    • 1
    • 2
    • 3
    • 4

    ②如何使用Ribbon

    在这里其实是服务消费者使用RestTemplate)之间的通信,为RestTemplate配置类添加@LoadBalanced注解即可,如下所示:

    @Bean
    @LoadBalanced
      public RestTemplate restTemplate() {
      return new RestTemplate();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ③如何解决硬编码

    使用添加@LoadBalanced注解后的RestTemplate调用服务提供者的接口时,可以使用虚拟IP替代真实IP地址。所谓的虚拟IP就是服务提供者在application.properties或yml文件中配置的spring.application.name属性的值。示例如下:

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
      // VIP: Virtual IP http://microservice-provider-user/即虚拟IP 服务提供者的ServiceId (spring.application.name)
      return this.restTemplate.getForObject("http://microservice-provider-user/simple/" + id, User.class);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    自定义RibbonClient

    如何为服务消费者自定义RibbonClient?

    ①代码自定义RibbonClient

    所谓的自定义RibbonClient的主要作用就是使用自定义配置替代Ribbon默认的负载均衡策略,注意:自定义的RibbonClient是有针对性的,一般一个自定义的RibbonClient是对一个服务提供者(包括服务名相同的一系列副本)而言的。自定义了一个RibbonClient它所设定的负载均衡策略只对某一特定服务名的服务提供者有效,但不能影响服务消费者与别的服务提供者通信所使用的策略。根据官方文档的意思,推荐在springboot主程序扫描的包范围之外进行自定义配置类。其实纯代码自定义RibbonClient的话有两种方式:

    方式一:在springboot主程序扫描的包外定义配置类,然后为springboot主程序添加@RibbonClient注解引入配置类

    注意:@RibbonClient注解中的name属性是指服务提供者的服务名(即当前消费者使用自定义配置与其通信的服务提供者的spring.application.name的属性)

    @RibbonClient(name = "microservice-provider-user",configuration = TestConfiguration.class)
    
    • 1

    方式二:在与springboot主程序的同一级目录新建RibbonClient的配置类,但是必须在springboot扫描的包范围内排除掉,方法是自定义注解标识配置类,然后在springboot的添加@ComponentScan根据自定义注解类型过滤掉配置类

    自定义注解

    public @interface ExcludeFromComponentScan {
    }
    
    • 1
    • 2

    自定义配置类

    @Configuration
    @ExcludeFromComponentScan
    public class TestConfiguration1 {
        @Autowired
        private IClientConfig config;
        @Bean
        public IRule ribbonRule(IClientConfig config) { // 自定义为随机规则
            return new RandomRule();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在springboot主程序上添加注解

    @RibbonClient(name = "microservice-provider-user",configuration = TestConfiguration1.class)
    @ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = ExcludeFromComponentScan.class)})
    
    • 1
    • 2

    Ribbon负载均衡策略有以下几种:

    策略类

    名称

    说明

    RandomRule

    随机策略

    随机选择 Server

    RoundRobinRule

    轮训策略

    按顺序循环选择 Server

    RetryRule

    重试策略

    在一个配置时问段内当选择 Server 不成功,则一直尝试选择一个可用的 Server

    BestAvailableRule

    最低并发策略

    逐个考察 Server,如果 Server 断路器打开,则忽略,再选择其中并发连接最低的 Server

    AvailabilityFilteringRule

    可用过滤策略

    过滤掉一直连接失败并被标记为 circuit tripped 的 Server,过滤掉那些高并发连接的 Server(active connections 超过配置的网值)

    ResponseTimeWeightedRule

    响应时间加权策略

    根据 Server 的响应时间分配权重。响应时间越长,权重越低,被选择到的概率就越低;响应时间越短,权重越高,被选择到的概率就越高。这个策略很贴切,综合了各种因素,如:网络、磁盘、IO等,这些因素直接影响着响应时间

    ZoneAvoidanceRule

    区域权衡策略

    综合判断 Server 所在区域的性能和 Server 的可用性轮询选择 Server,并且判定一个 AWS Zone 的运行性能是否可用,剔除不可用的 Zone 中的所有 Server

    OpenFeign

    Feign 是一个声明性的 web 服务客户端。 它使编写 web 服务客户端更加容易。 使用 Feign 创建一个接口并对其进行注释。 它具有可插拔的注释支持,包括 Feign 注释和 JAX-RS 注释。 Feign 还支持可插拔编码器和解码器。 Spring Cloud 增加了对 Spring MVC 注释的支持,以及对使用 Spring Web 默认使用的 HttpMessageConverters 的支持。 Spring Cloud 集成了 Ribbon 和 Eureka,以及 Spring Cloud LoadBalancer,以便在使用 Feign 时提供负载平衡的 http 客户端

    OpenFeign的使用

    创建项目

    配置项目

    主启动类添加以下注解

    添加@EnableFeignClients和@EnableDiscoveryClient注解

    @SpringBootApplication
    @EnableFeignClients
    @EnableDiscoveryClient
    public class OpenFeignApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(OpenFeignApplication.class, args);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    pom文件

    
    
    	4.0.0
    	
    		com.milo
    		milgenius-springcloud
    		1.0.0
    	
    	com.milo
    	springcloud-openfeign
    	1.0.0
    	springcloud-openfeign
    	openfeign学习项目
    
    	
    		1.8
    		Greenwich.SR5
    	
    
    	
    		
    			org.springframework.boot
    			spring-boot-starter-web
    		
    		
    			org.springframework.cloud
    			spring-cloud-starter-netflix-eureka-client
    		
    		
    			org.springframework.cloud
    			spring-cloud-starter-openfeign
    		
    
    		
    			org.springframework.boot
    			spring-boot-starter-test
    			test
    		
    	
    
    	
    		
    			
    				org.springframework.cloud
    				spring-cloud-dependencies
    				${spring-cloud.version}
    				pom
    				import
    			
    		
    	
    
    	
    		
    			
    				org.springframework.boot
    				spring-boot-maven-plugin
    			
    		
    	
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    application.yml

    ## 注册中心
    eureka:
      client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/
    
    
    server:
      port: 8766
    
    spring:
      application:
        name: service-openfeign
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    相关代码

    ConsumerClient

    @FeignClient("service-provider")
    @Component
    public interface ConsumerClient {
    
           @GetMapping("/hello")
           String hello();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ConsumerController

    @RestController
    public class ConsumerController {
    
        @Autowired
        private IConsumerService consumerService;
    
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public String index(@RequestParam String name){
            return  consumerService.hello(name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    IConsumerService

    public interface IConsumerService {
    
        String hello(String name);
    }
    
    • 1
    • 2
    • 3
    • 4

    ConsumerServiceImpl

    @Service
    public class ConsumerServiceImpl implements IConsumerService {
    
        //注入client
        @Autowired
        private ConsumerClient client;
    
        @Override
        public String hello(String name) {
            return client.hello();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    Elasticsearch使用mapping映射定义以及基本的数据类型
    Mysql004:用户管理
    在vue3中如何使用百度地图API(详细步骤+demo示例)
    Python3用Django连接Mysql以及学习过程中的一些问题
    Windows ssh免密访问Linux服务器
    NX许可证错误:VD is starting, please check vendor daemon s status in debug log
    Unity 快捷键的一些记录
    【已拿offer】最新AI产品经理大厂面经(含百度&腾讯&科大讯飞&商汤&蚂蚁金服)
    MySQL数据类型总结与使用
    Java基础之接口(interface)详解
  • 原文地址:https://blog.csdn.net/jiey0407/article/details/126516389