zoukankan      html  css  js  c++  java
  • sping+redis实现消息队列的乱码问题

    使用spring支持redis实现消息队列,参考官方样例:https://spring.io/guides/gs/messaging-redis/

    实现后在运行过程中发现消费者在接收消息时会出现乱码的情况。经排查是由于序列化工具不同导致的。

    生产者的配置

      <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">  
                <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>  
            </property>  
            <property name="valueSerializer">  
                <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>  
            </property>  
            <!--开启事务-->  
            <property name="enableTransactionSupport" value="true"/>  
        </bean> 

    template默认是使用stringSerializer的,这里配置了JdkSerializationRedisSerializer来做一些对象的存储。

    这就导致了消费端接受消息时反序列化会出现问题,解决方法可以在消费者配置监听器中注入生产者使用的序列化工具。

    注意Serializer属性。

    <bean id="listener" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
               <constructor-arg index="0" ref= "receiver"/> 
               <constructor-arg index="1" value="receive" /> 
               <property name="serializer" >
                <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>  
                </property>
       </bean>
  • 相关阅读:
    原生js实现分页功能
    webpack4 前端框架基础配置实例-解决css分离图片路径问题
    Vue cli2.0 项目中使用Monaco Editor编辑器
    Vue相关开源项目库汇总
    git常用配置
    Python 2 下载与安装
    sqlmap下载与安装
    IDEA中集成gitee插件
    双系统之删除Ubuntu系统
    IntelliJ IDEA安装注册教程
  • 原文地址:https://www.cnblogs.com/tangyuanyuan/p/8331243.html
Copyright © 2011-2022 走看看