资讯专栏INFORMATION COLUMN

spring-session实现分布式集群session的共享

AdolphLWQ / 1037人阅读

摘要:实现分布式集群的共享共享本文使用实现共享,基于实现想使用基于容器的共享请搜索其他文章本文不讲解基础环境搭建,需要使用等相关知识点,不做介绍未做共享整体项目结构基础代码未做共

title: spring-session实现分布式集群session的共享
tags: springboot,spring,session共享

grammar_cjkRuby: true

**本文使用springboot实现session共享,基于spring session实现
想使用基于容器的session共享请搜索其他文章

本文不讲解基础环境搭建,需要使用idea、maven、springboot等相关知识点,不做介绍

未做session共享 整体项目结构

基础代码: pom.xml


    4.0.0

    com.xiang.springboot.demo
    springboot-demo
    0.0.1-SNAPSHOT
    jar
    springboot-demo
    Demo project for Spring Boot
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

SpringbootDemoApplication.java
package com.xiang.springboot.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

@SpringBootApplication
public class SpringbootDemoApplication implements ServletContextAware {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }

    @Override
    public void setServletContext(ServletContext context) {
        String ctx = context.getContextPath();
        System.out.println("ctx=" + ctx);
        context.setAttribute("ctx", ctx);
    }
}
TestController.java
package com.xiang.springboot.demo.module1.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/module1")
public class TestController {
    @GetMapping("/page1")
    public String page1(HttpServletRequest request, HttpServletResponse response, RedirectAttributes attr, ModelMap map) {
        System.out.println(attr.getFlashAttributes().get("test"));
        System.out.println("aaaaaaaaaaaaaa" + map.get("test"));
        HttpSession session = request.getSession();
        session.setAttribute("sessiontest","session test text...."+session.getId());
        String url = request.getScheme()+":111//"+request.getServerName()+":"+request.getServerPort()+"-"+request.getLocalPort()+"/"+request.getContextPath()+"/"+request.getRequestURI();
        session.setAttribute("ctpath",url);
        return "module1/page1";
    }

    @GetMapping("/page2")
    public String page2(HttpServletRequest request, HttpServletResponse response, RedirectAttributes attr) {
        attr.addFlashAttribute("test", "testaaaaa");
        return "redirect:/module1/page1";
    }

}
page1.html


    
    Spring MVC + Thymeleaf Example



 Hello,

ContextPath, !
Hello, !
session, !
未做session共享之前的结果

session共享 项目结构
整体项目结构与原来保持一致
pom.xml
原pom.xml中加入
        
            org.springframework.boot
            spring-boot-starter-redis
            1.3.8.RELEASE
            
        
        
            org.springframework.session
            spring-session-data-redis
        
application.yml

将原来的application.properties改名了

加入:

spring:
  redis:
    database: 0
    host: ***
    port: 6379
    password: ***
    timeout: 0
    pool:
      max-active: 8
      max-wait: -1
      max-idle: 8
      min-idle: 0
    session:
      store-type: none

加入新文件RedisHttpSessionConfig.java

package com.xiang.springboot.demo.module1.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
//maxInactiveIntervalInSeconds 默认是1800秒过期,这里测试修改为60秒
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60)
public class RedisHttpSessionConfig{

}

其他不用变

最终结果

代码地址:
https://gitee.com/soft_xiang/...

---------------2018.06.20 更新-----------------------------------------------

使用spring session之后原httpsessionListener的创建和销毁session的监听器会失效
处理方法:https://docs.spring.io/spring...

代码如下:

@Configuration
//maxInactiveIntervalInSeconds 默认是1800秒过期,这里测试修改为60秒
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=180)
public class RedisHttpSessionConfig{

    @Bean
    public SessionEventHttpSessionListenerAdapter getSessionEventHttpSessionListenerAdapter(){
        List listeners = new ArrayList<>();
        listeners.add(getHttpSessionListener());
        return new SessionEventHttpSessionListenerAdapter(listeners);
    }

    @Bean
    public HttpSessionListener getHttpSessionListener(){
        return new MyHttpSessionListener();
    }
}

---------------2018.06.20 更新end-----------------------------------------------

参考:

不是springboot实现方式,但讲的较好,推荐
https://www.cnblogs.com/youzh...

springboot方式,有一些踩坑记录
https://blog.csdn.net/dream_b...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72056.html

相关文章

  • 解决spring-session在redis集群下监听expired事件失败

    摘要:问题正如上描述,事件有时监听会丢失,不支持集群这种场景。只有订阅和创建连接同时连接到一台节点才能监听到这个产生的事件。解决方案自己对所有集群主备节点进行事件订阅。 问题:正如github上issue描述,expired事件有时监听会丢失,spring-session不支持redis集群这种场景。https://github.com/spring-pro... 原因:spring-ses...

    xeblog 评论0 收藏0
  • Java 类文章 - 收藏集 - 掘金

    摘要:而调用后端服务就应用了的高级特分布式配置管理平台后端掘金轻量的分布式配置管理平台。关于网络深度解读后端掘金什么是网络呢总的来说,网络中的容器们可以相互通信,网络外的又访问不了这些容器。 在 Java 路上,我看过的一些书、源码和框架(持续更新) - 后端 - 掘金简书 占小狼转载请注明原创出处,谢谢!如果读完觉得有收获的话,欢迎点赞加关注 物有本末,事有终始,知所先后,则近道矣 ......

    RayKr 评论0 收藏0
  • 如果连铁将军都不再可靠--记一次排查使用布式轮候锁+SESSION防订单重复仍然加锁失效问题经历

    摘要:尽可能地将数据写入,例如创建设置的都会将数据立即的写入再来看看文档怎么描述的看看这可爱的默认值我们终于知道了当我们不做任何设置时,默认采用的是方式显而易见,使用方式能最大限度的减少与的交互,而在大多数场景下都是没有问题的。 0.问题背景 此次问题源于一次挺严重的生产事故:客户的订单被重复生成了,而出问题的代码其实很简单: // .... redisLockUtil.lock(membe...

    econi 评论0 收藏0

发表评论

0条评论

AdolphLWQ

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<