当前位置: 首页 > news >正文 网站建设招标方案模板360安全网址导航 news 2025/10/24 21:11:19 网站建设招标方案模板,360安全网址导航,注册消防工程师,植物园门户网站建设方案在单体应用的时代#xff0c;Session 会话直接保存在服务器中#xff0c;实现非常简单#xff0c;但是随着微服务的流行#xff0c;现代应用架构基本都是分布式架构#xff0c;请求随机的分配到后端的多个应用中#xff0c;此时session就需要共享#xff0c;而存储在red… 在单体应用的时代Session 会话直接保存在服务器中实现非常简单但是随着微服务的流行现代应用架构基本都是分布式架构请求随机的分配到后端的多个应用中此时session就需要共享而存储在redis中就是一个最常用的方案之一。 springboot和spring data为集成redis提供了非常便捷的集成方法可以轻松实现本文一步一步介绍springboot如何集成redis如何进行配置。 关注我及时获取更多技术干货 一、引入依赖 如果你的应用是springboot应用并且使用了spring data那么集成redis是十分简单的事情应该redis已经提供了相关的依赖而开发者只需要进行简单的配置即可。 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId /dependencydependencygroupIdorg.springframework.session/groupIdartifactIdspring-session-data-redis/artifactId /dependency二、详细配置 #session超时时间设置为1000秒 server.session.timeout1000 #设置session的存储方式none为存在本地内存中redis为存在redis中 spring.session.store-typeredis #namespace用于区分不同应用的分布式session spring.session.redis.namespacedemo #session更新到redis的模式分为on_save和immediate,on_save是当执行执行完响应以后才将数据同步到redisimmediate是在使用session的set等操作时就同步将数据更新到redis spring.session.redis.flush-modeon_save#redis配置针对springboot2.X以下版本 #redis的数据库默认为0 spring.redis.database0 #redis主机 spring.redis.host #redis端口 spring.redis.port6380 #redis密码 spring.redis.password #连接池最大连接数使用负值表示没有限制 spring.redis.pool.max-active40 #连接池最大阻塞等待时间使用负值表示没有限制 spring.redis.pool.max-wait1000 #连接池中的最大空闲连接 spring.redis.pool.max-idle5 #连接超时时间单位毫秒 spring.redis.timeout1000三、使用spring redis session 在spring入口应用上使用注解EnableRedisHttpSession开启spring redis session。 3.1 配置事件通知 当使用EnableRedisHttpSession时sessionMessageListener和redis KESPACE events将会被自动配置这将在session失效时能够通知到应用以便应用可以进行相关的操作。 值得注意的是这个功能需要redis 2.8以上版本并且开启事件通知 而事件通知在一般情况下都是关闭的所以需要特别进行开启,方法有两种: 配置文件 notify-keyspace-events Egx使用config命令 config set notify-keyspace-events Egx若是不需要使用到session失效通知可以取消这个自动配置 Bean public static ConfigureRedisAction configureRedisAction(){return ConfigureRedisAction.NO_OP; }3.2 配置监听线程池 Spring session监听默认采用的线程池是SimpleAsyncTaskExecutor虽然名义上也是线程池但是实际上每次请求都会创建新的线程这在流量较大的情况下如果响应较慢大量请求将会导致出现大量的线程容易导致OOM。一般情况下可以自行定义线程池如下例子: Bean public ThreadPoolTaskExecutor threadPoolTaskExecutor(){ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(10);executor.setMaxPoolSize(30);executor.setKeepAliveSeconds(30)executor.setThreadNamePrefix(spring-redis-)return executor; }3.3 设置session Id传递方式 默认情况下session id通过cookie进行传递但是有些浏览器不支持cookie,或者用户主动关闭了cookie所以cookie的方式不是最稳妥的spring redis提供了将session id放到请求头的方式,设置x-auth-token header头对应的值就是session id,在首次请求以后spring会将x-auth-token放到header中后续前端请求的时候需要继续将对应的session id放到header头中。 Bean public HttpSessionStrategy httpSessionStrategy(){return new HeaderHttpSessionStrategy(); }综上整体的配置如下: SpringBootConfiguration ConfigurationProperties(prefixspring.session.custom.executor) public class SpringRedisSessionConfig(){private Integer corePoolSize;private Integer maxPoolSize;private Integer keepAliveSeconds;private String threadNamePrefix;/*与HttpSessionListener两者选其一*/Beanpublic static ConfigureRedisAction configureRedisAction(){return ConfigureRedisAction.NO_OP;}Beanpublic HttpSessionListener httpSessionListener(){return new HttpSessionListener();}Beanpublic ThreadPoolTaskExecutor threadPoolTaskExecutor(){ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(this.corePoolSize);executor.setMaxPoolSize(this.maxPoolSize);executor.setKeepAliveSeconds(this.keepAliveSeconds)executor.setThreadNamePrefix(this.threadNamePrefix)return executor;}Beanpublic HttpSessionStrategy httpSessionStrategy(){return new HeaderHttpSessionStrategy();}public Integer getCorePoolSize(){return corePoolSize;}public Integer getMaxPoolSize(){return maxPoolSize;}public Integer getKeepAliveSeconds(){return keepAliveSeconds;}public String getThreadNamePrefix(){return threadNamePrefix;} } 文章转载自: http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.njddz.cn.gov.cn.njddz.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.qsy36.cn.gov.cn.qsy36.cn http://www.morning.nbsfb.cn.gov.cn.nbsfb.cn http://www.morning.spsqr.cn.gov.cn.spsqr.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.dswtz.cn.gov.cn.dswtz.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.bsrqy.cn.gov.cn.bsrqy.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.rqbkc.cn.gov.cn.rqbkc.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.fewhope.com.gov.cn.fewhope.com http://www.morning.qgghj.cn.gov.cn.qgghj.cn http://www.morning.wkwds.cn.gov.cn.wkwds.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.gywfp.cn.gov.cn.gywfp.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.aa1585.com.gov.cn.aa1585.com http://www.morning.rdsst.cn.gov.cn.rdsst.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.mqldj.cn.gov.cn.mqldj.cn http://www.morning.lfdrq.cn.gov.cn.lfdrq.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.nxzsd.cn.gov.cn.nxzsd.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.hxhrg.cn.gov.cn.hxhrg.cn http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.xtlty.cn.gov.cn.xtlty.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.mkhwx.cn.gov.cn.mkhwx.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.ymwcs.cn.gov.cn.ymwcs.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.wxqmc.cn.gov.cn.wxqmc.cn 查看全文 http://www.tj-hxxt.cn/news/246356.html 相关文章: 福州医疗网站建设网站建设方面的知识 linux系统如何做网站免费网站正能量小说 对小米网站的建设意见深圳建网站找哪家 余姚网站建设在哪里马铃薯交易网站建设方案 php网站开发文本格式设置大连网站建设方案 贵州网络公司网站建设网站维护服务 邢台专业网站建设源码网站怎么不要钱自己做 浙江省建设厅网站在哪里广州代理记账 房地产企业网站开发建设银行 上海科技中心网站 徐州丰县建设局网站做网站重要标签 做网站有什么用专业制作网站服务公司 宁津网站设计深圳龙华区是郊区吗 苏州网站建站推广免费搭建永久网站 制作网站开发多少钱lnmp安装wordpress限权 手表网站背景wordpress更换域名首页无法访问 wordpress手机网站怎么做wordpress移动版修改 网站开发包括什么o2o网站建设渠道 贵州省住房和城乡建设厅网网站在线网站建设费用是多少 张家口网站设计怎么分析一个网站seo 那个网站是响应式的做网站的前端技术 flash网站什么意思企业网站模板优化 快速搭建网站软件wordpress 国内主机 仓储服务 东莞网站建设 技术支持如何做彩票销售网站 个人php网站wordpress视频去广告插件 简单做图网站佛山哪家公司建设网站 电子商务网站建设期末试题答案05wordpress积分提现 免费软件下载官方网站茶网站建设宗旨 jsp网站开发实训报告小微企业名录 免费学校网站系统企业网站优化报告 网站注册界面设计园林建设网站