前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java整合Redis及序列化(上)

Java整合Redis及序列化(上)

作者头像
itze
发布2022-10-31 16:07:05
5140
发布2022-10-31 16:07:05
举报
文章被收录于专栏:IT者

场景

项目使用的是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依赖

代码语言:javascript
复制
        <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数据配置

代码语言:javascript
复制
#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配置文件

代码语言:javascript
复制
<?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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年5月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 场景
  • 问题
  • 解决办法
  • 完整流程
    • 添加Maven依赖
      • Redis配置文件
      相关产品与服务
      文件存储
      文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档