当前位置: 代码网 > it编程>编程语言>Java > SpringCloud 负载均衡

SpringCloud 负载均衡

2024年08月03日 Java 我要评论
负责均衡 (Load Balance,简称LB),是高并发、高可用系统必不可少的关键组件。当服务流量增大时,通常会采用增加机器的方式来进行扩容,负载均衡就是用来在多个机器或者其它资源中按照一定的规则合理分配负载

spring cloud 负载均衡

1. 概念

负责均衡 (load balance,简称lb),是高并发、高可用系统必不可少的关键组件。

当服务流量增大时,通常会采用增加机器的方式来进行扩容,负载均衡就是用来在多个机器或者其它资源中按照一定的规则合理分配负载

2. 分类

负责均衡分为服务端负载均衡和客户端负载均衡:

2.1 服务端负载均衡

在服务端进行负责均衡的算法分配,比较有名的服务端负载均衡器是nginx,请求先到达nginx负载均衡器,然后通过负载均衡算法,在多个服务器之间选择一个进行访问:

在这里插入图片描述

2.2 客户端负载均衡

在客户端进行负载均衡的算法分配.

把负责均衡的功能以库的方式集成到客户端,而不再是由一台指定的负载均衡设备集中提供

在这里插入图片描述

ribbon是spring cloud早期的默认实现,由于现在已不再维护,最新版本的spring cloud负载均衡集成的是由spring cloud官方维护的 spring cloud loadbalancer

3. spring cloud loadbalancer

在springcloud中,我们使用spring cloud loadbalancer来实现负载均衡,使用方式如下

  1. resttemplate这个bean添加 @loadbalanced注解

    package com.order.config;
    
    import org.springframework.cloud.client.loadbalancer.loadbalanced;
    import org.springframework.context.annotation.bean;
    import org.springframework.context.annotation.configuration;
    import org.springframework.web.client.resttemplate;
    
    @configuration
    public class beanconfig {
        
        @bean
        @loadbalanced
        public resttemplate resttemplate() {
            return new resttemplate();
        }
    
    }
    
  2. 修改ip端口号为服务名称(orderserviceimpl)

    package com.order.service.impl;
    
    import com.order.mapper.ordermapper;
    import com.order.model.orderinfo;
    import com.order.model.productinfo;
    import com.order.service.orderservice;
    import org.springframework.beans.factory.annotation.autowired;
    import org.springframework.cloud.client.serviceinstance;
    import org.springframework.cloud.client.discovery.discoveryclient;
    import org.springframework.stereotype.service;
    import org.springframework.web.client.resttemplate;
    
    import java.util.list;
    
    @service
    public class orderserviceimpl implements orderservice {
    
        @autowired
        private ordermapper ordermapper;
    
        @autowired
        private discoveryclient discoveryclient;
    
        @autowired
        private resttemplate resttemplate;
        @override
        public orderinfo selectorderbyid(integer orderid) {
    
            orderinfo orderinfo = ordermapper.selectorderbyid(orderid);
    //        string url = "http://127.0.0.1:9090/product/" + orderinfo.getproductid();
            string url = "http://product-service/product/" + orderinfo.getproductid();
            productinfo productinfo = resttemplate.getforobject(url, productinfo.class);
            orderinfo.setproductinfo(productinfo);
            return orderinfo;
        }
    }
    
  3. 启动多个product-service实例

    先复制一份实例:

    在这里插入图片描述

    更改实例名称,并修改选项:

    在这里插入图片描述

    选择 add vm options 选项:

    在这里插入图片描述

    添加 -dserver.port=自定义端口号

    在这里插入图片描述

部署好后同时启动多个product-service实例,并刷新eureka-server

在这里插入图片描述

在这里插入图片描述

可以看到新创建的几个实例都加入了注册中心!

这个时候我们来调用order-service服务,并多次刷新调用,可以看到每个product-service都被均衡的调用到了

在这里插入图片描述

4. 负载均衡策略

负载均衡策略是一种思想,无论是哪种负载均衡器,它们的负载均衡策略都是相似的。spring cloud loadbalancer 仅支持两种负载均衡策略:轮询策略随机策略

  • 轮询(round robin):指服务器轮流处理用户的请求,这个一种实现最简单,也最常用的策略
  • 随机选择(random):随机选择一个后端服务器来处理新的请求

spring cloud loadbalancer 默认负载均衡策略是 轮询策略,实现的是roundrobinloadbalancer,如果服务的消费者需要采用随机的负载均衡策略也可以自行定义:

具体操作:

  1. 定义随机算法对象,通过@bean将器加载到spring容器中

    package com.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 loadbalancerclient {
        @bean
        reactorloadbalancer<serviceinstance> randomloadbalancer(environment environment,
                                                                loadbalancerclientfactory loadbalancerclientfactory) {
            string name = environment.getproperty(loadbalancerclientfactory.property_name);
            return new randomloadbalancer(loadbalancerclientfactory
                    .getlazyprovider(name, serviceinstancelistsupplier.class),
                    name);
        }
    }
    
  2. resttemplate配置类上方使用@loadbalancerclient 或者 @loadbalancerclients 注解(根据不同的服务提供方配配置不同的客户端负载均衡算法策略,这里我们使用@loadbalancerclient )

    package com.order.config;
    
    import org.springframework.cloud.client.loadbalancer.loadbalanced;
    import org.springframework.cloud.loadbalancer.annotation.loadbalancerclient;
    import org.springframework.context.annotation.bean;
    import org.springframework.context.annotation.configuration;
    import org.springframework.web.client.resttemplate;
    
    @loadbalancerclient(name = "product-service", configuration = customloadbalancerconfiguration.class)
    @configuration
    public class beanconfig {
    
        @bean
        @loadbalanced
        public resttemplate resttemplate() {
            return new resttemplate();
        }
    
    }
    

    @loadbalancerclient注解中的name表示该负载均衡策略对哪个服务生效(服务提供方),configuration表示该负载均衡器使用哪个负载均衡策略
    配置完成后,后续对product-service服务实例的调用就是随机的了!!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com