skill:RabbitMq

如何使用

  • 安装RabbitMq服务

安装 Eralng OTP ,安装 Rabbit MQ Server,具体可以到网上搜教程。

  • spring 集成 RabbitMq

spring配置文件:

<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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd" >
    <description>rabbitmq 连接服务配置</description>
    <!-- 连接配置 -->
    <rabbit:connection-factory id="connectionFactory" host="${mq.host}" 
        username="${mq.username}" password="${mq.password}" port="${mq.port}"/> 
    <!--
        通过指定下面的admin信息,
        当前producer中的exchange和queue会在rabbitmq服务器上自动生成 
    -->
    <rabbit:admin connection-factory="connectionFactory"/>
    <!-- spring template声明-->
    <rabbit:template id="amqpTemplate" exchange="sms_exchange" 
        connection-factory="connectionFactory" message-converter="jsonMessageConverter" /> 
    <!-- 消息对象json转换类 -->
    <bean id="jsonMessageConverter" 
        class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /> 
    <!-- 申明一个消息队列Queue ; 
        durable:是否持久化 ; 
        exclusive: 仅创建者可以使用的私有队列,断开后自动删除; 
        auto_delete: 当所有消费客户端连接断开后,是否自动删除队列;
    -->
    <rabbit:queue name="sms_code_queue" durable="true" auto-delete="false" 
        exclusive="false" />
    <!-- 交换机定义 (
        rabbit:direct-exchange:
            定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。 
        rabbit:binding:设置消息queue匹配的key)
    -->
    <rabbit:direct-exchange  name="sms_exchange" durable="true" auto-delete="false" >
        <rabbit:bindings>
            <rabbit:binding queue="sms_code_queue" key="sms_code_queue_key"/>
        </rabbit:bindings>
    </rabbit:direct-exchange>
    <!-- 监听配置 (queues:监听的队列,多个的话用逗号(,)分隔ref:监听器) -->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
        <rabbit:listener queues="sms_code_queue" ref="smsCodeQueueListenter"/>
    </rabbit:listener-container>    
</beans>

生产者:

@Service
public class SmsMQProducerImpl implements MQProducer{
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Override
    public void sendDataToQueue(String key, Object object) {
        amqpTemplate.convertAndSend("sms_code_queue_key", object);
    }
}

消费者:

@Component
public class SmsCodeQueueListenter implements MessageListener {
    @Override
    public void onMessage(Message message) {}
}

简单解释

核心要素

生产者、消费者、队列、交换机

关系
生产者通过<rabbit:template/>将消息发送到指定交换机的指定队列
<rabbit:template id="amqpTemplate" exchange="sms_exchange" 
        connection-factory="connectionFactory" message-converter="jsonMessageConverter" />

通过RabbitTemplate.void convertAndSend(String routingKey, final Object object)中的routingKey参数来指定队列,队列的 routingKey在<rabbit:direct-exchange />中指定

<rabbit:direct-exchange />关联队列,并指定队列的routingKey:
<rabbit:direct-exchange  name="sms_exchange" durable="true" auto-delete="false" >
    <rabbit:bindings>
        <rabbit:binding queue="sms_code_queue" key="sms_code_queue_key"/>
    </rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:listener-container />配置队列的消费者
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
    <rabbit:listener queues="sms_code_queue" ref="smsCodeQueueListenter"/>
</rabbit:listener-container>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值