In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "java how to achieve stand-alone interface current limit", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "java how to achieve stand-alone interface current limit" this article.
To put it simply, an API is set to accept only a fixed number of requests at a certain time. For example, the / add API receives up to 100 requests per second, and many requests are rejected directly. This problem is very common, and the scenario is easy to understand. Go to the code directly:
/ * standalone current limit * / @ Slf4jpublic class FlowLimit {/ / Interface current limit upper limit and current limit time cache private static Cache localCache = CacheBuilder.newBuilder (). MaximumSize (100) .resolreAfterWrite (1000, TimeUnit.MILLISECONDS). Build (); / / the upper limit cache of each interface private static Map maxFlowLimitMap = new ConcurrentHashMap (); private static final FlowLimit instance = new FlowLimit () / / the purpose of this block is to initialize the upper limit of each interface. The following variables: apiFlowLimitConfigure / / when actually used, the upper limit value of each interface should be obtained from db or other places. / / this can dynamically adjust the upper limit of the interface, such as directly modifying db without publishing. You can adjust the current limit value of the interface static {new ScheduledThreadPoolExecutor (1, runnable-> {Thread thread = new Thread (runnable, "api-flowLimit-configure") / / thread.setDaemon (true); return thread;}) .scheduleAtFixedRate (()-> {try {String apiFlowLimitConfigure = "{\" doAdd\ ": 100}"; / / indicates that the / doAdd interface accepts 100 requests per second Map mapObj = JSONObject.parseObject (apiFlowLimitConfigure, Map.class) If (mapObj! = null) {mapObj.forEach ((key, value)-> {if (value! = null) {instance.setMaxFlowLimit (key.toString (), new Long (value.toString () } else {log.warn (key + "- set interface current limit discovery value is empty, set default value"); instance.setMaxFlowLimit (key.toString (), 100L);}}) }} catch (Exception e) {log.error ("exception {}", e);}}, 0,3, TimeUnit.SECONDS);} public static FlowLimit getInstance () {return instance;} private FlowLimit setMaxFlowLimit (String key, Long maxFlowLimit) {maxFlowLimitMap.put (key, maxFlowLimit); return this } public Boolean isAvailable (String key) {return checkAvailable (key, 1L);} public Boolean isAvailable (String key, Long incrNum) {return checkAvailable (key, incrNum);} private Boolean checkAvailable (String key, Long incrNum) {Long maxFlowLimit = maxFlowLimitMap.get (key); if (null = = maxFlowLimit | | maxFlowLimit = = 0) {return true;} if (incrAndGet (key, incrNum)
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.