spring cloud学习笔记4(请求合并处理)

本文介绍了在微服务架构中,为优化远程调用的通信消耗和连接数占用,Spring Cloud Hystrix通过HystrixCollapser实现请求合并的原理和实践。在高并发场景下,合并处理器能在短时间内将多个相同依赖服务的请求合并处理,减少通信时间和线程资源的占用。通过在eureka-consume项目中添加依赖并进行测试,验证了请求合并的成功。
摘要由CSDN通过智能技术生成

 微服务架构中的依赖通过远程调用实现,而远程调用中最常见的问题就是通信消耗与连接数占用。在高并发的情况下,因通信次数的增加,总的通信时间消耗将会变得不那么理想。同时,因为依赖服务的线程池资源有限,将出现排队等待与相应延迟的情况,为了优化这个两个问题,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,出现如下则表示成功





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值