SpringBoot 如何實(shí)現(xiàn)Session共享
HttpSession,是通過Servlet容器創(chuàng)建并進(jìn)行管理的,創(chuàng)建成功以后將會(huì)保存在內(nèi)存中,這里將會(huì)使用Redis解決session共享的問題。
創(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用來執(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;@RestControllerpublic 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; }}
請求分發(fā)
保存數(shù)據(jù)
獲取數(shù)據(jù)
以上就是SpringBoot 如何實(shí)現(xiàn)Session共享的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 實(shí)現(xiàn)Session共享的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測可用)3. CSS hack用法案例詳解4. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)8. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法9. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明10. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息
