设为首页 收藏本站
查看: 754|回复: 0

[经验分享] rabbitmq使用心得

[复制链接]

尚未签到

发表于 2017-7-3 13:37:56 | 显示全部楼层 |阅读模式
  因为公司项目需要使用消息中间件,实现相关业务的异步处理,所有选用了rabbitmq.通过看文档,爬过一个一个坑,终于还是实现了相关功能。
  直接上配置文件:

<?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:rabbit="http://www.springframework.org/schema/rabbit"
       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/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="connectionFactory"  class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
        <property name="username" value="${mq.userName}" />
        <property name="password" value="${mq.password}" />
        <property name="host" value="${mq.host}" />
        <property name="port" value="${mq.port}" />
        <property name="virtualHost" value="${mq.virtualHost}"/>
        <property name="channelCacheSize" value="${mq.cache.size}"/>
    </bean>
   <bean id="rabbitAdmin"  class="org.springframework.amqp.rabbit.core.RabbitAdmin">
        <constructor-arg ref="connectionFactory" />
    </bean>
    <bean id="rabbitTemplate"  class="org.springframework.amqp.rabbit.core.RabbitTemplate">
        <constructor-arg ref="connectionFactory"></constructor-arg>
        <property name="exchange" value="${mq.exchange}"/>
        <property name="routingKey" value="${mq.routingKey}"/>
        <property name="queue" value="${mq.queue}"/>
    </bean>

    <bean id="serializerMessageConverter"  class="org.springframework.amqp.support.converter.SimpleMessageConverter"></bean>

    <bean id="queue"  class="org.springframework.amqp.core.Queue">
        <constructor-arg index="0" value="${mq.queue}"></constructor-arg>
        <constructor-arg index="1" value="true"></constructor-arg>
        <constructor-arg index="2" value="false"></constructor-arg>
        <constructor-arg index="3" value="false"></constructor-arg>
    </bean>

    <bean id="directExchange"  class="org.springframework.amqp.core.DirectExchange">
        <constructor-arg index="0" value="${mq.routingKey}"></constructor-arg>
        <constructor-arg index="1" value="true"></constructor-arg>
        <constructor-arg index="2" value="false"></constructor-arg>
    </bean>
    <util:map id="arguments">
    </util:map>

    <bean id="binding"  class="org.springframework.amqp.core.Binding">
        <constructor-arg index="0" value="${mq.queue}"></constructor-arg>
        <constructor-arg index="1" value="QUEUE"></constructor-arg>
        <constructor-arg index="2" value="${mq.exchange}"></constructor-arg>
        <constructor-arg index="3" value="${mq.routingKey}"></constructor-arg>
        <constructor-arg index="4" value="#{arguments}"></constructor-arg>
    </bean>

    <bean id="rmqConsumer"   class="com.tom.rabbitmq.MessageQueueReceiver"></bean>
    <bean id="messageListenerAdapter"  class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
        <constructor-arg ref="rmqConsumer" />
        <property name="defaultListenerMethod" value="onMessage"></property>
        <property name="messageConverter" ref="serializerMessageConverter"></property>
    </bean>
    <bean id="listenerContainer"  class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
        <property name="queues" ref="queue"></property>
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="messageListener" ref="messageListenerAdapter"></property>
    </bean>
</beans>

  在这个配置中,我使用的exchange模式是direct,生产者发送消息到 exchange,exchange根据其生产者的routingkey,找到对应bingkey的队列queue.这样消息就会存到对应的queue中,可以把queue理解为一个存储消息的地方。消费者会从该队列中获取消息,消费消息,返回消息应答,成功,该消息从消息队列中删除。  注意点:
  配置queue的时候,Durability设为durable,这样在rabbitmq服务端重启的时候,消息队列里面的消息不回丢失。
    配置文件中的如下,配置了在项目启动的时候,会自动去获得exchange,queue,在rabbitmq中注册。

<bean id="rabbitAdmin"  class="org.springframework.amqp.rabbit.core.RabbitAdmin">
     <constructor-arg ref="connectionFactory" />
</bean>

在配置中使用的spring的CachingConnectionFactory,用它来管理rabbit的connectionFactory, 其中可以设置其的channelCacheSize,默认是1.这个相当于建立一个channel的缓存池,
channel的作用类似于session.一个connection可以创建多个channel.
在该例子中使用SimpleMessageListenerContainer来管理消费者。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-390611-1-1.html 上篇帖子: python队列 rabbitmq介绍使用 下篇帖子: RabbitMQ-C 客户端接口使用说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表