在上一篇【Spring Cloud(F版)Ribbon入门】中,我们简单的实现了客户端负载均衡,默认是轮询的方式。本文将继续按照我之前的学习路径,介绍微服务调用的方法,首先我们来看看上一篇中就使用到的RestTemplate对象中最简单的功能getForEntity发起一个get请求,接下来我们详细来了解一下RestTemplate对象。
——————————————————————————————————————————————————————
RestTemplate实现方法
在启动项中,@Bean来加载RestTemplate对象,使用@LoadBalanced来开启客户端负载均衡。
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientOrderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientOrderApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
然后在Controller中添加依赖注入,通过getForEntity来调用服务即可。
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value="helloConsumer", method = RequestMethod.GET)
public String helloConsumer(){
return restTemplate.getForEntity("http://CLIENT-USER/hello",String.class).getBody();
}
}
请求方式
- GET请求
- POST请求
- PUT请求
环境搭建
这是我搭建的一个maven多模块的测试项目,如果不知道IDEA如何创建多模块项目的可以看一下【IntelliJ IDEA创建Maven多模块项目】这篇文章。
此项目中,commons是公共类模块,存放实体类、工具类等,是一个普通的java项目,consumers是消费者,这里我创建两个模块分别是移动端和pc端这两个消费者,providers是提供者,同样也创建了用户以及订单两个提供者。
eureka-server则是服务注册中心,这个就不过多讲解了可以看之前的几篇文章。
这里提供者和消费者都需要引入commons模块的依赖
<dependency>
<groupId>com.clarezhou.example</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
GET请求方式
getForEntity方法
getForEntity方法的返回值是一个ResponseEntity,ResponseEntity是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。
@RequestMapping(value="forentityHello", method = RequestMethod.GET)
public String forentityHello(){
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://PROVIDER-USER/hello/sayHello",String.class);
String body = responseEntity.getBody();
HttpStatus statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCodeValue();
HttpHeaders headers = responseEntity.getHeaders();
log.info("getStatusCode:"+statusCode+" getStatusCodeValue:"+ statusCodeValue+" Headers:" +headers);
return body;
}
结果如下:
getStatusCode:200 getStatusCodeValue:200 Headers:{Content-Type=[text/plain;charset=UTF-8], Content-Length=[11], Date=[Fri, 26 Jul 2019 14:54:40 GMT]}
当然,我们调用服务接口肯定是需要传递参数,下面两种方式都是可行的。
@RequestMapping(value="helloByName", method = RequestMethod.GET)
public String helloConsumer(String name){
return restTemplate.getForEntity("http://PROVIDER-USER/hello/sayHelloByName?name={1}",String.class,name).getBody();
}
@RequestMapping(value="helloBymap", method = RequestMethod.GET)
public String helloBymap(String name){
Map<String,Object> map = new HashMap<>();
map.put("name",name);
return restTemplate.getForEntity("http://PROVIDER-USER/hello/sayHelloByName?name={name}",String.class,map).getBody();
}
返回除了String类型,当然还有各种自定义的实体对象,这里我简单来获取一下用户信息的例子。
提供者:
@RequestMapping("getUserInfo")
public UserInfoEntity getUserInfo(){
UserInfoEntity user = new UserInfoEntity();
user.setUsername("张三");
user.setPassword("123456");
user.setEmail("132456789@qq.com");
user.setMobilePhone("13888888888");
return user;
}
消费者:
@RequestMapping(value="getUserInfo",method = RequestMethod.GET)
public UserInfoEntity getUserInfo(){
return restTemplate.getForEntity("http://PROVIDER-USER/hello/getUserInfo",UserInfoEntity.class).getBody();
}
结果如下:
getForObject
getForObject其实就是对getForEntity进行了一次封装,不在返回ResponseEntity,只返回消息体的内容,如下例子得到的结果跟上面是一样的。
@RequestMapping(value="getUserInfo",method = RequestMethod.GET)
public UserInfoEntity getUserInfo(){
return restTemplate.getForObject("http://PROVIDER-USER/hello/getUserInfo",UserInfoEntity.class);
}
POST请求
1、postForEntity
postForEntity和getForEntity类似,返回的也是ResponseEntity实体,看如下例子:
提供者:
@RequestMapping(value = "getUserInfoPost",method = RequestMethod.POST)
public UserInfoEntity getUserInfoPost(@RequestBody Map<String,Object> map){
log.info("姓名:"+(String) map.get("name"));
UserInfoEntity user = new UserInfoEntity();
user.setUsername("张三");
user.setPassword("123456");
user.setEmail("132456789@qq.com");
user.setMobilePhone("13888888888");
return user;
}
消费者:
@RequestMapping(value="getUserInfoPost",method = RequestMethod.GET)
public UserInfoEntity getUserInfoPost(){
Map<String,Object> map = new HashMap<>();
map.put("name","张三");
ResponseEntity<UserInfoEntity> resp = restTemplate.postForEntity("http://PROVIDER-USER/hello/getUserInfoPost",map,UserInfoEntity.class);
return resp.getBody();
}
postForEntity第二个参数(map)是传递的参数。
2、postForObject
封装了postForEntity 与getForObject 类似
消费者:
@RequestMapping(value="getUserInfoPost2",method = RequestMethod.GET)
public UserInfoEntity getUserInfoPost2(){
Map<String,Object> map = new HashMap<>();
map.put("name","张三");
return restTemplate.postForObject("http://PROVIDER-USER/hello/getUserInfoPost",map,UserInfoEntity.class);
}
PUT请求
PUT请求可以通过put方法调用,put方法的参数和前面介绍的postForEntity方法的参数基本一致,只是put方法没有返回值而已。
提供者:
@RequestMapping(value = "userInfoPut/{str}",method = RequestMethod.PUT)
public void userInfoPut(@RequestBody Map<String,Object> map, @PathVariable("str") String str){
log.info("姓名:"+(String)map.get("name"));
log.info(str);
/** 逻辑处理 **/
}
消费者:
@RequestMapping(value="getUserInfoPut",method = RequestMethod.GET)
public void getUserInfoPut(){
Map<String,Object> map = new HashMap<>();
map.put("name","王五");
restTemplate.put("http://PROVIDER-USER/hello/userInfoPut/{1}",map,"李四");
}