文章目录
概述
SpringCloud Ribbon是基于Netflix Ribbon实现的一套客户端复杂均衡的工具。
简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的负载均衡算法和服务调用。Ribbon客户端组件提供一系列完善的配置项如连接超时、重试等。简单地说,就是在配置文件中列出Load Balancer所有的机器,Ribbon会自动的帮助你基于某种规则(如轮序、随机等)去连接这些机器。我们同样很容易使用Ribbon实现自定义的负载均衡算法。
Ribbon目前已经进入维护模式
SpringCloud提供的替换方案是LoadBalancer
负载均衡(Load Balance)是什么
简单地说,就是将用户的请求平摊到多个服务上,从而到达系统的HA(高可用)。常见的负载均衡软件有Nginx,LVS,硬件F5等。
Ribbon客户端本地负载均衡 VS Nginx服务端负载均衡
Ribbon本地负载均衡,在调用微服务接口时,会在注册中心上获取注册信息服务列表之后缓存到JVM本地,从而在本地实现RPC远程服务调用技术。
Nginx服务器负载均衡,客户端所有请求都会交给Nginx,然后由Nginx实现转发请求。
Eureka+Ribbon+RestTemplate使用
- pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Eureka依赖中已经包含了Ribbon的依赖。
- yaml
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: false
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#单机
#defaultZone: http://localhost:7001/eureka
# 集群
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
- 主启动类
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80
{
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
- @LoadBalancer
@Configuration
public class ApplicationContextConfig
{
@Bean
@LoadBalanced
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}
- RestTemplate
//返回的对象为响应体中数据转化成的对象,基本上可以理解为JSON
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment)
{
return restTemplate.postForObject(PAYMENT_URL +"/payment/create",payment,CommonResult.class);
}
//返回的对象为响应体中数据转化成的对象,基本上可以理解为JSON
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id)
{
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
//返回对象为ResponseEntity,包含了响应中的一些重要信息,响应头,响应状态码,响应体等
@GetMapping("/consumer/payment/getForEntity/{id}")
public CommonResult<Payment> getPayment2(@PathVariable("id") Long id)
{
ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
if(entity.getStatusCode().is2xxSuccessful()){
return entity.getBody();
}else{
return new CommonResult<>(444,"操作失败");
}
}
核心组件IRule
根据特定算法从服务列表中选择一个要访问的服务
- com.netflix.loadbalancer.RoundRobinRule 轮询
- com.netflix.loadbalancer.RandomRule 随机
- com.netflix.loadbalancer.RetryRule 先按照RoundRobinRule的策略获取服务,如果获取服务失败则在指定时间内进行重试,获取可用的服务
- WeightedResponseTimeRule 对RoundRobinRule的扩展,响应速度越快的实例选择权重越多大,越容易被选择
- BestAvailableRule 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务
- AvailabilityFilteringRule 先过滤掉故障实例,再选择并发较小的实例
- ZoneAvoidanceRule 复合判断server所在区域的性能和server的可用性选择服务器
规则替换
注意!自定义配置类不能放在@ComponentScan所扫描的当前包以及子包下,否则自定义的配置类就会被所有Ribbon客户端共享,不能达到定值的目的。
与SpringCloud包并行创建一个包,新建MySelfRule规则类
@Configuration
public class MySelfRule
{
@Bean
public IRule myRule()
{
return new RandomRule();//定义为随机
}
}
主启动类添加@RibbonClient
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration=MySelfRule.class)
public class OrderMain80
{
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}