IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    java 频次控制

    hpkaiq发表于 2023-04-18 14:21:15
    love 0

    访问某接口拉取数据,接口需要频次控制,经调研,com.google.common.util.concurrent.RateLimiter可轻易实现。

    1. Maven

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>31.1-jre</version>
            </dependency>
    
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.8.4</version>
            </dependency>
    

    2. 测试代码

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    import cn.hutool.http.HttpRequest;
    import com.google.common.util.concurrent.RateLimiter;
    
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;
    
    public class RateLimiterTest {
        private final RateLimiter rateLimiter = RateLimiter.create(20,1000, TimeUnit.MILLISECONDS);
    
        private RateLimiterTest() {
        }
    
        private static final class InstanceHolder {
            static final RateLimiterTest instance = new RateLimiterTest();
        }
    
        public static RateLimiterTest getInstance() {
            return InstanceHolder.instance;
        }
    
        public String runHttpGet(String url, Map<String, List<String>> header) {
            rateLimiter.acquire(1);
            return HttpRequest
                    .get(url)
                    .header(header)
                    .execute()
                    .body();
        }
    }
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    
    import java.util.*;
    import java.util.concurrent.*;
    
    public class TestRate {
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            String url = "https://xxxxx";
            Map<String, List<String>> header = new HashMap<>();
            header.put("Content-type", Collections.singletonList("application/json"));
            header.put("Access-Token", Collections.singletonList("xxxxx"));
    
            List<Callable<String>> tasks = new ArrayList<>();
    
            for (int i = 0; i < 60; i++) {
    
                Callable<String> callable = () -> {
                    RateLimiterTest instance = RateLimiterTest.getInstance();
                    return instance.runHttpGet(url, header);
                };
    
                tasks.add(callable);
    
            }
    
            ExecutorService exec = Executors.newFixedThreadPool(60);
            List<Future<String>> futures = exec.invokeAll(tasks);
            for (Future<String> future : futures) {
                String s = future.get();
                System.out.println(s);
            }
            exec.shutdown();
    
        }
    }
    


沪ICP备19023445号-2号
友情链接