SpringCloud基础知识:Ribbon
1. Ribbon概述
Ribbon是Netflix提供的一个基于HTTP和TCP的客户端负载均衡工具
。
Ribbon主要有两个功能:
-
简化远程调用
-
负载均衡
-
服务端负载均衡
- 负载均衡算法在服务端
- 由负载均衡器维护服务地址列表
- 客户端负载均衡
- 负载均衡算法在客户端
- 客户端维护服务地址列表
2. Ribbon 远程调用
Ribbon可以与简化RestTemplate的远程调用
-
在声明
restTemplate
的Bean时候,添加一-个注解:@LoadBalanced
-
在使用restTemplate发起请求时,需要定义url时,
host:port
可以替换为服务提供方的应用名称
- 修改服务消费者config中的RestTemplateConfig
package com.itheima.consumer.config;
import org.springframework.cloud.client.1oadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@LoadBalance
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 修改eureka-consumer的OrderController 类
package com.itheima.eurekaconsumer.controller;
import com.itheima.eurekaconsumer.domain.Goods;
//import com.netflix.discovery.DiscoveryClient; 不是这一个包
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient; //是这个包
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* 服务的调用方
*/
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/goods2/{id}")
public Goods findGoodsById2(@PathVariable("id") int id){
System.out.println("findGoodsById...."+id);
//String url = "http://localhost:8000/goods/findOne/"+id;
String url = "http://EUREKA-PROVIDER/goods/findOne/"+id;
//调用方法
Goods goods = restTemplate.getForObject(url,Goods.class);
return goods;
}
}
调用
3. Ribbon 负载均衡
Ribbon负载均衡策略:
- 随机: RandomRule
- 轮询: RoundRobinRule
默认
- 最小并发: BestAvailableRule
- 过滤: AvailabilityFilteringRule
- 响应时间: WeightedResponse TimeRule
- 轮询重试: RetryRule
- 性能可用性: ZoneAvoidanceRule
演示
- 为了方便演示,在eureka-provider中
修改GoodsController 代码
把端口号设置在goods 的 tilte 中
package com.itheima.eurekaprovider.controller;
import com.itheima.eurekaprovider.domain.Goods;
import com.itheima.eurekaprovider.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 服务的提供方
*/
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@Value("$server.port")
private int port
@GetMapping("/findOne/{id}")
public Goods findOne(@PathVariable("id") int id){
Goods goods = goodsService.findOne(id);
goods.setTitle(goods.getTitle()+":"+port)
return goods;
}
}
- 修改端口号,启动两个eureka-provider模块
(客户端8001、8002)
当前应用默认只能启动一次,为了模拟两次的效果。需要做以下修改:
测试两个端口都能通过测试
- 不断刷新测试,可以看到每次选择的端口都不同,这就是
负载均衡
。通过负载均衡算法,选择同一个的不同地址。
4. Ribbon负载均衡配置
通过代码实现
- 注意:该配置是 在消费者模块中编写的,即eureka-consumer模块
4. 不断刷新测试,可以看到每次选择的端口都不同且是随机的
,
通过配置实现
- 注意:该配置是在eureka-consumer模块