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
来实现负载均衡,使用方式如下
-
给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(); } }
-
修改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; } }
-
启动多个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
,如果服务的消费者需要采用随机的负载均衡策略也可以自行定义:
具体操作:
-
定义随机算法对象,通过@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); } }
-
在
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
服务实例的调用就是随机的了!!
发表评论