如何实现分布式限速器? [英] How to implement distributed rate limiter?

查看:60
本文介绍了如何实现分布式限速器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有 P 个进程在 N 台物理机上运行一些业务逻辑.例如,这些进程调用一些 Web 服务 S.我想确保所有 P 进程组合起来,每秒对服务 S 的调用不超过 X 次.

Let's say, I have P processes running some business logic on N physical machines. These processes call some web service S, say. I want to ensure that not more than X calls are made to the service S per second by all the P processes combined.

如何实施这样的解决方案?

How can such a solution be implemented?

Google Guava 的 Rate Limiter 适用于在单个机器上运行的进程,但不适用于分布式设置.

Google Guava's Rate Limiter works well for processes running on single box, but not in distributed setup.

是否有任何标准的、随时可用的 JAVA 解决方案?[可能基于zookeeper]

Are there any standard, ready to use, solutions available for JAVA? [may be based on zookeeper]

谢谢!

推荐答案

Bucket4j 是 java 实现令牌桶"限速算法.它可以在本地和分布式(在 JCache 之上)工作.对于分布式用例,您可以自由选择任何 JCache 实现,例如 Hazelcast 或 Apache Ignite.见 这个例子在集群中使用 Bucket4j.

Bucket4j is java implementation of "token-bucket" rate limiting algorithm. It works both locally and distributed(on top of JCache). For distributed use case you are free to choose any JCache implementation like Hazelcast or Apache Ignite. See this example of using Bucket4j in cluster.

这篇关于如何实现分布式限速器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆