这里只会演示部分常用的工作模式
1.工作队列模式
1.1引入相关依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
1.2编写yml配置
spring:application:name: rabbitmq-spring-bootrabbitmq:addresses: amqp://pikes:123456@121.36.254.234:5672/pikes
1.3编写生产者代码
@RequestMapping("/producer")
@RestController
public class ProducerController {@Autowiredprivate RabbitTemplate rabbitTemplate;@RequestMapping("/work")public String work(){for(int i=0;i<10;i++){//使用内置交换机,RoutingKey和队列名称一致rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,"hello spring amqp: work..."+i);}return "发送成功";}
编写消费者代码
@Component
public class WorkListener {@RabbitListener(queues = Constants.WORK_QUEUE)public void queueListener1(Message message, Channel channel){System.out.println("listener 1 ["+ Constants.WORK_QUEUE+"] 接收到消息:" +message + ",channel:"+channel);}@RabbitListener(queues = Constants.WORK_QUEUE)public void queueListener2(Message message, Channel channel){System.out.println("listener 2 ["+ Constants.WORK_QUEUE+"] 接收到消息:" +message + ",channel:"+channel);}
}
@RabbitListener 是Spring框架中⽤于监听RabbitMQ队列的注解, 通过使⽤这个注解,可以定
义⼀个⽅法, 以便从RabbitMQ队列中接收消息. 该注解⽀持多种参数类型,这些参数类型代表了从
RabbitMQ接收到的消息和相关信息.
以下是⼀些常⽤的参数类型:
1. String :返回消息的内容
2. Message ( org.springframework.amqp.core.Message ): Spring AMQP的
Message 类,返回原始的消息体以及消息的属性, 如消息ID, 内容, 队列信息等.
⾼级的操作,如⼿动确认消息
2.发布订阅模式
2.1声明队列交换机
@Configuration
public class RabbitMQConfig {@Bean("workQueue")public Queue workQueue(){return QueueBuilder.durable(Constants.WORK_QUEUE).build();}//声明两个队列@Bean("fanoutQueue1")public Queue fanoutQueue1(){return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();}@Bean("fanoutQueue2")public Queue fanoutQueue2(){return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();}//声明交换机@Bean("fanoutExchange")public FanoutExchange fanoutExchange(){return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE).durable(true).build();}//队列和交换机绑定@Beanpublic Binding fanoutBinding1(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange,@Qualifier("fanoutQueue1") Queue queue){return BindingBuilder.bind(queue).to(fanoutExchange);}@Beanpublic Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange,@Qualifier("fanoutQueue2") Queue queue){return BindingBuilder.bind(queue).to(fanoutExchange);}
2.2生产者代码
@RequestMapping("/fanout")public String fanout(){//routingKey为空,表示所有队列都可以收到信息rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"","hello spring amqp:fanout...");return "发送成功";}@Request
2.3消费者代码
@Component
public class FanoutListener {@RabbitListener(queues = Constants.FANOUT_QUEUE1)public void queueListener1(String message){System.out.println("队列["+Constants.FANOUT_QUEUE1+"] 接收到消息:" +message);}@RabbitListener(queues = Constants.FANOUT_QUEUE2)public void queueListener2(String message){System.out.println("队列["+Constants.FANOUT_QUEUE2+"] 接收到消息:" +message);}
}
路由模式和通配符模式跟上面模式差不多 ,就不过多演示