redis yaml配置
redis:
host: 1.117.89.11
port: 6378
password: Terry123456.
timeout: 60s
database: 2
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
#spring cache 配置
cache:
type: redis
redis:
# 半小时
time-to-live: 1800000 #毫秒
redisConfig配置:
package com.wlient.springboot_redis.Configuar;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
WebConfig配置文件:
package com.wlient.springboot_redis.Configuar;
import com.wlient.springboot_redis.common.Request.RequestLimitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private RequestLimitInterceptor requestLimitInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestLimitInterceptor)
//拦截所有请求路径
.addPathPatterns("/**")
//再设置 放开哪些路径
.excludePathPatterns("/static/**","/auth/login");
}
}
自定义注解RequestLimit:
package com.wlient.springboot_redis.Interface;
import java.lang.annotation.*;
@Inherited
@Documented
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLimit {
/**
* 时间内 秒为单位
*/
int second() default 10;
/**
* 允许访问次数
*/
int maxCount() default 5;
//默认效果 : 10秒内 对于使用该注解的接口,只能总请求访问数 不能大于 5次
}
过滤器拦截限流类:
RequestLimitInterceptor
package com.wlient.springboot_redis.common.Request;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wlient.springboot_redis.Interface.RequestLimit;
import com.wlient.springboot_redis.utils.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Component
public class RequestLimitInterceptor implements HandlerInterceptor {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
try {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
// 获取RequestLimit注解
RequestLimit requestLimit = handlerMethod.getMethodAnnotation(RequestLimit.class);
if (null==requestLimit) {
return true;
}
//限制的时间范围
int seconds = requestLimit.second();
//时间内的 最大次数
int maxCount = requestLimit.maxCount();
String ipAddr = IpUtil.getIpAddr(request);
// 存储key
String key = ipAddr+":"+request.getContextPath() + ":" + request.getServletPath();
// 已经访问的次数
Integer count = (Integer) redisTemplate.opsForValue().get(key);
log.info("检测到目前ip={} 对接口:{} 已经访问的次数:{}",ipAddr, request.getServletPath() , count);
if (null == count || -1 == count) {
redisTemplate.opsForValue().set(key, 1, seconds, TimeUnit.SECONDS);
return true;
}
if (count < maxCount) {
redisTemplate.opsForValue().increment(key);
return true;
}
log.info("请求过于频繁请稍后再试");
returnData(response);
return false;
}
return true;
} catch (Exception e) {
log.warn("请求过于频繁请稍后再试");
e.printStackTrace();
}
return true;
}
public void returnData(HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
ObjectMapper objectMapper = new ObjectMapper();
//这里传提示语可以改成自己项目的返回数据封装的类
response.getWriter().println(objectMapper.writeValueAsString("请求过于频繁请稍后再试"));
return;
}
}
接口测试:
@GetMapping("/test")
@RequestLimit(maxCount = 3,second = 60)
public String test() {
return "你好,如果对你有帮助,请点赞加关注。";
}
运行结果:
springboot-redis- 2021-08-17 15:25:37 [http-nio-8099-exec-1] INFO
c.w.s.c.R.RequestLimitInterceptor - 检测到目前ip=0:0:0:0:0:0:0:1 对接口:/user/test 已经访问的次数:null
springboot-redis- 2021-08-17 15:25:39 [http-nio-8099-exec-2] INFO
c.w.s.c.R.RequestLimitInterceptor - 检测到目前ip=0:0:0:0:0:0:0:1 对接口:/user/test 已经访问的次数:1
springboot-redis- 2021-08-17 15:25:40 [http-nio-8099-exec-5] INFO
c.w.s.c.R.RequestLimitInterceptor - 检测到目前ip=0:0:0:0:0:0:0:1 对接口:/user/test 已经访问的次数:2
springboot-redis- 2021-08-17 15:25:41 [http-nio-8099-exec-3] INFO
c.w.s.c.R.RequestLimitInterceptor - 检测到目前ip=0:0:0:0:0:0:0:1 对接口:/user/test 已经访问的次数:3
springboot-redis- 2021-08-17 15:25:41 [http-nio-8099-exec-3] INFO
c.w.s.c.R.RequestLimitInterceptor - 请求过于频繁请稍后再试