Spring session + SpringBoot + redis 实现session共享
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring session + SpringBoot + redis 实现session共享,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5294字,纯文字阅读大概需要8分钟。
内容图文
![Spring session + SpringBoot + redis 实现session共享](/upload/InfoBanner/zyjiaocheng/906/142671921cc44693951f784c0ea1a18f.jpg)
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
内容总结
以上是互联网集市为您收集整理的Spring session + SpringBoot + redis 实现session共享全部内容,希望文章能够帮你解决Spring session + SpringBoot + redis 实现session共享所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。