2_Ribbon客户端负载均衡和传参方式
前言
Github:https://github.com/HealerJean
上一篇讲了Eureka
服务注册和发现,已经服务提供这和服务消费者Ribbon
,下面这个我们重点讲下Ribbon客户端的负载均衡和传参的方式,那么开始之前,我要先讲下负载均衡
restTemplate.getForObject
和restTemplate.getForEntity
是一样的,只不过不到需要.getBody了
@GetMapping(value = "/c_getForObjectTest")
public UserDTO getForObject() {
UserDTO userDTO = restTemplate.getForObject(
"http://" + serverProviderName + "/api/provider/urlGet?name={1}",
UserDTO.class, "HealerJean");
return userDTO;
}
1、负载均衡
1.1、服务端负载均衡
我们通常说的负载均衡都是服务端的负载均衡,其中分为硬件负载均衡和软件负载均衡
硬件负载均衡主要是服务器节点之间专门安装用于负载均衡的设备,如F5
软件负载均衡则通过在服务器上安装具有负载均衡的功能或模块来完成请求分发,比如Nginx等。
原理: 当客户段发送请求到负载均衡设备的时候,该设备按照某种算法(轮询,权重负载,按照流量负载)
1.2、客户端负载均衡(本篇)
客户端负载均衡所有的客户端节点都维护着自己要访问的服务器清单,而这些服务器清单来自于服务注册中心,
2、服务提供者代码
package com.healerjean.proj.controller;
import com.healerjean.proj.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
/**
* @author HealerJean
* @ClassName HomeController
* @date 2020/4/8 17:03.
* @Description
*/
@Api(description = "服务提供者_2001_控制器")
@RestController
@RequestMapping("api/provider")
@Slf4j
public class ProviderController extends BaseController {
@Autowired
private DiscoveryClient discoveryClient;
@ApiOperation(value = "urlGet",
notes = "urlGet",
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
response = UserDTO.class)
@GetMapping(value = "urlGet")
@ResponseBody
public UserDTO urlGet(UserDTO user) {
user.setId("18");
return user;
}
@ApiOperation(value = "urlPost",
notes = "urlPost",
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
response = UserDTO.class)
@PostMapping(value = "urlPost")
@ResponseBody
public UserDTO urlPost(UserDTO user) {
user.setId("1");
return user;
}
}
3、普通请求测试
3.1、Get请求
3.1.1、占位符:{1}、{2}
1、这里的Uerl对象内容和上面的内容是一样的,但是位置不是同一个的位置
2、{1} 表示占位符,1表示第一个
@GetMapping(value = "/c_get_url")
public UserDTO userUrl() {
ResponseEntity<UserDTO> responseEntity = restTemplate.getForEntity(
"http://" + serverProviderName + "/api/provider/urlGet?name={1}",
UserDTO.class, "HealerJean");
UserDTO body = responseEntity.getBody();
return body;
}
3.1.2、map传参占位符:{name}
@RequestMapping(value = "c_get_map", method = RequestMethod.GET)
public UserDTO userUrlMap() {
Map<String, Object> map = new HashMap<>();
map.put("name", "HealerJean");
ResponseEntity<UserDTO> responseEntity = restTemplate.getForEntity(
"http://" + serverProviderName + "/api/provider/urlGet?name={name}",
UserDTO.class, map);
UserDTO body = responseEntity.getBody();
return body;
}
3.2、post请求
@GetMapping(value = "/c_post")
public UserDTO postFirst() {
UserDTO user = new UserDTO();
user.setName("HealerJean");
ResponseEntity<UserDTO> responseEntity =
restTemplate.postForEntity("
http://" + serverProviderName + "/api/provider/urlPost",
user,
UserDTO.class);
UserDTO body = responseEntity.getBody();
return body;
}
3.3、put请求
@GetMapping(value = "/c_put")
public UserDTO put() {
UserDTO user = new UserDTO();
user.setName("HealerJean");
String id = "2";
restTemplate.put("http://" + serverProviderName + "/api/provider/put/{1}", user, id);
return user;
}
3.4、delete请求
@GetMapping(value = "c_delete")
public UserDTO delete() {
String id = "2";
restTemplate.delete("http://" + serverProviderName + "/api/provider/delete/{1}", id);
return null;
}