微服务架构中的依赖通过远程调用实现,而远程调用中最常见的问题就是通信消耗与连接数占用。在高并发的情况下,因通信次数的增加,总的通信时间消耗将会变得不那么理想。同时,因为依赖服务的线程池资源有限,将出现排队等待与相应延迟的情况,为了优化这个两个问题,Hystrix提供啦HystrixCollapser来实现请求的合并,以减少通信消耗和线程数的占用。
HystrixCollapser实现了在HystrixCommand之前放置一个合并处理器,将处于一个很短的时间窗(默认10毫秒)内对同一依赖服务
多个请求合并处理。
修改eureka-consume中pom文件,添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
创建user类
package com.study.cloud.consumer.services;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private long id;
private String name;
private String address;
public User(){
}
public User(String name, String address,long id) {
this.name = name;
this.id = id;
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
创建userservice
package com.study.cloud.consumer.controllers;
import com.study.cloud.consumer.services.User;
import com.study.cloud.consumer.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public User finduser(@RequestParam(name = "id") Long id){
System.out.println("id:"+id);
return userService.find(id);
}
}
增加usercontroller
package com.study.cloud.consumer.controllers;
import com.study.cloud.consumer.services.User;
import com.study.cloud.consumer.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public User finduser(@RequestParam(name = "id") Long id){
System.out.println("id:"+id);
return userService.find(id);
}
}
在HI-SERVICE增加user
package com.study.cloud.client.controllers;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private long id;
private String name;
private String address;
public User(){
}
public User(String name, String address,long id) {
this.name = name;
this.id = id;
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
修改controller
package com.study.cloud.client.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@RestController
public class HelloController {
private List<User> users = new ArrayList<>();
{
users.add(new User("caohui1", "caohui1", 1L));
users.add(new User("caohui2", "caohui2", 2L));
users.add(new User("caohui3", "caohui3", 3L));
users.add(new User("caohui4", "caohui4", 4L));
users.add(new User("caohui5", "caohui5", 5L));
users.add(new User("caohui6", "caohui6", 6L));
}
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHi(@RequestParam(value = "name") String name) {
String host = client.getLocalServiceInstance().getHost();
String serviceId = client.getLocalServiceInstance().getServiceId();
int port = client.getLocalServiceInstance().getPort();
return serviceId + ",host:" + host + ",port:" + port + " say hello " + name;
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public List<User> users(@RequestParam(value = "ids") String ids) {
System.out.println("ids:" + ids);
List<User> collect = users.parallelStream().filter((item) -> ids.contains(String.valueOf(item.getId()))).collect(Collectors.toList());
System.out.println("collect:" + collect);
return collect;
}
}
重新启动
调用 http://localhost:9002/user?id=1,出现如下则表示成功