本例描述了如下场景,限流为QPS=1
假设请求数据为 aaaaaa
如果被限流就返回 limited,aaaaaa
如果正常执行就返回 hello,aaaaaa
<int:channel id="inputActivatorChannel" /> <int:filter id="rateLimitfilterfilter" input-channel="inputActivatorChannel" output-channel="inputActivatorChannel2" ref="rateLimitFilter" discard-channel="discardChannel"/> <!-- 当限流后执行此通道 --> <int:channel id="discardChannel" /> <int:service-activator input-channel="discardChannel" ref="helloActivator" method="sayDiscard" output-channel="outputActivatorChannel" /> <!-- 未限流执行此通道 --> <int:channel id="inputActivatorChannel2" /> <int:service-activator input-channel="inputActivatorChannel2" ref="helloActivator" method="sayHello" output-channel="outputActivatorChannel" /> <bean id="helloActivator" class="org.tinygroup.integration.sample.activator.HelloActivatorImpl" /> <!-- 限流相关bean --> <bean id="rateLimitFilter" class="org.tinygroup.springintegration.ratelimit.RateLimitFilter"> <property name="rateLimiter" ref="rateLimiter"></property> </bean> <bean id="rateLimiter" class="org.tinygroup.springintegration.ratelimit.SmoothBurstyRateLimiterFactoryBean"> <property name="permitsPerSecond" value="1"></property> </bean>
HelloActivatorImpl的实现:
public class HelloActivatorImpl { public String sayHello(String name) { return "hello," + name; } public String sayDiscard(String name){ return "limited,"+name; } }