有几种办法:

1.扩展指定server
利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略。缺点:耦合Tomcat/Jetty等Servlet容器,不能随意更换容器。

2.利用Filter
利用HttpServletRequestWrapper,实现自己的 getSession()方法,接管创建和管理Session数据的工作。spring-session就是通过这样的思路实现的。

3 利用spring session

Spring Boot中spring session支持方式:

JDBC、MongoDB、Redis、Hazelcast、HashMap

一、引入maven依赖

<properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependencies>

二、配置application.properties

server.port=8080
spring.redis.host=localhost
spring.redis.port=6379 # spring session使用存储类型
spring.session.store-type=redis
  • spirngboot默认就是使用redis方式,如果不想用可以填none。、

三、在启动类中加入@EnableRedisHttpSession  注解

package com.shyroke;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @EnableCaching
@EnableRedisHttpSession
@SpringBootApplication
public class SpringbootSessionApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootSessionApplication.class, args);
}
}

四、编写控制器

package com.shyroke.controller;

import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping(value = "/")
public class IndexController { @ResponseBody
@RequestMapping(value = "/session")
public Map<String, Object> getSession(HttpServletRequest request) {
request.getSession().setAttribute("username", "admin");
Map<String, Object> map = new HashMap<String, Object>();
map.put("sessionId", request.getSession().getId());
return map;
} @ResponseBody
@RequestMapping(value = "/get")
public String get(HttpServletRequest request) {
String userName = (String) request.getSession().getAttribute("username"); return userName;
} }

五、测试

  • 先输入http://localhost:8080/session,在session中设置一个值

  • http://localhost:8080/get,获取session中的值



  • 复制这个工程,application.properties中的server.port=8081,然后访问“http://localhost:8081/get”,如下获取到了另一个工程中设置的session的值。

SpringBoot2 使用Spring Session集群的更多相关文章

  1. (十九)SpringBoot之使用Spring Session集群-redis

    一.引入maven依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnc ...

  2. Springboot Session集群处理

    在集群环境下,常见的基于Session的身份认证就会有一个问题,因为Session是跟着服务器走的,当用户在服务器1登陆成功后,当用户在访问服务器2的时候会因为服务器2没有用户的身份信息而再次跳转到认 ...

  3. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

  4. Springboot2.x集成Redis集群模式

    Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...

  5. Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群

    Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...

  6. Tomcat session集群

    author:JevonWei 版权声明:原创作品 环境 tomcatA 172.16.253.108 tomcatB 172.16.253.105 代理服务器 172.16.253.191 Tomc ...

  7. Spring+quartz集群解决多服务器部署定时器重复执行的问题

    一.问题描述 Spring自带的Task虽然能很好使用定时任务,只需要做些简单的配置就可以了.不过如果部署在多台服务器上的时候,这样定时任务会在每台服务器都会执行,造成重复执行. 二.解决方案 Spr ...

  8. spring boot 集群 + Nginx --- 心得

    1.前言 已经掌握了spring cloud 得使用 ,但这是在内部网络做业务 ,现在需要 在外部网络 访问内部网络 服务 ,引入了 服务端负载均衡 Nginx , Nginx 根据预定的策略 ,将请 ...

  9. spring+redis 集群下的操作

    文章就是记录一下工作当中的用到的点,与测试方法以备用,会不断更新. 配置文件spring-redis.xml: <?xml version="1.0" encoding=&q ...

随机推荐

  1. css的基础用法之标签选择

    一.css的4种引入方式 #.内联式 <p style="color: red;font-size: 50px;text-align: center">Egon是一个非 ...

  2. 49.UILable宽度高度自适应

    第一种: UILabel *labl = [[UILabel alloc]init]; labl.backgroundColor = [UIColor redColor]; labl.numberOf ...

  3. 824. Goat Latin

    class Solution { public: string toGoatLatin(string S) { S.push_back(' '); //add a space that the loo ...

  4. JAVA折腾微信公众平台(Token验证)[转]

    JAVA折腾微信公众平台(Token验证) BAE的JAVA还在内测的时候,抱着好奇的态度发邮件申请了内测权限,当时折腾了一天,然后就没折腾了.现在BAE的JAVA都已经正式开放使用了,我又蛋疼的想写 ...

  5. Tomcat服务器的安装和配置

    一.Tomcat下载 可以直接从Apache的网站上下载Tomcat(http://tomcat.apache.org/),进入首页后,在左边Download一栏可选择你要下载的版本,点击便可进入To ...

  6. Mybatis之动态构建SQL语句(转)

    https://www.cnblogs.com/zhangminghui/p/4903351.html

  7. Linux下通过管道杀死所有与tomcat相关的进程

    先将正确的命令放上来: ps -ef | grep ps -ef将系统中运行的进程展示出来 选择带有tomcat的进程后同时去除自身带有grep的进程,毕竟本身运行的这条命令是与tomcat相关的 a ...

  8. Promise.all函数的使用

    Promise.all([this.getCity('guess'),this.getCity('hot'),this.getCity('group')]).then(res=>{ // con ...

  9. ASP.NET 压缩输出的HTML字符

    重写Render using System; using System.Collections.Generic; using System.Text; using System.Web.UI; usi ...

  10. Android webview 退出时关闭声音 4.视频全屏 添加cookie

    全屏问题,可以参考 http://bbs.csdn.net/topics/390839259,点击 webView = (WebView) findViewById(R.id.webView); vi ...