SpringBoot 如何实现Session共享
更新时间:2020年09月03日 09:34:17 作者:mySoul
这篇文章主要介绍了SpringBoot 如何实现Session共享,帮助大家更好的理解和学习spring boot框架,感兴趣的朋友可以了解下
HttpSession,是通过Servlet容器创建并进行管理的,创建成功以后将会保存在内存中,这里将会使用Redis解决session共享的问题。
创建项目

添加pom
添加相关的maven
<?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 https://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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.0.0.M1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置redis连接
配置redis连接
spring:
redis:
database: 0
host: 106.53.115.12
port: 6379
password: 12345678
jedis:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
创建Controller用来执行测试操作
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class HelloController {
@PostMapping("/save")
public String saveName(String name, HttpSession session){
session.setAttribute("name", name);
return "8080";
}
@GetMapping("/get")
public String getName(HttpSession httpSession){
return httpSession.getAttribute("name").toString();
}
}
Nginx 负载均衡
mingming@xiaoming-pc:~$ sudo apt-get install nginx
修改配置文件
upstream sang.com {
server 192.168.0.1:8080 weight = 1;
server 192.168.0.2:8080 weight = 1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://sang.com;
proxy_redirect default;
}
}
请求分发
保存数据

获取数据

以上就是SpringBoot 如何实现Session共享的详细内容,更多关于SpringBoot 实现Session共享的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章:
- 浅谈Spring Session工作原理
- SpringBoot+SpringSession+Redis实现session共享及唯一登录示例
- SpringCloud Feign转发请求头(防止session失效)的解决方案
- springboot中的springSession的存储和获取实现
- 多个SpringBoot项目采用redis实现Session共享功能
- Springboot中登录后关于cookie和session拦截问题的案例分析
- Springboot Session共享实现原理及代码实例
- SpringBoot中实现分布式的Session共享的详细教程
- spring-redis-session 自定义 key 和过期时间
- SpringBoot2.x 整合Spring-Session实现Session共享功能
- Spring Session的使用示例
相关文章
提示:Decompiled.class file,bytecode version如何解决
在处理Decompiled.classfile和bytecodeversion问题时,通过修改Maven配置文件,添加阿里云镜像并去掉默认镜像,解决了下载源的问题,同时,检查并修改了依赖版本,确保了问题的解决2024-12-12
Java利用trueLicense实现项目离线证书授权操作步骤
文章介绍了如何使用trueLicense实现离线授权控制,包括生成公私钥、创建证书校验模块、生成证书模块和测试模块,通过这种方式,可以控制用户使用的项目模块、授权周期、使用的设备和服务器,感兴趣的朋友跟随小编一起看看吧2024-11-11
Java使用POI从Excel读取数据并存入数据库(解决读取到空行问题)
有时候需要在java中读取excel文件的内容,专业的方式是使用java POI对excel进行读取,这篇文章主要给大家介绍了关于Java使用POI从Excel读取数据并存入数据库,文中介绍的办法可以解决读取到空行问题,需要的朋友可以参考下2023-12-12


最新评论