SpringBoot 如何實現(xiàn)Session共享
HttpSession,是通過Servlet容器創(chuàng)建并進行管理的,創(chuàng)建成功以后將會保存在內(nèi)存中,這里將會使用Redis解決session共享的問題。
創(chuà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用來執(zhí)行測試操作
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; } }
請求分發(fā)
保存數(shù)據(jù)
獲取數(shù)據(jù)
以上就是SpringBoot 如何實現(xiàn)Session共享的詳細內(nèi)容,更多關(guān)于SpringBoot 實現(xiàn)Session共享的資料請關(guān)注腳本之家其它相關(guān)文章!
- 淺談Spring Session工作原理
- SpringBoot+SpringSession+Redis實現(xiàn)session共享及唯一登錄示例
- SpringCloud Feign轉(zhuǎn)發(fā)請求頭(防止session失效)的解決方案
- springboot中的springSession的存儲和獲取實現(xiàn)
- 多個SpringBoot項目采用redis實現(xiàn)Session共享功能
- Springboot中登錄后關(guān)于cookie和session攔截問題的案例分析
- Springboot Session共享實現(xiàn)原理及代碼實例
- SpringBoot中實現(xiàn)分布式的Session共享的詳細教程
- spring-redis-session 自定義 key 和過期時間
- SpringBoot2.x 整合Spring-Session實現(xiàn)Session共享功能
- Spring Session的使用示例
相關(guān)文章
springboot?log4j2日志框架整合與使用過程解析
這篇文章主要介紹了springboot?log4j2日志框架整合與使用,包括引入maven依賴和添加配置文件log4j2-spring.xml的相關(guān)知識,需要的朋友可以參考下2022-05-05Java中通過ZipOutputStream類如何將多個文件打成zip
ZipOutputStream?是Java中用于創(chuàng)建ZIP文件的類,它是?java.util.zip?包中的一部分,通過使用?ZipOutputStream?,可以將多個文件壓縮到一個ZIP文件中,這篇文章主要介紹了Java中(ZipOutputStream)如何將多個文件打成zip,需要的朋友可以參考下2023-09-09java?SpringBoot注解@Async不生效的解決方法
大家好,本篇文章主要講的是java?SpringBoot注解@Async不生效的解決方法,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01SpringBoot整合Hibernate Validator實現(xiàn)參數(shù)驗證功能
這篇文章主要介紹了SpringBoot整合Hibernate Validator實現(xiàn)參數(shù)驗證功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java常問面試內(nèi)容--數(shù)組、聲明、初始化、冒泡、多維數(shù)組、稀疏數(shù)組
這篇文章主要介紹了Java多線程面試題(面試官常問),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07