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)文章
springboot?log4j2日志框架整合與使用過(guò)程解析
這篇文章主要介紹了springboot?log4j2日志框架整合與使用,包括引入maven依賴(lài)和添加配置文件log4j2-spring.xml的相關(guān)知識(shí),需要的朋友可以參考下2022-05-05Java中通過(guò)ZipOutputStream類(lèi)如何將多個(gè)文件打成zip
ZipOutputStream?是Java中用于創(chuàng)建ZIP文件的類(lèi),它是?java.util.zip?包中的一部分,通過(guò)使用?ZipOutputStream?,可以將多個(gè)文件壓縮到一個(gè)ZIP文件中,這篇文章主要介紹了Java中(ZipOutputStream)如何將多個(gè)文件打成zip,需要的朋友可以參考下2023-09-09java?SpringBoot注解@Async不生效的解決方法
大家好,本篇文章主要講的是java?SpringBoot注解@Async不生效的解決方法,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01SpringBoot整合Hibernate Validator實(shí)現(xiàn)參數(shù)驗(yàn)證功能
這篇文章主要介紹了SpringBoot整合Hibernate Validator實(shí)現(xiàn)參數(shù)驗(yàn)證功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06java Lombok之@Accessors用法及說(shuō)明
這篇文章主要介紹了java Lombok之@Accessors用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java中四種9*9乘法表的實(shí)現(xiàn)方式(附代碼)
這篇文章主要介紹了Java中四種9*9乘法表的實(shí)現(xiàn)方式(附代碼),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11java使用枚舉封裝錯(cuò)誤碼及錯(cuò)誤信息詳解
這篇文章主要介紹了java使用枚舉封裝錯(cuò)誤碼及錯(cuò)誤信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java常問(wèn)面試內(nèi)容--數(shù)組、聲明、初始化、冒泡、多維數(shù)組、稀疏數(shù)組
這篇文章主要介紹了Java多線程面試題(面試官常問(wèn)),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07