场景
项目使用的是SSM框架,在引入Redis之后需要对Redis中数据类型结构类型进行序列化,如果不进行序列化,那么只能存储String类型,如果存储其他类型将会出现:can’t cast to String 错误, Spring-data-Redis中提供的有StringRedisSerializer、JdkSerializationRedisSerializer序列化方式 - StringRedisSerializer:一般是对key进行序列化 - JdkSerializationRedisSerializer:一般是对value进行序列化
问题
经过测试使用在SSM框架中引入Redis,以xml配置文件的方式对Redis数据类型结构进行序列化,如果key、value都是以StringRedisSerializer这种方式序列化,那么value也只能是String类型,如果将value改为JdkSerializationRedisSerializer序列化,不影响正常使用get/set,但是如果在Redis可视化窗口工具里、如RDM(Redis Desktop Manager)、Another Redis Desktop Manager 可视化工具中查看会出现乱码问题
解决办法
key值使用阿里的FastJson进行序列化
完整流程
整合Redis完整流程
添加Maven依赖
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.3.0.RELEASE</version> </dependency> <!-- redis 依赖 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.4.2</version> </dependency> <!-- 阿里巴巴 Fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</version> </dependency>
|
注意:这两个Jar包的版本号如果使用过高可能会出现一些问题
Redis配置文件
redis.properties数据配置
#Redis redis.addr = 127.0.0.1 redis.port = 12345 redis.auth = 123456 #最大空闲数(默认:8) redis.maxIdle=300 #当连接池资源耗尽时,调用者最大阻塞时间,超时将抛出异常.单位:毫秒,默认:-1,表示永不超时. redis.maxWait=1000 #最大连接数(默认:8) redis.maxTotal=500 #指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 (默认:false) redis.testOnBorrow=true redis.testOnReturn=true redis.testWhileIdle=true redis.blockWhenExhausted=false redis.numTestsPerEvictionRun=1024 redis.timeBetweenEvictionRunsMillis=30000 redis.minEvictableIdleTimeMillis=1800000 redis.maxActive = 1024 redis.timeOut = 10000
|
application-redis.xml配置文件
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描redis配置文件--> <context:property-placeholder ignore-unresolvable="true" location="classpath:properties/redis.properties"/> <!--设置连接池--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大空闲连接数 --> <property name="maxIdle" value="${redis.maxIdle}"/> <!-- 最大连接数 --> <property name="maxTotal" value="${redis.maxTotal}"/> <!-- 每次释放连接的最大数目 --> <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/> <!-- 释放连接的扫描间隔(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/> <!-- 连接最小空闲时间 --> <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/> <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --> <property name="maxWaitMillis" value="${redis.maxWait}"/> <!-- 在获取连接的时候检查有效性, 默认false --> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> <property name="testOnReturn" value="${redis.testOnReturn}"/> <!-- 在空闲时检查有效性, 默认false --> <property name="testWhileIdle" value="${redis.testWhileIdle}"/> <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --> <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/> </bean> <!--Spring整合Jedis,设置连接属性--> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.addr}" p:port="${redis.port}" p:password="${redis.auth}" p:pool-config-ref="poolConfig" p:timeout="100000"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory"/>
<!-- !!!这段代码可以删除 开始!!!--> <!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!--> <property name="keySerializer"> <!--对key的默认序列化器。默认值是StringSerializer--> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <!--是对value的默认序列化器,默认值是取自DefaultSerializer的JdkSerializationRedisSerializer。--> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> <!--存储Map时key需要的序列化配置--> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <!--存储Map时value需要的序列化配置--> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> <!-- !!!这段代码可以删除 结束!!!-->
</bean> <!--配置redis工具类bean--> <bean id="redisUtils" class="com.beyondtech.common.redis.RedisUtils"></bean> <!-- 配置redisTemp序列化类bean --> <bean id="redisConfig" class="com.beyondtech.common.configuration.redisConfig"></bean> </beans>
|