单机限流
限流过滤器
package com.doudou.filter;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @title CustomLimitFilter
* @description 自定义限流过滤器
* author zzw
* version 1.0.0
* create 2025/5/6 22:29
**/
@Activate(group = CommonConstants.PROVIDER)
public class CustomLimitFilter implements Filter {
/**
* 存储计数资源的Map数据结构,预分配容量64,避免无谓的扩容消耗
*/
private static final ConcurrentMap<String, AtomicInteger> COUNT_MAP = new ConcurrentHashMap<>(64);
/**
* QOS流量启动是否开启。 {@code true}:标识开启流量监测;@{@code 其它值}:标识不开启流量监测
*/
public static final String KEY_QPS_ENABLE = "qps.enable";
/**
* 每个方法开启的限流检测值
*/
public static final String KEY_QPS_VALUE = "qps.value";
/**
* 默认的限流检测值,默认为 30
*/
public static final long DEFAULT_QPS_VALUE = 30L;
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// 获取限流资源的结果
// true:获取到计数资源
// false:计数已满,无法获取计数资源
// null:不需要限流
Boolean acquired = null;
try {
// 获取限流技术资源
acquired = tryAcquire(invoker.getUrl(), invocation);
if (null != acquired && !acquired) {
throw new RuntimeException("Failed to acquire service "
+ String.join(".", invoker.getInterface().getName(), invocation.getMethodName())
+ " because of overload.");
}
// 不限流或或者获取到限流资源,执行下一步调用
return invoker.invoke(invocation);
} finally {
// 释放获取到的计数资源
release(acquired, invoker.getUrl(), invocation);
}
}
private void release(Boolean acquired, URL url, Invocation invocation) {
// 未限流或者获取计数资源失败时,无需释放资源
if (null == acquired || !acquired) {
return;
}
String serviceKey = String.join("_", url.getServiceKey(), invocation.getMethodName());
COUNT_MAP.get(serviceKey).decrementAndGet();
}
private Boolean tryAcquire(URL url, Invocation invocation) {
// 判断是否开启限流
// 获取全局配置 结果是8
// url.getParameter(KEY_QPS_ENABLE);
// 获取方法级别的配置 结果是5
String qpsEnableFlag = url.getMethodParameter(invocation.getMethodName(), KEY_QPS_ENABLE);
if (!Boolean.TRUE.toString().equals(qpsEnableFlag)) {
// 限流开关的值不是true时,不开启限流
return null;
}
// 获取限流监测值,未配置时默认30
long qpsValue = url.getMethodParameter(invocation.getMethodName(), KEY_QPS_VALUE, DEFAULT_QPS_VALUE);
// 构建限流的key
String serviceKey = String.join("_", url.getServiceKey(), invocation.getMethodName());
// 获取对应的计数器
AtomicInteger currentCount = COUNT_MAP.get(serviceKey);
if (null == currentCount) {
// 第一次访问时没有计数对象值,进行计数容器的初始化。
currentCount = COUNT_MAP.putIfAbsent(serviceKey, new AtomicInteger(0));
}
// 如果当前的计数器值大于等于配置的限流值时,返回false,表示未获取到计数资源
if (currentCount.get() >= qpsValue) {
return Boolean.FALSE;
}
currentCount.incrementAndGet();
return Boolean.TRUE;
}
}
META-INF/dubbo/org.apache.dubbo.rpc.Filter
consumerLimit=com.doudou.filter.CustomLimitFilter
限流服务配置
import com.doudou.demo.api.RoleQueryFacade;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import java.util.concurrent.TimeUnit;
/**
* @title RoleQueryFacadeImpl
* @description <TODO description class purpose>
* author zzw
* version 1.0.0
* create 2025/5/6 23:27
**/
@DubboService(methods = {@Method(name = "queryRoleList", parameters = {"qps.enable", "true", "qps.value", "5"})},
parameters = {"qps2.enable", "true", "qps2.value", "8"})
public class RoleQueryFacadeImpl implements RoleQueryFacade {
@Override
public String queryRoleList(String userId) {
try {
// 睡眠 1 秒,模拟一下查询数据库需要耗费时间
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
String result = String.format(System.currentTimeMillis() + ": Hello %s, 已查询该用户【角色列表信息】", userId);
System.out.println(result);
return result;
}
}
限流现象
java.lang.RuntimeException: Failed to acquire service com.doudou.demo.api.RoleQueryFacade.queryRoleList because of overload.
at com.doudou.filter.CustomLimitFilter.invoke(CustomLimitFilter.java:50)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.rpc.filter.AccessLogFilter.invoke(AccessLogFilter.java:120)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:222)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.rpc.protocol.tri.h12.HttpContextFilter.invoke(HttpContextFilter.java:38)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:54)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:41)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.metrics.filter.MetricsFilter.invoke(MetricsFilter.java:86)
at org.apache.dubbo.metrics.filter.MetricsProviderFilter.invoke(MetricsProviderFilter.java:37)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.rpc.filter.ProfilerServerFilter.invoke(ProfilerServerFilter.java:66)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:191)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CopyOfFilterChainNode.invoke(FilterChainBuilder.java:349)
at org.apache.dubbo.rpc.cluster.filter.FilterChainBuilder$CallbackRegistrationInvoker.invoke(FilterChainBuilder.java:197)
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:167)
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:110)
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:205)
at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:52)
at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.dubbo.common.threadlocal.InternalRunnable.run(InternalRunnable.java:39)
at java.lang.Thread.run(Thread.java:748)
, dubbo version: 3.3.0, current host: 169.254.80.162, error code: 2-16. This may be caused by failed to retry do invoke, go to https://dubbo.apache.org/faq/2/16 to find instructions.
分布式限流
限流过滤器
import java.util.Objects;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
/**
* @title CustomLimitFilter
* @description 自定义限流过滤器 jvm + redis 支持 author zzw version 1.0.0 create 2025/5/6 22:29
**/
@Activate(group = CommonConstants.PROVIDER)
public class CustomLimitFilter implements Filter {
/**
* 存储计数资源的Map数据结构,预分配容量64,避免无谓的扩容消耗
*/
private static final ConcurrentMap<String, AtomicInteger> COUNT_MAP = new ConcurrentHashMap<>(
64);
/**
* QOS流量启动是否开启。 {@code true}:标识开启流量监测;@{@code 其它值}:标识不开启流量监测
*/
public static final String KEY_QPS_ENABLE = "qps.enable";
/**
* 处理限流的工具,枚举值:jLimit - JVM限流;rLimit - Redis限流
*/
public static final String KEY_QPS_TYPE = "qps.type";
/**
* 处理限流的工具 JVM限流key
*/
public static final String VALUE_QPS_TYPE_OF_JVM_LIMIT = "jLimit";
/**
* 处理限流的工具 Redis限流key
*/
public static final String VALUE_QPS_TYPE_OF_REDIS_LIMIT = "rLimit";
/**
* 每个方法开启的限流检测值
*/
public static final String KEY_QPS_VALUE = "qps.value";
/**
* 默认的限流检测值,默认为 30
*/
public static final long DEFAULT_QPS_VALUE = 30L;
/**
* 策略分发,通过不同的 qps.type 值来选择不同的限流工具进行获取计数资源处理
*/
private static final Map<String, BiFunction<URL, Invocation, Boolean>> QPS_TYPE_ACQUIRE_MAP = new ConcurrentHashMap<>(
8);
private static final Map<String, BiConsumer<URL, Invocation>> QPS_TYPE_RELEASE_MAP = new ConcurrentHashMap<>(
8);
// dubbo服务中的filter服务,变量有dubbo进行复制,无法通过@Autowire或@Resource进行复制
private RedisTemplate<String, Integer> redisTemplate;
// 对于需要赋值的,需要通过set方法,同时在外界构建一个实例,当参数放到到dubbo配置中
/*
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(RedisSerializer.string());
template.setValueSerializer(RedisSerializer.json());
return template;
}
*/
public void setRedisTemplate(RedisTemplate<String, Integer> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 初始化策略Map
*/
public CustomLimitFilter() {
init();
}
private void init() {
QPS_TYPE_ACQUIRE_MAP.put(VALUE_QPS_TYPE_OF_JVM_LIMIT, (this::tryAcquireOfJvmLimit));
QPS_TYPE_ACQUIRE_MAP.put(VALUE_QPS_TYPE_OF_REDIS_LIMIT, (this::tryAcquireOfRedisLimit));
QPS_TYPE_RELEASE_MAP.put(VALUE_QPS_TYPE_OF_JVM_LIMIT, (this::releaseOfJvmLimit));
QPS_TYPE_RELEASE_MAP.put(VALUE_QPS_TYPE_OF_REDIS_LIMIT, (this::releaseOfRedisLimit));
}
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// 获取限流资源的结果
// true:获取到计数资源
// false:计数已满,无法获取计数资源
// null:不需要限流
Boolean acquired = null;
try {
// 获取限流计数资源
acquired = tryAcquire(invoker.getUrl(), invocation);
if (null != acquired && !acquired) {
throw new RuntimeException("Failed to acquire service " + String.join(".",
invoker.getInterface().getName(), invocation.getMethodName())
+ " because of overload.");
}
// 不限流或或者获取到限流资源,执行下一步调用
return invoker.invoke(invocation);
} finally {
// 释放获取到的计数资源
release(acquired, invoker.getUrl(), invocation);
}
}
private Boolean tryAcquire(URL url, Invocation invocation) {
// 判断是否开启限流
// 获取全局配置
// url.getParameter(KEY_QPS_ENABLE);
// 获取方法级别的配置
String qpsEnableFlag = url.getMethodParameter(invocation.getMethodName(), KEY_QPS_ENABLE);
if (!Boolean.TRUE.toString().equals(qpsEnableFlag)) {
// 限流开关的值不是true时,不开启限流
return null;
}
// 获取 qps.type 参数值,默认采用 JVM 内存来处理限流,若设置的类型从 Map 中找不到则当作不需要限流处理
String qpsTypeValue = url.getMethodParameter(invocation.getMethodName(), KEY_QPS_TYPE,
VALUE_QPS_TYPE_OF_JVM_LIMIT);
BiFunction<URL, Invocation, Boolean> func = QPS_TYPE_ACQUIRE_MAP.get(qpsTypeValue);
if (null == func) {
return null;
}
// 根据配置的限流类型进行策略分发,按照不同的工具进行限流处理
return func.apply(url, invocation);
}
private void release(Boolean acquired, URL url, Invocation invocation) {
// 未限流或者获取计数资源失败时,无需释放资源
if (null == acquired || !acquired) {
return;
}
// 获取qps.type参数值,默认使用 JVM 内存来处理限流,若设置的类型从Map中找不到,则当做不限流处理
String qpsTypeValue = url.getMethodParameter(invocation.getMethodName(), KEY_QPS_TYPE,
VALUE_QPS_TYPE_OF_JVM_LIMIT);
BiFunction<URL, Invocation, Boolean> func = QPS_TYPE_ACQUIRE_MAP.get(qpsTypeValue);
if (null == func) {
return;
}
func.apply(url, invocation);
}
/**
* 使用 JVM 内存 进行资源加锁限流
*/
private Boolean tryAcquireOfJvmLimit(URL url, Invocation invocation) {
// 获取限流阈值
long qpsValue = url.getMethodParameter(invocation.getMethodName(), KEY_QPS_VALUE,
DEFAULT_QPS_VALUE);
// 构建map的key值
String serviceKey = String.join("_", url.getServiceKey(), invocation.getMethodName());
// 获取对应的限流计数器对象
AtomicInteger currentCount = COUNT_MAP.get(serviceKey);
if (null == currentCount) {
// 第一次进来时,没有计数器对象,进行初始话
COUNT_MAP.putIfAbsent(serviceKey, new AtomicInteger(0));
currentCount = COUNT_MAP.get(serviceKey);
}
// 如果当前的计数值大于等于配置的限流值时,则返回false,表示无法获取技术资源
if (currentCount.get() >= qpsValue) {
return Boolean.FALSE;
}
// 可以获取到资源,资源使用个数+1
currentCount.incrementAndGet();
return Boolean.TRUE;
}
/**
* JVM 内存资源释放
*/
private void releaseOfJvmLimit(URL url, Invocation invocation) {
String serviceKey = String.join("_", url.getServiceKey(), invocation.getMethodName());
COUNT_MAP.get(serviceKey).decrementAndGet();
}
/**
* 使用 Redis 进行资源加锁 限流
*/
private Boolean tryAcquireOfRedisLimit(URL url, Invocation invocation) {
// 获取限流阈值
long qpsValue = url.getMethodParameter(invocation.getMethodName(), KEY_QPS_VALUE,
DEFAULT_QPS_VALUE);
// 构建map的key值
String serviceKey = String.join("_", url.getServiceKey(), invocation.getMethodName());
// 获取当前key已使用的资源计数
Integer currentCount = redisTemplate.opsForValue().get(serviceKey);
if (Objects.isNull(currentCount)) {
currentCount = 1;
}
if (currentCount >= qpsValue) {
return Boolean.FALSE;
}
// 当前还有资源可以使用,资源数使用+1
redisTemplate.opsForValue().increment(serviceKey);
return Boolean.TRUE;
}
/**
* redis 限流器解锁
*/
private void releaseOfRedisLimit(URL url, Invocation invocation) {
// 构建map的key值
String serviceKey = String.join("_", url.getServiceKey(), invocation.getMethodName());
redisTemplate.opsForValue().decrement(serviceKey);
}
}
服务提供者限流配置
import com.doudou.demo.api.HelloService;
import java.util.concurrent.TimeUnit;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author admin
*/
@DubboService(methods = {
@Method(name = "hello", parameters = {"qps.enable", "true", "qps.value", "3", "qps.type",
"jLimit"}),
@Method(name = "hello2", parameters = {"qps.enable", "true", "qps.value", "3", "qps.type",
"rLimit"})})
public class HelloServiceImpl implements HelloService {
private Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);
@Override
public String hello(String name) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
logger.info("hello ----------------------------------->>> {}", name);
return "hello " + name;
}
@Override
public String hello2(String name) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
logger.info("hello2 {}", name);
return "hello2 " + name;
}
}