怎么建淘宝优惠券网站做推广,wordpress 图片压缩插件,泉州建站平台,微商城网站开发文章目录 安全性todo 数据存储限流限流的几种算法限流粒度限流的实现本地限流#xff08;单机限流#xff09;Redisson实现分布式限流(多机限流) 安全性
问题引入#xff1a;如果用户上传一个超大的文件怎么办#xff1f;比如1000G#xff1f; 预防#xff1a; 只要涉及… 文章目录 安全性todo 数据存储限流限流的几种算法限流粒度限流的实现本地限流单机限流Redisson实现分布式限流(多机限流) 安全性
问题引入如果用户上传一个超大的文件怎么办比如1000G 预防 只要涉及到用户自主上传的操作一定要校验文件图像 校验什么
文件的大小文件的后缀文件的内容成本高一点文件的合规性比如敏感内容建议用第三方审核功能todo 接入腾讯云的图片万象数据审核COS对象存储的审核功能
代码校验实现 //校验文件大小long ONE_MB 1024 * 1024l;long size multipartFile.getSize();ThrowUtils.throwIf(size ONE_MB,ErrorCode.PARAMS_ERROR,文件过大);//校验后缀名String originalFilename multipartFile.getOriginalFilename();String suffix FileUtil.getSuffix(originalFilename);ListString validSuffix Arrays.asList(png,jpg,svg,webp,jpeg);ThrowUtils.throwIf(!validSuffix.contains(suffix),ErrorCode.PARAMS_ERROR,文件后缀非法);todo 数据存储
现状我们把每个图表的原始数据全部放在了同一个数据表chart表的字段里 问题
如果用户上传的原始数据量很大图表数日益增多查询chart表就会很慢对于BI平台用户是有查看原始数据对原始数据进行简单查询的需求的现在如果把所有数据放在一个字段列中查询时只能取出这个列的所有内容
**解决方案分库分表
**把每个图表对应的原始数据单独保存为一个新的数据表而不是都存在一个字段里 优点
存储时能够分开存储互不影响也能增加安全性查询时可以使用各种sql语句灵活取出需要的字段查询性能更快
todo 实现动态sql,这里鱼皮也实现了不过没有应用只是测试等等复习下知识再说
限流
现在的问题使用系统是需要消耗成本的用户有可能疯狂刷量让你破产 解决问题
控制成本 - 限制用户调用总次数用户在短时间内疯狂使用导致服务器资源被占满其他用户无法使用-限流
思考限流阈值多大合适参考正常用户的使用比如限制单个用户在每秒只能使用一次
限流的几种算法
固定窗口限流滑动窗口限流漏桶限流领令牌桶限流
限流粒度
针对某个方法限流针对某个用户限流针对用户调用某个方法限流
限流的实现
本地限流单机限流
每个服务器单独限流一般适用于单体项目你的项目只有一个服务器 Guava RateLimiter
import com.google.common.util.concurrent.RateLimiter;public static void main(String[] args) {// 每秒限流5个请求RateLimiter limiter RateLimiter.create(5.0);while (true) {if (limiter.tryAcquire()) {// 处理请求} else {// 超过流量限制需要做何处理}}
}Redisson实现分布式限流(多机限流)
[官方项目仓库和文档]
引入依赖
dependencygroupIdorg.redisson/groupIdartifactIdredisson/artifactIdversion3.28.0/version
/dependency 创建Redisson配置类
package com.yupi.springbootinit.config;import io.lettuce.core.RedisClient;
import io.swagger.models.auth.In;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
ConfigurationProperties(spring.redis)
Data
public class RedissonConfig {private Integer database;private String host;private Integer port;// spring启动时会自动创建一个RedissonClient对象Beanpublic RedissonClient getRedissonClient() {// 1.创建配置对象Config config new Config();// 2. 添加单机Redisson配置config.useSingleServer()// 设置数据库.setDatabase(1)//设置redis的地址.setAddress(redis:// host : port);//3..创建Redisson实例RedissonClient redissonClient Redisson.create(config);return redissonClient;}}
创建通用限流管理类RedisLimiterManager
专门提供 RedisLimiter 限流基础服务manager包存放通用模版没有业务逻辑可以放在任何一个项目里
package com.yupi.springbootinit.manager;import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;import javax.annotation.Resource;Service
public class RedisLimiterManager {Resourceprivate RedissonClient redissonClient;public void doRateLimit(String key){// 创建一个名称为rateLimiter的限流器RRateLimiter rateLimiter redissonClient.getRateLimiter(key);// 限流器的统计规则(每秒2个请求;连续的请求,最多只能有1个请求被允许通过)// RateType.OVERALL表示速率限制作用于整个令牌桶,即限制所有请求的速率rateLimiter.trySetRate(RateType.OVERALL,2,1, RateIntervalUnit.SECONDS);// 每当一个操作来了后请求一个令牌boolean canop rateLimiter.tryAcquire(1);// 如果没有令牌,还想执行操作,就抛出异常if(!canop){throw new BusinessException(ErrorCode.TOO_MANY_REQUEST);}}
}
测试后整合进项目一行代码解决 //限流判断每个用户一个限流器
redisLimiterManager.doRateLimit(genChartByAi_ loginUser.getId());
文章转载自: http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.lzqdl.cn.gov.cn.lzqdl.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.wklmj.cn.gov.cn.wklmj.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.xbptx.cn.gov.cn.xbptx.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.bgkk.cn.gov.cn.bgkk.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.qsy41.cn.gov.cn.qsy41.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.nlhcb.cn.gov.cn.nlhcb.cn http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.rqgq.cn.gov.cn.rqgq.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.hmbxd.cn.gov.cn.hmbxd.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.trsdm.cn.gov.cn.trsdm.cn http://www.morning.taojava.cn.gov.cn.taojava.cn http://www.morning.wjjsg.cn.gov.cn.wjjsg.cn http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.hypng.cn.gov.cn.hypng.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.bdfph.cn.gov.cn.bdfph.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.bpmns.cn.gov.cn.bpmns.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.ljzgf.cn.gov.cn.ljzgf.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.mpszk.cn.gov.cn.mpszk.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.tnkwj.cn.gov.cn.tnkwj.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.ppzgr.cn.gov.cn.ppzgr.cn http://www.morning.iiunion.com.gov.cn.iiunion.com