基于SSM框架实现Redis限流(ssm框架redis限流)

Spring + Spring MVC + MyBatis(abbreviated as SSM)is a widely used popular open source framework, providing rAPId application development capabilities. Due to its powerful development capabilities, the project can rapidly construct a complete framework for the system and quickly realize a non trivial engineering.

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、虚拟空间、营销软件、网站建设、德惠网站维护、网站推广。

In actual development, we often face a problem, that is, how to limit the call rate for API and service. In order to ensure adequate system performance, when the requested API and service increases sharply to a certn extent, it needs to throttle to avoid service collapse.

One common way to do throttling is to use Redis as a counter. Redis can store key-value prs, such as a token, and then store the number of API requests that the client has requested in a certn period of time. When the API request number has reached a certn threshold, the request is rejected.

In order to implement this throttling in SSM framework, we need to do the following procedures:

1. In the configuration file, add the configuration information of Redis and the limit rate of API requests.

2. Use AOP aspect programming to encapsulate the API call action as an aspect, and intercept the current API call.

3. Pass the key to Redis to obtn the current API call number from Redis, judge whether it is within the limit range, and then control the request forwarding by returning different HTTP status codes.

4. Refresh the Redis key-value pr to adjust the counter.

Here is an example of how to implement this throttling based on SSM framework:

“`java

//Configuration of Redis in the configuration file

@Bean

public JedisPool jedisPool() {

return new JedisPool(“127.0.0.1”, 6379);

}

//AOP aspect programming

@Aspect

public class ApiPointcut {

@Before(“execution(* com.example.controllers.HelloController.hello(..))”)

public void requestLimit() {

String requestIP = getIpAddr();

String key = “req_limit_” + requestIP;

//Get the current API call number from Redis

Jedis jedis = jedisPool.getResource();

Long reqCount = jedis.incrBy(key, 1L);

jedis.expire(key, 10);

jedis.close();

//Judge whether it is within the limit range and control the request forwarding by returning different HTTP status codes.

if(reqCount == 1) {

logger.info(“Sucessful access”);

} else if (reqCount > 5) {

logger.error(“Number of access exceeded the limitation”);

response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());

}

}

}


In this way, we use the Redis key-value pr to record the number of API requests in a certn period of time, and use AOP aspect programming to intercept the current API calls to perform throttling. In this way, we can effectively limit the request rate of the API and ensure the normal performance of the system.

香港服务器选创新互联,2H2G首月10元开通。
创新互联(www.cdcxhl.com)互联网服务提供商,拥有超过10年的服务器租用、服务器托管、云服务器、虚拟主机、网站系统开发经验。专业提供云主机、虚拟主机、域名注册、VPS主机、云服务器、香港云服务器、免备案服务器等。

当前文章:基于SSM框架实现Redis限流(ssm框架redis限流)
分享网址:http://www.zyruijie.cn/qtweb/news28/12078.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联