In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how Java implements the current limiter to deal with Rest interface requests". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how Java implements the current limiter to handle Rest interface requests".
Maven relies on com.google.guava guava 31.0.1-jre code
Put the code on, no nonsense.
The first is the current limiter code.
Package com.huyi.csdn.tools.rate; import com.google.common.util.concurrent.Monitor;import com.google.common.util.concurrent.RateLimiter; import java.util.concurrent.ConcurrentLinkedQueue;import java.util.function.Consumer; / * * @ Program: csdn @ ClassName: RequestRateLimiter @ Author: huyi @ Date: 2021-10-30 22:16 @ Description: * request current limiter @ Version: V1.0 * / public class RequestRateLimiter {/ / request queue private final ConcurrentLinkedQueue bucket = new ConcurrentLinkedQueue () / / queue upper limit private static final int BUCKET_CAPACITY = 100; / / flow velocity along the leaky bucket private final RateLimiter rateLimiter = RateLimiter.create (10.0D); / / request monitor private final Monitor requestMoniter = new Monitor (); / / processing monitor private final Monitor handleMoniter = new Monitor (); / * * request entity * / public static class Request {private int data; public Request (int data) {this.data = data } @ Override public String toString () {return "Request {" + "data=" + data +'}';}} public void submitRequest (int data) {this.submitRequest (new Request (data));} public void submitRequest (Request request) {/ / request monitor, create a monitoring wizard with queue data less than the upper limit if (requestMoniter.enterIf (()-> bucket.size ())
< BUCKET_CAPACITY))) { try { boolean result = bucket.offer(request); if (result) { System.out.println("成功向队列加入新的请求!" + Thread.currentThread() + " request:" + request); } else { System.out.println("加入新请求失败!"); } } finally { requestMoniter.leave(); } } else { // 队列已满 // System.out.println("请求队列已经上限,请稍后重试!"); } } // 处理请求方法 public void handleRequest(Consumer consumer) { if (handleMoniter.enterIf(handleMoniter.newGuard(() ->! bucket.isEmpty ()) {try {/ / uniform processing rateLimiter.acquire (); consumer.accept (bucket.poll ());} finally {handleMoniter.leave ();}
Code description
1. There is a task queue of 100 in length, and the monitor is added.
2. A current limiter is added, and the current limit is 10.
Verification code
Package com.huyi.csdn.tools.rate; import java.time.LocalTime;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;import java.util.stream.IntStream; / * * @ Program: csdn @ ClassName: TestRateLimiter @ Author: huyi @ Date: 2021-10-30 * 22:35 @ Description: @ Version: V1.0 * / public class TestRateLimiter {private static final AtomicInteger DATA = new AtomicInteger (0); private static final RequestRateLimiter HANDLE = new RequestRateLimiter () Public static void main (String [] args) {IntStream.range (0,10) .forEach ((x)-> new Thread (())-> {while (true) {HANDLE.submitRequest (DATA.getAndIncrement () Try {TimeUnit.MILLISECONDS.sleep (100);} catch (InterruptedException e) {e.printStackTrace () }) .start () IntStream.range (0 20) .forEach ((x)-> new Thread (() -)-> {while (true) {HANDLE.handleRequest (y->) System.out.println (LocalTime.now () + ": processing data->" + y.toString () }}) .start ();}}
Verify the execution result
These are all the contents of the article "how Java implements the current limiter to handle Rest interface requests". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.