Introduction: When the server and client interfaces are invoked, we often encounter that the client frequently invokes the same interface of the server, and each invocation takes only a short time and the content is rarely transmitted. For example, in a unit of time (seconds), the client sends 1000 requests to the server based on the ID query. To complete this function, it may need to generate 1000 calls. In this case, the batch interface is a good solution. But how do you merge those 1000 requests and call them in batches, and then return them to each request correctly?

Above as the original model, here give a optimization idea, first ask the server to provide a batch interface, the client receives the request of the encapsulated into an event to join a blocking queue, every 100 ms, a element with the queue, to carry on the batch request to the server, and then according to the different thread request will return to distribute to the corresponding interface thread; Again, this is just an idea, not deeply consider such as queue congestion, memory, etc., the security part is expanded and designed according to the actual situation

Theoretically, for the above QPS, throughput increases by about 10 times. If you already know how to do this, you don’t need to read the following article. If you don’t know how to do this, you might as well spend a few more minutes reading;

Here’s a demo I wrote myself, using CompletableFuture as an asynchronous interface to return and BlockingDeque as a queue to store requests.

import lombok.SneakyThrows; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.*; import java.util.concurrent.*; public class Batch { static class Resp { final private String id; final private String queryId; CompletableFuture<String> future; public Resp(String id, String queryId, CompletableFuture<String> future) { this.id = id; this.queryId = queryId; this.future = future; } } public String doService(String queryId) throws ExecutionException, InterruptedException { CompletableFuture<String> future = new CompletableFuture<>(); deque.add(new Resp(UUID.randomUUID().toString(), queryId, future)); return future.get(); } BlockingDeque<Resp> deque = new LinkedBlockingDeque<>(); @PostConstruct void init() { ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1); scheduledExecutorService.scheduleAtFixedRate(() ->{ int size = deque.size(); if (size <= 0) { return; } List<Resp> list = new ArrayList<>(); List<Map> mapList = new ArrayList<>(); for (int i = 0; i < size; i++) { Resp resp = deque.poll(); Map<String, String> map = new HashMap<>(); map.put("id", resp.id); map.put("queryId", resp.queryId); mapList.add(map); //map.put("service", resp.service); list.add(resp); } List<Map<String, String>> s = doBatch(mapList); for (Resp resp : list) { for (Map<String, String> m : s) { if (resp.id.equals(m.get("id"))) { resp.future.complete(m.get("result")); } } } },0, 100, TimeUnit.MILLISECONDS); } public List<Map<String, String>> doBatch(List<Map> mapList) {system.out.println (" execute once "+ maplist.size ()); List<Map<String, String>> list = new ArrayList<>(); ForEach (x -> list.add(new HashMap<String, String>(){{put("id",(String) x.et ("id")); put("result", x.get("queryId")+ "__"+ "resp"); }})); return list; } public void test() { for (int i = 0; i < 1000; i++) { int kj = i; new Thread(new Runnable() { @SneakyThrows @Override public void run() { String s = doService(kj + ""); Map<String, String> map = new HashMap<>(); map.put(kj + "", s); map.forEach((k, v) -> System.out.println(k + " ==== " + v)); } }).start(); }}}Copy the code

Last but not least: this idea is learned from Tencent open class, and simply shared here. This is also the first time to write some articles, before eating too much copy and paste and so on misleading article loss, worried about their level limit will also mislead others, so simply do not write nor published. Recently, I have received encouragement from my friends and changed my mind, so I will write it as study notes. If someone has gained something, it is the best. I am very sorry if it misled you, and I hope you can reply. Finally, thank you, everyone who has read this far, come on.

bye~