当前位置:网站首页>Redis+AOP怎么自定义注解实现限流
Redis+AOP怎么自定义注解实现限流
2022-06-30 02:38:00 【亿速云】
Redis+AOP怎么自定义注解实现限流
今天小编给大家分享一下Redis+AOP怎么自定义注解实现限流的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
下载
1,下载页面
2,下载
解压
tar -xzvf redis-5.0.7.tar.gz
准备编译
1, 请在操作前确认gcc是否已安装,gcc -v
如未安装,可以执行这个命令安装:yum install gcc
2,请在操作前确认tcl是否已安装如未安装,可以执行这个命令安装:yum install tcl
编译
[[email protected] source]# cd redis-5.0.7/[[email protected] redis-5.0.7]# make MALLOC=libc
make 后加 MALLOC的参数的原因:
避免提示找不到 jemalloc/jemalloc.h
测试编译
[[email protected] redis-5.0.7]# make test
如果看到以下字样:表示无错误:\o/ All tests passed without errors!
安装
[[email protected] redis-5.0.7]# mkdir /usr/local/soft/redis5 可分步创建[[email protected] redis-5.0.7]# cd /usr/local/soft/redis5/[[email protected] redis5]# mkdir bin[[email protected] redis5]# mkdir conf[[email protected] redis5]# cd bin/
find / -name redis-cli 查找文件位置
[[email protected] bin]# cp /root/redis-5.0.7/src/redis-cli ./[[email protected]localhost bin]# cp /root/redis-5.0.7/src/redis-server ./[[email protected] bin]# cd …/conf/[[email protected] conf]# cp /root/redis-5.0.7/redis.conf ./
配置
[[email protected] conf]# vi redis.conf
设置以下两个地方:
# daemonize no daemonize yes # maxmemory <bytes>maxmemory 128MB
说明:分别是以daemon方式独立运行 / 内存的最大使用限制
运行
[[email protected] conf]# /usr/local/soft/redis5/bin/redis-server /usr/local/soft/redis5/conf/redis.conf
检查端口是否在使用中
[[email protected] conf]# netstat -anp | grep 6379tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 16073/redis-server
查看redis的当前版本:
[[email protected] conf]# /usr/local/soft/redis5/bin/redis-server -vRedis server v=5.0.7 sha=00000000:0 malloc=libc bits=64 build=8e31d2ed9a4c9593
使redis可以用systemd方式启动和管理
1,编辑service文件
[[email protected] liuhongdi]# vim /lib/systemd/system/redis.service
2,service文件内容:
[Unit]Description=RedisAfter=network.target[Service]Type=forkingPIDFile=/var/run/redis_6379.pidExecStart=/usr/local/soft/redis5/bin/redis-server /usr/local/soft/redis5/conf/redis.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target
3.重载系统服务
[[email protected] liuhongdi]# systemctl daemon-reload
4,用来管理redis
启动
systemctl start redis
查看状态
systemctl status redis
使开机启动
systemctl enable redis
查看本地centos的版本:
[[email protected] lib]# cat /etc/redhat-releaseCentOS Linux release 8.1.1911 (Core)
客户端连接redis
1、阿里云得设置redis.conf中的bind 后跟着的127.0.0.1修改为0.0.0.0,重启redis
2、开放端口:开放服务器的端口号,步骤如下:
打开实例列表,点击“ 更多”按钮,选择“ 网络和安全组 ”中的“安全组配置”,选择 “安全组列表”tab页面,点击 “配置规则”按钮,点击 “快速添加”按钮,勾选“Redis(6379)”,点击 “确定”之后就可以正常连接了。
3、给redis设置连接密码:
查找到# requirepass foobared 注释去掉并写入要设置的密码,例如:requirepass 123456
redis启动之后测试是否可以连接命令
./redis-cli -h 127.0.0.1 -p 6379127.0.0.1:6379> auth 123456//此处是你的密码
注意: 如果是阿里云的话一定要设置密码,否则很可能被矿机程序注入定时任务,用你的服务器挖矿,阿里云一直会有信息提示你。
Redis限流
服务器上的Redis已经安装完成了(安装步骤见上文),今天就让我们使用Redis来做个小功能:自定义拦截器限制访问次数,也就是限流。
首先我们要在项目中引入Redis
1、引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- redis依赖commons-pool 这个依赖一定要添加 --><dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId></dependency>
2、application.yml配置
server:port: 8181spring:redis: host: 127.0.0.1 port: 6379 timeout: 10s lettuce: pool: # 连接池中的最小空闲连接 默认0 min-idle: 0 # 连接池中的最大空闲连接 默认8 max-idle: 8 # 连接池最大连接数 默认8 ,负数表示没有限制 max-active: 8 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认-1 max-wait: -1ms #选择哪个库存储,默认是0 database: 0 password: 123456
3、创建redisConfig,引入redisTemplate
@Configurationpublic class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; }}自定义注解和拦截器
1、自定义注解
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@Documentedpublic @interface AccessLimit { int seconds(); //秒数 int maxCount(); //最大访问次数 boolean needLogin()default true;//是否需要登录}2、创建拦截器
@Componentpublic class FangshuaInterceptor extends HandlerInterceptorAdapter { @Autowired private RedisTemplate redisTemplate; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //判断请求是否属于方法的请求 if(handler instanceof HandlerMethod){ HandlerMethod hm = (HandlerMethod) handler; //获取方法中的注解,看是否有该注解 AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); if(accessLimit == null){ return true; } int seconds = accessLimit.seconds(); int maxCount = accessLimit.maxCount(); boolean login = accessLimit.needLogin(); String key = request.getRequestURI(); //如果需要登录 if(login){ //获取登录的session进行判断,此处只是例子,不写具体的业务 //..... key+=""+"1"; //这里假设用户是1,项目中是动态获取的userId } //从redis中获取用户访问的次数 Integer count; if(Objects.isNull(redisTemplate.opsForValue().get(key))){ count = 0; }else{ count = (Integer) redisTemplate.opsForValue().get(key); } if(count == 0){ redisTemplate.opsForValue().set(key,1,seconds, TimeUnit.SECONDS); }else if(count<maxCount){ //key的值加1 redisTemplate.opsForValue().increment(key); }else{ //超出访问次数 Map<String,Object> errMap=new HashMap<>(); errMap.put("code",400); errMap.put("msg","请求超时,请稍后再试"); render(response,errMap); //这里的CodeMsg是一个返回参数 return false; } } return true; } private void render(HttpServletResponse response, Map<String,Object> errMap) throws Exception { response.setContentType("application/json;charset=UTF-8"); OutputStream out = response.getOutputStream(); String str = JSON.toJSONString(errMap); out.write(str.getBytes("UTF-8")); out.flush(); out.close(); }}3、将自定义拦截器加入到拦截器列表中
@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter { @Autowired private FangshuaInterceptor interceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(interceptor); }}最后做一下简单的测试
@[email protected]("test")public class TestController { //每三十秒最多可以请求三次,不需要登录 @AccessLimit(seconds=30, maxCount=3, needLogin=false) @PostMapping("/fangshua") public String fangshua(){ return "成功"; }}
以上就是“Redis+AOP怎么自定义注解实现限流”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
边栏推荐
- 2022 the action of protecting the net is imminent. Things about protecting the net
- Global and Chinese markets for light cargo conveyors 2022-2028: Research Report on technology, participants, trends, market size and share
- Jupyter notebook显示k线图集合
- 迅為恩智浦iTOP-IMX6開發平臺
- Global and Chinese market of mobile commerce solutions 2022-2028: Research Report on technology, participants, trends, market size and share
- CA数字证书包含哪些文件?如何查看SSL证书信息?
- Cmake tutorial series-03-dependency management
- Recursion frog jumping steps problem
- [Postgres] Postgres database migration
- How to display all keys through redis cli- How to show ALL keys through redis-cli?
猜你喜欢

matlab代码运行教程(如何运行下载的代码)

Matlab code running tutorial (how to run the downloaded code)

IBM WebSphere channel connectivity setup and testing

Seven common errors of SSL certificate and their solutions

Quick sort

有流量,但没有销售?增加网站销量的 6 个步骤

PR second training notes

走进江苏作家诗人胭脂茉莉|世界读书日

Cmake tutorial series -02- generating binaries using cmake code

如何预防钓鱼邮件?S/MIME邮件证书来支招
随机推荐
What is a dangling pointer- What is a dangling pointer?
Seven common errors of SSL certificate and their solutions
Global and Chinese markets of liquid optical waveguides 2022-2028: Research Report on technology, participants, trends, market size and share
2.8 【 weight of complete binary tree 】
Precautions for purchasing wildcard SSL certificate
并发请求下如何防重复提交
Matlab code running tutorial (how to run the downloaded code)
桶排序
Heap sort
速看 2021-2022年23项重大网络犯罪统计数据
Time complexity analysis
选择排序
Network neuroscience -- a review of network Neuroscience
Raii memory management
Merge sort
三层交换机和二层交换机区别是什么
Global and Chinese markets for light cargo conveyors 2022-2028: Research Report on technology, participants, trends, market size and share
PMP考生如何应对新考纲?看过来!
【postgres】postgres 数据库迁移
What should academic presentation /ppt do?