SpringBoot+Redis实现HttpSession共享

前提:需要使用redis做session存储

一、效果演练(这里使用SpringBoot工程,Spring同理)

  1.一个工程使用两个端口启用,或者两个工程两个端口启用(这里使用一个工程两个端口号启动)
    (1)#设置properties配置的服务器端口号
      server.port=8081
      然后启动。
    (2)#设置properties配置的服务器端口号
      server.port=8082
      然后启动。
    到此,已经启动两个工程。   2.分别访问工程8081和8082
    (1)8081

       

     (2)8082

       

    (3)8081添加session信息,name="丁洁"

       

    (4)再次查看8081和8082的session信息

       

       

    (5)清除8082的session name 再分别查看8081和8082的session信息

       

       

       

二、源码分享

  1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.szw</groupId>
<artifactId>springboot_nginx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_nginx</name> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath></relativePath>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.test.skip>true</maven.test.skip>
<skipTests>true</skipTests>
<java.version>1.8</java.version>
<start-class>com.nginx.session.NginxSessionApplication</start-class>
</properties> <dependencies>
<!-- 使用web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 使用aop模板启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring-Session -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<!-- 打可运行的jar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<!-- 打源码包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> <!-- maven私服deploy -->
<distributionManagement>
<repository>
<id>releases</id>
<name>Releases</name>
<url>http://192.168.3.71:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshots</name>
<url>http://192.168.3.71:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement> <repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories> </project>

  2.application.properties

#设置项目contex路径
#server.context-path=/nginx/session
#服务器端口号
server.port=8081
#server.port=8082
#会话session超时时长
server.session.timeout=1800 ## redis地址
spring.redis.host=192.168.159.129
## redis端口
spring.redis.port=7001
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=300
## Redis数据库索引(默认为0)
spring.redis.database=0
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=100
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=20
## 连接超时时间(毫秒)
spring.redis.timeout=60000

  3.controller

package com.nginx.session.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloWorldContoller {
@Value("${server.port}")
private String port; @RequestMapping("/")
public String helloWold(HttpServletRequest req){
HttpSession session = req.getSession();
Object obj = req.getSession().getAttribute("name");
String name = null!=obj?obj.toString():null;
return "<h2>hello world<br/>我的端口号是:"+port+"<br/>我的sessionId:"+session.getId()+"<br/>session name = "+name+"</h2>";
} @RequestMapping("add/{name}")
public String addSession(HttpServletRequest req,@PathVariable("name")String name){
if(StringUtils.isEmpty(name)){
return "add/{name}不能为空";
}else{
req.getSession().setAttribute("name", name);
return "<h1>设置session name = "+name+"</h1>";
}
} @RequestMapping("get")
public String getSession(HttpServletRequest req){
Object obj = req.getSession().getAttribute("name");
String name = null!=obj?obj.toString():null;
return "<h1>获取session name = "+name+"</h1>";
} @RequestMapping("clear")
public String clearSession(HttpServletRequest req){
req.getSession().removeAttribute("name");
Object obj = req.getSession().getAttribute("name");
String name = null!=obj?obj.toString():null;
return "<h1>清除session name = "+name+"</h1>";
}
}

  4.Application启动类

package com.nginx.session;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class NginxSessionApplication { public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(NginxSessionApplication.class, args);
}
}

  5.工程截图

  

Spring session共享(使用redis)的更多相关文章

  1. 【Spring Session】和 Redis 结合实现 Session 共享

    [Spring Session]和 Redis 结合实现 Session 共享 参考官方文档 HttpSession with Redis Guide https://docs.spring.io/s ...

  2. 大话 Spring Session 共享

    javaweb中我们项目稍微正规点,都会用到单点登录这个技术.实现它的方法各家有各界的看法.这几天由于公司项目需求正在研究.下面整理一下最近整理的心得. 简介 在分布式项目中另我们头疼的是多项目之间的 ...

  3. spring-session之四:Spring Session下的Redis存储结构

    spring-session项目启动后 127.0.0.1:6379> keys * 1) "spring:session:index:org.springframework.sess ...

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

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

  5. session共享同步redis策略

    关于session共享的文章,网上很多,可是最关键的点我没有看到一篇.也就是session对象到底是怎么同步到redis的. spring-session底层原理到底是怎么样的一个同步更新策略,我没有 ...

  6. Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享

    小伙伴们好久不见!最近略忙,博客写的有点少,嗯,要加把劲.OK,今天给大家带来一个JavaWeb中常用的架构搭建,即Nginx+Tomcat搭建服务集群,然后通过Spring Session+Redi ...

  7. Spring Session + Redis实现分布式Session共享

    发表于 2016-09-29 文章目录 1. Maven依赖 2. 配置Filter 3. Spring配置文件 4. 解决Redis云服务Unable to configure Redis to k ...

  8. Spring Boot从入门到精通(七)集成Redis实现Session共享

    单点登录(SSO)是指在多个应用系统中,登录用户只需要登录验证一次就可以访问所有相互信任的应用系统,Redis Session共享是实现单点登录的一种方式.本文是通过Spring Boot框架集成Re ...

  9. springboot+redis实现分布式session共享

    官方文档,它是spring session项目的redis相关的一个子文档:https://docs.spring.io/spring-session/docs/2.0.0.BUILD-SNAPSHO ...

随机推荐

  1. python升级导致yum命令无法使用的解决

    1.报错信息如下: [root@develop bin]# yum [root@develop local]# yum -y install prce There was a problem impo ...

  2. 基于HTML5 audio元素播放声音jQuery小插件

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1609 一.前面的些唠 ...

  3. Photoshop脚本之创建文件夹

    function checkFolder(path){ var folder = Folder(path) if(!folder.exists) folder.create() }

  4. MySQL - 统计每个月生日的人数

    Person表定义如下: create table person(id int primary key auto_increment, birthday datetime); Person 数据如下: ...

  5. 一步步教你搭建TinyOS2.1.2开发环境

    (本教程使用的是VirtualBOX +ubuntu14.04+tinyos2.1.2) note:看了非常多的tinyos的安装教程.区别不是非常大,无非就是安装编译器配置环境等.尽管简单,但还是有 ...

  6. Laravel使用ORM操作数据

    数据表 CREATE TABLE IF NOT EXISTS students( `id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NO ...

  7. python 中NumPy和Pandas工具包中的函数使用笔记(方便自己查找)

    二.常用库 1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数据进行快速运算的标准 ...

  8. sql server 2008获取表的字段注释

    SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号=a.colorder, 字段名=a.n ...

  9. [Spring Data Repositories]学习笔记--为repository添加通用的方法

    如果想把一个方法加到所有的repository中,用前一篇提到的方法就不合适了. 英文原版,请看 http://docs.spring.io/spring-data/data-mongo/docs/1 ...

  10. MVC路由自定义及视图找寻规则

    这篇关于MVC路由及视图规则本来是昨天要发的,但是本人真的有点懒,终于今天忍无可忍了.初学MVC的时候比现在还菜一点(现在也很菜),想着会用就行,但是有时还是会好奇,为什么它能找到控制器?为什么控制器 ...