redis保存session信息的示例代碼
本文實現(xiàn)一個將session信息保存在 redis中,多個tomcat中的工程都從redis獲取session信息的示例。
1、新建一個maven web 工程名為 session-redis 如下:
目錄結構如下:
2、修改配置文件內容
pom.xml 中依賴如下:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <spring.version>5.0.4.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--spring 依賴包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!--spring 依賴包 結束--> <!-- Spring 升級4+ 依賴的JSON包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> <!-- /Spring 升級4+ 依賴的JSON包 --> <!--sfl4j+spring--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.25</version> </dependency> <!--sfl4j+spring--> <!--jedis依賴--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!--web包依賴--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <!--spirng實現(xiàn)redissession存儲依賴包--> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>2.0.4.RELEASE</version> </dependency> </dependencies>
web.xml 配置文件內容
<web-app> <display-name>Archetype Created Web Application</display-name> <!--配置spring session 過濾器 session數(shù)據的保存應該是通過過濾器來起作用,具體如何起作用日后研究--> <filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置springmvc --> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:mvc-demo.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
mvc-demo.xml 增加配置內容、
<!--spring注解配置--> <mvc:annotation-driven/> <context:component-scan base-package="com.test.session"/> <!--redis保存session信息配置類--> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="60"/><!--session 信息過期時間--> </bean> <!--redis 鏈接信息配置--> <bean id="standlongConfig" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration"> <property name="port" value="6379"/> <property name="hostName" value="127.0.0.1"/> <property name="database" value="0"/> </bean> <!--jedis 連接工廠配置--> <bean id="jedisConnectionFactory" class=" org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="standaloneConfig" ref="standlongConfig"/> </bean>
3、增加IndexController類,里邊兩個方法
index 方法返回字符串 index ,并且在session中添加屬性 name 為 zhangsan ,打印sessionid
getName 方法 返回session中的name屬性 .打印sessionid
如下:
@RestController public class IndexController { @GetMapping ("/index") public String index(HttpServletRequest request){ request.getSession().setAttribute("name","zhagnsan"); System.out.println("-----index------------sessionid"+request.getSession().getId()); return "index"; } @GetMapping ("/getName") public String getName(HttpServletRequest request){ System.out.println("----getName-------------sessionid======"+request.getSession().getId()); return (String)request.getSession().getAttribute("name"); } }
4、在本機啟動redis服務
5、idea中配置兩個tomcat 端口不能相同 我們這里 tomcat8.5-1 使用 80端口 tomcat8.5-1使用8080 端口 如下:
兩個tomcat 都部署session_redis 工程如下 ,訪問路徑為 /
6、啟動兩個tomcat 訪問 tomcat1 中的項目 http://localhost:80/index 在session中設置了name =zhangsan
后臺打印sessionid為:
直接訪問 tomcat2 中的 getName 方法 http://localhost:8080/getName 可以直接訪問到 session 并且后臺輸出sessionid和tomcat1中相同 。所以 session保存成功。
由 于我們在配置文件中設置 session過期時間為 60秒 所以60秒后再次訪問 getName方法,發(fā)現(xiàn)訪問不到內容了如下:
7、通過redis 客戶端 我們可以看到 隨著session信息的保存以及過期 redis中鍵值的變化 ,如下:
代碼下載地址: https://github.com/zhangxinmin/session-redis.git
到此這篇關于redis保存session信息的文章就介紹到這了,更多相關redis保存session信息內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot整合Redis入門之緩存數(shù)據的方法
Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基于內存亦可持久化的日志型、Key-Value數(shù)據庫,并提供多種語言的API,下面通過本文給大家介紹下SpringBoot整合Redis入門之緩存數(shù)據的相關知識,感興趣的朋友一起看看吧2021-11-11Redis中什么是Big?Key(大key)問題?如何解決Big?Key問題?
大key并不是指key的值很大,而是key對應的value很大,下面這篇文章主要給大家介紹了Redis中什么是Big?Key(大key)問題?如何解決Big?Key問題的相關資料,需要的朋友可以參考下2023-03-03