上一篇用最原始的的方法写了一下rabbitmq的direct模式
这一篇 用spring的方式将rabbitmq 的三种模式都讲一下
首先是maven依赖
<!--rabbitmq和spring的依赖整合--> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.4.0.RELEASE</version> </dependency> <!-- rabbitmq相关依赖 --> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>4.1.0</version> </dependency> <!-- 序列化相关依赖 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency>rabbitmq 的配置 rabbitMQ.properties
mq.host=127.0.0.1 mq.username=guest mq.password=guest mq.port=5672applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <context:property-placeholder location="classpath:rabbitMQ.properties"/> <!--连接配置--> <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"/> <!--direct开始--> <!--spring template模板声明--> <rabbit:template exchange="test-mq-exchange" id="amqpTemplate" connection-factory="connectionFactory"/> <!--声明一个队列--> <!-- durable:是否持久化 exclusive: 仅创建者可以使用的私有队列,断开后自动删除 auto_delete: 当所有消费客户端连接断开后,是否自动删除队列 --> <rabbit:queue id="test_queue" name="test_queue" durable="true" auto-delete="false" exclusive="false"/> <!--声明一个Exchange--> <!-- rabbit:direct-exchange:定义exchange模式为direct,意思就是消息与一个特定的路由键完全匹配,才会转发。 rabbit:binding:设置消息queue匹配的key --> <rabbit:direct-exchange name="test-mq-exchange" durable="true" auto-delete="false" id="test-mq-exchange"> <rabbit:bindings> <rabbit:binding queue="test_queue" key="test_queue"/> </rabbit:bindings> </rabbit:direct-exchange> <!--direct模式的监听者--> <bean id="queueListenter" class="com.yjp.rabbitmq.Test1.QueueListenter"/> <!--topic模式的监听者--> <bean id="topicListenter1" class="com.yjp.rabbitmq.topic.TopicListenter1"/> <bean id="topicListenter2" class="com.yjp.rabbitmq.topic.TopicListenter2"/> <!--fanout模式的监听者--> <bean id="fanoutListenter1" class="com.yjp.rabbitmq.fanout.FanoutListenter1"/> <bean id="fanoutListenter2" class="com.yjp.rabbitmq.fanout.FanoutListenter2"/> <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto"> <!--direct模式的监听者--> <rabbit:listener queues="test_queue" ref="queueListenter"/> <!--topic的监听者--> <rabbit:listener ref="topicListenter1" queue-names="test123"/> <rabbit:listener ref="topicListenter2" queue-names="test1234"/> <!--fanout模式的监听者--> <rabbit:listener ref="fanoutListenter1" queue-names="fanout-test-1"/> <rabbit:listener ref="fanoutListenter2" queue-names="fanout-test-2"/> </rabbit:listener-container> <context:component-scan base-package="com.yjp.rabbitmq"/> <!--direct结束--> <!--rabbitmq的topic模式--> <!--队列--> <rabbit:queue id="test123" name="test123" durable="true" auto-delete="false" exclusive="false"/> <rabbit:queue id="test1234" name="test1234" durable="true" auto-delete="false" exclusive="false"/> <!--将队列与交换机绑定 然后设置队列的routingkey--> <rabbit:topic-exchange name="test-topic-exchange" durable="true" auto-delete="false" id="test-topic-exchange"> <rabbit:bindings> <!--*.*.test123 *代表着匹配任意的一个单词 路由规则--> <rabbit:binding pattern="*.*.test123" queue="test123"/> <!--test1234.# #代表着匹配任意单词 只要是test1234开头的 路由规则--> <rabbit:binding pattern="test1234.#" queue="test1234"/> </rabbit:bindings> </rabbit:topic-exchange> <!-- 创建rabbitTemplate 消息模板类 --> <bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="connectionFactory"/> </bean> <!--rabbitmq的topic模式 结束--> <!--rabbitmq 的fanout模式--> <rabbit:queue name="fanout-test-1" durable="true"/> <rabbit:queue name="fanout-test-2" durable="true"/> <!--使用fanout模式的时候没有routingkey--> <rabbit:fanout-exchange name="test-fanout-exchange" durable="true" auto-delete="false" id="test-fanout-exchange"> <rabbit:bindings> <rabbit:binding queue="fanout-test-1"/> <rabbit:binding queue="fanout-test-2"/> </rabbit:bindings> </rabbit:fanout-exchange> <!--rabbitmq 的fanout模式结束--> </beans>direct的生产者:
test类:
@Autowired private MQProducer mqProducer; private static final String QUEUE_KEY = "test_queue"; @Test public void test() { String message = "hello rabbitMQ!"; mqProducer.sendDataToQueue(QUEUE_KEY, message); }接口:
package com.yjp.rabbitmq.Test1; public interface MQProducer { /** * 发送消息到指定队列 * * @param queueKey * @param object */ void sendDataToQueue(String queueKey, Object object); }实现类:
package com.yjp.rabbitmq.Test1; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * 发送消息 * * @Author : WenBao * Date : 16:24 2017/12/12 */ @Service public class MQProducerImpl implements MQProducer { @Autowired private AmqpTemplate amqpTemplate; @Override public void sendDataToQueue(String queueKey, Object object) { amqpTemplate.convertAndSend(queueKey, object); } }消费者:
package com.yjp.rabbitmq.Test1; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; /** * 队列监听 * * @Author : WenBao * Date : 16:24 2017/12/12 */ public class QueueListenter implements MessageListener { @Override public void onMessage(Message message) { String str = ""; try { str = new String(message.getBody(), "UTF-8"); System.out.println("=============监听【QueueListenter】消息" + message); System.out.print("=====获取消息" + str); } catch (Exception e) { } } }topic模式:
生产者:
@Test public void testRabbitMq() throws Exception { ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); RabbitTemplate rabbitTemplate = (RabbitTemplate) ac.getBean("rabbitTemplate"); //第二个参数为路由key(routingKey)的值, for (int i = 1; i <= 10; i++) { String str = "hello" + i; //test1234.hello.test123 在配置文件中队列是设定路由规则了的 当我们向exchange中发送消息打的时候 exchange会根据路由规则向队列中发送消息 rabbitTemplate.send("test-topic-exchange", "test1234.hello.test123", new Message(str.getBytes(), new MessageProperties())); } }
消费者 1:
package com.yjp.rabbitmq.topic; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class TopicListenter1 implements MessageListener { @Override public void onMessage(Message message) { String str = ""; try { str = new String(message.getBody(), "UTF-8"); System.out.println("=============监听【TopicListenter1】消息" + message); System.out.print("=====获取消息" + str); } catch (Exception e) { } } }消费者 2:
package com.yjp.rabbitmq.topic; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class TopicListenter2 implements MessageListener { @Override public void onMessage(Message message) { String str = ""; try { str = new String(message.getBody(), "UTF-8"); System.out.println("=============监听【TopicListenter2】消息" + message); System.out.print("=====获取消息" + str); } catch (Exception e) { } } }fanout 模式:
生产者:
@Test public void testRabbitMqFanout() throws Exception { ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); RabbitTemplate rabbitTemplate = (RabbitTemplate) ac.getBean("rabbitTemplate"); //往名字为leo.pay.fanout.exchange的路由里面发送数据,客户端中只要是与该路由绑定在一起的队列都会收到相关消息,这类似全频广播,发送端不管队列是谁,都由客户端自己去绑定,谁需要数据谁去绑定自己的处理队列。 for (int i = 1; i <= 10; i++) { String str = "hello" + i; rabbitTemplate.send("test-fanout-exchange", "", new Message(str.getBytes(), new MessageProperties())); } }消费者 1:
package com.yjp.rabbitmq.fanout; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class FanoutListenter1 implements MessageListener { @Override public void onMessage(Message message) { String str = ""; try { str = new String(message.getBody(), "UTF-8"); System.out.println("=============监听【fanoutListenter1】消息" + message); System.out.print("=====获取消息" + str); } catch (Exception e) { } } }消费者 2:
package com.yjp.rabbitmq.fanout; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class FanoutListenter2 implements MessageListener { @Override public void onMessage(Message message) { String str = ""; try { str = new String(message.getBody(), "UTF-8"); System.out.println("=============监听【fanoutListenter2】消息" + message); System.out.print("=====获取消息" + str); } catch (Exception e) { } } }
所有生产者的测试类:
package com.yjp.rabbitmq.Test2; import com.yjp.rabbitmq.Test1.MQProducer; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class TestQueue { @Autowired private MQProducer mqProducer; private static final String QUEUE_KEY = "test_queue"; @Test public void test() { String message = "hello rabbitMQ!"; mqProducer.sendDataToQueue(QUEUE_KEY, message); } @Test public void testRabbitMq() throws Exception { ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); RabbitTemplate rabbitTemplate = (RabbitTemplate) ac.getBean("rabbitTemplate"); //第二个参数为路由key(routingKey)的值, for (int i = 1; i <= 10; i++) { String str = "hello" + i; //test1234.hello.test123 在配置文件中队列是设定路由规则了的 当我们向exchange中发送消息打的时候 exchange会根据路由规则向队列中发送消息 rabbitTemplate.send("test-topic-exchange", "test1234.hello.test123", new Message(str.getBytes(), new MessageProperties())); } } @Test public void testRabbitMqFanout() throws Exception { ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); RabbitTemplate rabbitTemplate = (RabbitTemplate) ac.getBean("rabbitTemplate"); //往名字为leo.pay.fanout.exchange的路由里面发送数据,客户端中只要是与该路由绑定在一起的队列都会收到相关消息,这类似全频广播,发送端不管队列是谁,都由客户端自己去绑定,谁需要数据谁去绑定自己的处理队列。 for (int i = 1; i <= 10; i++) { String str = "hello" + i; rabbitTemplate.send("test-fanout-exchange", "", new Message(str.getBytes(), new MessageProperties())); } } }demo到此结束,这里讲三种模式都写出来,下一篇会用springboot去整合rabbitmq 依旧是代码 然后再讲讲 这三种模式的区别 以及rabbitmq和activemq的区别 最后应该还会写一些关于kafka队列的博客,将一些常用的队列都写一下,之后一段时间可能大多的博客会更新关于storm方面的,所以这段时间将之前所学的都以博客的形式记录下来,一些nginx,redis,有时间的话会更新一些关于springcloud方面的博客,大多都是实践,如何去做,源码方面的话会慢慢的更新一些,不过这个要看时间
努力吧 皮卡丘