新聞中心
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.

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:空間域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、德惠網(wǎng)站維護(hù)、網(wǎng)站推廣。
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.
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
當(dāng)前題目:基于SSM框架實現(xiàn)Redis限流(ssm框架redis限流)
網(wǎng)站URL:http://fisionsoft.com.cn/article/cdgjdog.html


咨詢
建站咨詢
