spring cloud learning note 4 (request merge processing)

Posted by wescrock on Tue, 05 May 2020 11:13:49 +0200

The dependency in microservice architecture is realized by remote call, and the most common problem in remote call is communication consumption and connection number occupation. In the case of high concurrency, due to the increase of communication times, the total communication time consumption will become less ideal. At the same time, due to the limited resource of thread pool relying on services, there will be queuing and corresponding delay. In order to optimize these two problems, Hystrix provides Hystrix collapser to implement the request merging, so as to reduce the communication consumption and the occupation of thread number.

The HystrixCollapser implements the placement of a merge processor before the HystrixCommand, which will be in a short time window (default 10 ms) for the same dependent service

Multiple requests are merged.

Modify pom file in Eureka consumer to add dependency

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.29</version>
        </dependency>
Create user class

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;
    }
}
Create 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);
    }
}
Add 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);
    }
}
Add user in HI-SERVICE

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;
    }
}

Modify 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;
    }

}

Restart

Call http://localhost:9002/user?id=1, and the following will indicate success





Topics: Java less