您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“springcloud怎么實現(xiàn)Session共享”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
通常情況下,Tomcat、Jetty等Servlet容器,會默認將Session保存在內(nèi)存中。如果是單個服務(wù)器實例的應(yīng)用,將Session保存在服務(wù)器內(nèi)存中是一個非常好的方案。但是這種方案有一個缺點,就是不利于擴展。目前越來越多的應(yīng)用采用分布式部署,用于實現(xiàn)高可用性和負載均衡等。那么問題來了,如果將同一個應(yīng)用部署在多個服務(wù)器上通過負載均衡對外提供訪問,如何實現(xiàn)Session共享?實現(xiàn)Session共享的方案很多,其中一種常用的就是使用Tomcat、Jetty等服務(wù)器提供的Session共享功能,將Session的內(nèi)容統(tǒng)一存儲在一個數(shù)據(jù)庫(如MySQL)或緩存(如Redis)中。
下面我們將在springcloud微服務(wù)項目中,使用redis實現(xiàn)簡單高效的session共享。
新建一個spring boot項目,命名springcloud-session-redis
POM依賴配置
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.carry</groupId> <artifactId>springcloud-session-redis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-session-redis</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件
在application.yml中加入redis、eureka、port等配置,需要JAVA Spring Cloud大型企業(yè)分布式微服務(wù)
云構(gòu)建的B2B2C電子商務(wù)平臺源碼:三五三六二四七二五九.
server: port: 8090 spring: application: name: service-session-redis redis: host: 192.168.68.100 port: 6379 password: 123456 timeout: 6000ms lettuce: pool: max-active: 8 max-wait: -1ms max-idle: 8 min-idle: 0 database: 0 eureka: client: serviceUrl: defaultZone: http://admin:123456@localhost:8761/eureka/ management: endpoints: web: exposure: include: "*" cors: allowed-origins: "*" allowed-methods: "*"
Redis Session配置類
package com.carry.config; import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @Configuration @EnableRedisHttpSession public class RedisSessionConfig { }
控制層Controller中添加測試方法
package com.carry.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope public class UserManagementController { /** * redis sesion共享 * * @param request * @return */ @GetMapping("/getUser") public String getUser(HttpServletRequest request) { HttpSession session = request.getSession(); String username = (String) session.getAttribute("username"); if (StringUtils.isEmpty(username)) { username = "testSessionRedis|" + System.currentTimeMillis(); session.setAttribute("username", username); } System.out.println("訪問端口:" + request.getServerPort()); return username; } }
“springcloud怎么實現(xiàn)Session共享”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。