1.引入pom.xml依赖
2.配置文件(spring-redis.xml)
3.spring-mvc.xml
4. web.xml 添加过滤器
5. springMVC控制器当中测试
6. 查看redis 当中是否存在数据
7. 查看Session一致性存入redis 当中数据内容
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>2.0.1.RELEASE</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <!--配置JedisPool连接池 用来连接Redis的工具--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--资源池允许的最大空闲连接数--> <property name="maxIdle" value="0"/> <!--资源池中的最大连接数--> <property name="maxTotal" value="20"/> <!--当资源池连接用尽后,调用者的最大等待时间(单位为毫秒)--> <property name="maxWaitMillis" value="1000"/> <!--向资源池借用连接时是否做连接有效性检测(ping)。检测到的无效连接将会被移除。--> <property name="testOnBorrow" value="true"/> </bean> <!--p:host-name :虚拟机的IP地址 p:port : redis的端口号 p:database = 0 ; 数据放入第一个数据中 redis数据中 (共16个)--> <!-- redis连接配置,依次为主机ip,端口,是否使用池,(usePool=true时)redis的池配置 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="192.168.182.130" p:port="6379" p:database="0" p:pool-config-ref="jedisPoolConfig"> </bean> <!-- 配置spring-session --> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <!-- 过期时间100分钟 --> <property name="maxInactiveIntervalInSeconds" value="6000"></property> </bean> <!--去掉redis client的CONFIG--> <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/> <!-- 声明操作redis CRUD的类 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <!-- 注入连接redis的bean --> <property name="connectionFactory" ref="connectionFactory"></property> <!-- key的序列化方式改为String --> <property name="keySerializer" ref="stringRedisSerializer"></property> <!-- value的序列化方式还是 json的方式 --> <property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"/> <!-- hash的key的序列化--> <property name="hashKeySerializer" ref="stringRedisSerializer"></property> <!-- hash的value的序列化--> <property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer"/> </bean> <!-- 默认的JDK 的序列化 --> <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"> </bean> <!-- String 的序列化 方式 --> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> <bean id="genericJackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"></bean> </beans>
引入第二步的文件 <import resource="spring-redis2.xml"></import>
<!--Session过滤器--> <filter> <filter-name>springSessionRepositoryFilter</filter-name> <!--aop拦截后由DelegatingFilterProxy处理session将数据放入redis中--> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
@RequestMapping(value = "list") public String list(HttpSession session){ session.setAttribute("aa","This is data"); return "list"; }
127.0.0.1:6379> keys * 1) "spring:session:sessions:expires:e3b2d107-66c5-4eea-9b53-8481aecd9307" 2) "spring:session:expirations:1640063820000" 3) "spring:session:sessions:e3b2d107-66c5-4eea-9b53-8481aecd9307"
127.0.0.1:6379> HKEYS "spring:session:sessions:e3b2d107-66c5-4eea-9b53-8481aecd9307" 1) "lastAccessedTime" 2) "sessionAttr:aa" 3) "maxInactiveInterval" 4) "creationTime" 127.0.0.1:6379> HGET "spring:session:sessions:e3b2d107-66c5-4eea-9b53-8481aecd9307" "sessionAttr:aa" "\xac\xed\x00\x05t\x00\x0cThis is data"
Thanks!!