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


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...

你可能感兴趣的:(spring-session,java,session)