2_Ribbon客户端负载均衡和传参方式
前言
上一篇讲了Eureke 服务注册和发现,已经服务提供这和服务消费者ribbon,下面这个我们重点讲下Ribbon客户端的负载均衡和传参的方式,那么开始之前,我要先讲下负载均衡
1、负载均衡
1.1、服务端负载均衡
我们通常说的负载均衡都是服务端的负载均衡,其中分为硬件负载均衡和软件负载均衡,
硬件负载均衡主要是服务器节点之间专门安装用于负载均衡的设备,如F5
软件负载均衡则通过在服务器上安装具有负载均衡的功能或模块来完成请求分发,比如Nginx等。
原理: 当客户段发送请求到负载均衡设备的时候,该设备按照某种算法(轮询,权重负载,按照流量负载)
1.2、客户端负载均衡(本篇)
客户端负载均衡所有的客户端节点都维护着自己要访问的服务器清单,而这些服务器清单来自于服务注册中心,
2、Get请求
2.1、GET请求(restTemplate.getForEntity)
2.1.1、服务端提供者的Controller代码
@RestController
public class HomeController {
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "hello",method = RequestMethod.GET)
@ResponseBody
public String index(String str){
ServiceInstance serviceInstance = client.getLocalServiceInstance();
System.out.println(
"host:"+serviceInstance.getHost()+"/n " +
"service_id :"+serviceInstance.getServiceId());
return str;
}
}
2.1.2、服务端消费者代码
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@ResponseBody
@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello?str=healejean",String.class).getBody();
}
}
2.1.3、浏览器查看 http://localhost:9000/ribbon-consumer
2.2、占位符{1}和请求获取对象
2.2.1、服务提供者方法
@RequestMapping(value = "user_url",method = RequestMethod.GET)
@ResponseBody
public User index(User user){
ServiceInstance serviceInstance = client.getLocalServiceInstance();
System.out.println(
"host:"+serviceInstance.getHost()+"/n " +
"service_id :"+serviceInstance.getServiceId());
user.setId("1");
return user;
}
2.2.2、服务消费者获取对象
解释:
1、这里的Uerl对象内容和上面的内容是一样的,但是位置不是同一个的位置
2、{1} 表示占位符,1表示第一个
3、第三位参数是一个Object数组
@ResponseBody
@RequestMapping(value = "/ribbon-consumer_user", method = RequestMethod.GET)
public User userUrl() {
ResponseEntity<User> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/user_url?name={1}",User.class,"HealerJean");
User body = responseEntity.getBody();
return body;
}
2.2.3、map传参占位符{name}
@ResponseBody
@RequestMapping(value = "/ribbon-consumer_userMap", method = RequestMethod.GET)
public User userUrlMap() {
Map<String,Object> map = new HashMap<>();
map.put("name","HealerJean");
ResponseEntity<User> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/user_url?name={name}",User.class,map);
User body = responseEntity.getBody();
return body;
}
2.3、get的第二中请求方式 restTemplate.getForObject
解释:传参形式和上面的getForEntity是一样的,只不过不到需要.getBody了
2.3.1
@ResponseBody
@RequestMapping(value = "/ribbon-consumer_getTwo", method = RequestMethod.GET)
public User getTwo() {
User body = restTemplate.getForObject("http://HELLO-SERVICE/user_url?name={1}",User.class,"HealerJean");
return body;
}
3、post请求
解释:(传递的方式其实相当于表单)
3.1、第一种方式 postForEntity
3.1.1、服务提供者
@RequestMapping(value = "user_urlPost",method = RequestMethod.POST)
@ResponseBody
public User urlPost(User user){
ServiceInstance serviceInstance = client.getLocalServiceInstance();
System.out.println(
"host:"+serviceInstance.getHost()+"/n " +
"service_id :"+serviceInstance.getServiceId());
user.setId("1");
return user;
}
3.1.2、发起Post请求
@ResponseBody
@RequestMapping(value = "/ribbon-consumer_postFirst", method = RequestMethod.GET)
public User postFirst() {
User user = new User();
user.setName("HealerJean");
ResponseEntity<User> responseEntity =
restTemplate.postForEntity("http://HELLO-SERVICE/user_urlPost",user,User.class);
User body = responseEntity.getBody();
return body;
}
3.2、第二种方式 postForObject(和上面的用法一样,不讲解了)
3、put请求
解释:一般情况下表示更新
@ResponseBody
@RequestMapping(value = "/ribbon-consumer_put", method = RequestMethod.GET)
public User put() {
User user = new User();
user.setName("HealerJean");
String id= "2";
restTemplate.put("http://HELLO-SERVICE/user_url/{1}",user,id);
return user;
}
4、delete请求
解释:delete传递的参数主要是id
/**
* delete 请求
*/
@ResponseBody
@RequestMapping(value = "/ribbon-consumer_delete", method = RequestMethod.GET)
public User delete() {
String id= "2";
restTemplate.delete("http://HELLO-SERVICE/user_url/{1}",id);
return null;
}
HaelerJean-代码下载
感兴趣的,欢迎添加博主微信,
哈,博主很乐意和各路好友交流,如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,备注您的微信或者其他联系方式。添加博主微信哦。
请下方留言吧。可与博主自由讨论哦
微信 | 微信公众号 | 支付宝 |
---|---|---|
![]() |
![]() |
![]() |