SpringBoot 如何實(shí)現(xiàn)Session共享
HttpSession,是通過(guò)Servlet容器創(chuàng)建并進(jìn)行管理的,創(chuàng)建成功以后將會(huì)保存在內(nèi)存中,這里將會(huì)使用Redis解決session共享的問(wèn)題。
創(chuàng)建項(xiàng)目

添加pom
添加相關(guān)的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
創(chuàng)建Controller用來(lái)執(zhí)行測(cè)試操作
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 負(fù)載均衡
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;
}
}
請(qǐng)求分發(fā)
保存數(shù)據(jù)

獲取數(shù)據(jù)

以上就是SpringBoot 如何實(shí)現(xiàn)Session共享的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 實(shí)現(xiàn)Session共享的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 淺談Spring Session工作原理
- SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例
- SpringCloud Feign轉(zhuǎn)發(fā)請(qǐng)求頭(防止session失效)的解決方案
- springboot中的springSession的存儲(chǔ)和獲取實(shí)現(xiàn)
- 多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享功能
- Springboot中登錄后關(guān)于cookie和session攔截問(wèn)題的案例分析
- Springboot Session共享實(shí)現(xiàn)原理及代碼實(shí)例
- SpringBoot中實(shí)現(xiàn)分布式的Session共享的詳細(xì)教程
- spring-redis-session 自定義 key 和過(guò)期時(shí)間
- SpringBoot2.x 整合Spring-Session實(shí)現(xiàn)Session共享功能
- Spring Session的使用示例
相關(guān)文章
提示:Decompiled.class file,bytecode version如何解決
在處理Decompiled.classfile和bytecodeversion問(wèn)題時(shí),通過(guò)修改Maven配置文件,添加阿里云鏡像并去掉默認(rèn)鏡像,解決了下載源的問(wèn)題,同時(shí),檢查并修改了依賴版本,確保了問(wèn)題的解決2024-12-12
JavaWeb開(kāi)發(fā)入門第二篇Tomcat服務(wù)器配置講解
JavaWeb開(kāi)發(fā)入門第二篇主要介紹了Tomcat服務(wù)器配置的方法教大家如何使用Tomcat服務(wù)器,感興趣的小伙伴們可以參考一下2016-04-04
Java利用trueLicense實(shí)現(xiàn)項(xiàng)目離線證書(shū)授權(quán)操作步驟
文章介紹了如何使用trueLicense實(shí)現(xiàn)離線授權(quán)控制,包括生成公私鑰、創(chuàng)建證書(shū)校驗(yàn)?zāi)K、生成證書(shū)模塊和測(cè)試模塊,通過(guò)這種方式,可以控制用戶使用的項(xiàng)目模塊、授權(quán)周期、使用的設(shè)備和服務(wù)器,感興趣的朋友跟隨小編一起看看吧2024-11-11
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):算法
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛(ài)好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-07-07
Java使用POI從Excel讀取數(shù)據(jù)并存入數(shù)據(jù)庫(kù)(解決讀取到空行問(wèn)題)
有時(shí)候需要在java中讀取excel文件的內(nèi)容,專業(yè)的方式是使用java POI對(duì)excel進(jìn)行讀取,這篇文章主要給大家介紹了關(guān)于Java使用POI從Excel讀取數(shù)據(jù)并存入數(shù)據(jù)庫(kù),文中介紹的辦法可以解決讀取到空行問(wèn)題,需要的朋友可以參考下2023-12-12

