coding
maojianyun的gravatar头像
maojianyun2018-07-26 17:56:05

spring boot+redis实现共享java servlet session对象

项目描述

springboot+redis进行session对象共享

运行环境

jdk8+Spring boot+redis+eclipse+maven

项目技术(必填)

springboot + redis

数据库文件

自己安装redis数据库

jar包文件

maven管理

是否原创(转载必填原文地址)

原创

项目截图(必填)

spring boot+redis实现共享java servlet session对象

spring boot+redis实现共享java servlet session对象

spring boot+redis实现共享java servlet session对象

spring boot+redis实现共享java servlet session对象

运行截图(必填)

1.访问8082的效果

spring boot+redis实现共享java servlet session对象

http://localhost:8082/getSessionId

spring boot+redis实现共享java servlet session对象

2.再启动8083端口的程序

spring boot+redis实现共享java servlet session对象

http://localhost:8083/getSessionId

spring boot+redis实现共享java servlet session对象

对比发现不同的sessionId对应的内容是相同的达到session共享的目的

3.redis中的数据

spring boot+redis实现共享java servlet session对象

注意事项

1.在配置文件中配置spring.session.store-type=redis才是使用redis做为session信息存储

2.在idea中需要添加servlet的依赖,请使用如下的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.singhand</groupId>
	<artifactId>springboot-session-redis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!-- 依赖仓库 设置从aliyun仓库下载 -->
	<repositories>
		<repository>
			<id>alimaven</id>
			<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
			<releases>
				<enabled>true</enabled>
			</releases>
		</repository>
	</repositories>
	<!-- 插件依赖仓库 -->
	<pluginRepositories>
		<pluginRepository>
			<id>alimaven</id>
			<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
			<releases>
				<enabled>true</enabled>
			</releases>
		</pluginRepository>
	</pluginRepositories>
	<properties>
		<!-- 文件拷贝时的编码 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- 编译时的编码 -->
		<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

		<spring_versin>1.5.2.RELEASE</spring_versin>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<!-- <version>${spring_versin}</version> -->

			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>

		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<!-- <version>${spring_versin}</version> -->
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
			<version>1.4.6.RELEASE</version>
		</dependency>


		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>

		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<!-- <version>3.1</version> -->
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


最代码官方编辑于Aug 1, 2018 5:57:52 PM


打赏

已有1人打赏

最代码官方的gravatar头像

文件名:springboot-session-redis.zip,文件大小:17.659K下载
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友