ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Spring session + SpringBoot + redis 实现session共享

2019-06-27 14:48:22  阅读:220  来源: 互联网

标签:redis SpringBoot Spring boot springframework session spring org


redis 安装参考: https://blog.csdn.net/u013792404/article/details/93873585

nginx安装参考: https://mp.csdn.net/postedit/93863306

 

springboot 版本:2.0.6

maven依赖:   spring-boot-starter-data-redis   和   spring-session-data-redis

<?xml version="1.0" encoding="UTF-8"?>
<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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.mei.springboot.springSession</groupId>
	<artifactId>SpringBoot-SpringSession</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</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>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>

	</dependencies>


	<build>
		<!-- -->
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
					<useDefaultDelimiters>true</useDefaultDelimiters>
				</configuration>
			</plugin>
			<plugin>
				<!-- maven插件 -->
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!-- 此处必须指定为1.4.2.RELEASE , 为SpringBoot的bug -->
				<!-- 打包只能用1.4.2.RELEASE 版本的,其它版本的都不可以。这个目前是springboot的bug。目前还未看到官方的解说。 -->
				<version>1.4.2.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
				<configuration>
					<mainClass>com.mei.springboot.rocket.SpringBootTestApplication</mainClass>
					<includeSystemScope>true</includeSystemScope>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<!-- <version>3.3</version> -->
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- 避免报错: pom.xml报错 web.xml is missing and <failOnMissingWebXml> is set 
				to true -->
			<!-- <plugin> -->
			<!-- <groupId>org.apache.maven.plugins</groupId> -->
			<!-- <artifactId>maven-war-plugin</artifactId> -->
			<!-- <configuration> -->
			<!-- <failOnMissingWebXml>false</failOnMissingWebXml> -->
			<!-- </configuration> -->
			<!-- </plugin> -->
		</plugins>
		<!-- 此处最为重要-否则打包后没有你的JSP页面模板,一定要记得写为资源文件 targetPath 设置必须是 META-INF/resources 
			我项目做出来的时候因为没写resources导致打包运行后正常启动,各个接口也都正常访问,但是所有的jsp页面都会报404,找不到页面 ,因为这个问题浪费了我大半天时间 -->
		<resources>
			<!-- 打包时将jsp文件拷贝到META-INF目录下 -->
			<resource>
				<!-- 指定resources插件处理哪个目录下的资源文件 -->
				<directory>src/main/webapp</directory>
				<!--注意此次必须要放在此目录下才能被访问到 -->
				<targetPath>META-INF/resources</targetPath>
				<includes>
					<include>**/**</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/**</include>
				</includes>
				<!-- 这个要加上,不然fontswone等字体库读不到 -->
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/java</directory>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>
	</build>


</project>

 

RedisSessionConfig.java

package com.springboot.rocket.controller;

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

@Configuration  
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class RedisSessionConfig {

}

 

session 测试类

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping(value = "/session", method = RequestMethod.GET)  
    public Object sessions (HttpServletRequest request){  
        Map<String, Object> map = new HashMap<>();  
        map.put("sessionId", request.getSession().getId());  
        map.put("host.port", "8081"); 
        return map;  
    } 
}

application.yml


server:
  port: 8081

      
spring:
  redis:
    host: 192.168.8.107 
    port: 6379
  application:
    name: SpringSession

      

 

将项目打包成2个jar包,

 

项目启动后配置到nginx;nginx.conf配置如下:

 

 

访问: http://192.168.8.107 , 结果如下:

发现端口不同(即不同应用), 但sessionId 是相同的, 即实现了session共享。 

 

 

springboot + spring session + redis集群参考:  https://blog.csdn.net/qq_39089022/article/details/82347843

 

 

 

 

 

 

 

 

标签:redis,SpringBoot,Spring,boot,springframework,session,spring,org
来源: https://blog.csdn.net/u013792404/article/details/93874464

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有