RabbitMQ的服务端安装
我这里是在Linux的Docker容器中进行安装
1.拉取镜像
[root@iz2ze ~]# docker pull registry.docker-cn.com/library/rabbitmq:3.6-management
2.运行镜像
- 15672:web访问端口
- 5672:客户端访问端口
[root@iz2ze ~]# docker run -d --name myrabbitmq01 -p 5672:5672 -p 15672:15672 镜像ID
3.访问测试
服务器linux主机IP+15672端口。打开页面后需要登录默认的用户名和密码都是guest
在以上成功响应的网页中可以自定义创建Exchange(交换机),Queue(消息队列),将交换机和消息队列进行绑定(Binding)、发送消息,接收消息等操作。
SpringBoot与RabbitMQ的结合
1.引入启动场景
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
源码中重要片段
1.相关的自动配置类:RabbitAutoConfiguration
@Configuration
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
public class RabbitAutoConfiguration {
在自动配置类注入重要的Bean有:
1.rabbitTemplate: 给RabbitMQ发送和接收消息
@Bean
@ConditionalOnSingleCandidate(ConnectionFactory.class)
@ConditionalOnMissingBean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
2.amqpAdmin:RabbitMQ系统管理功能组件
@Bean
@ConditionalOnSingleCandidate(ConnectionFactory.class)
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "dynamic", matchIfMissing = true)
@ConditionalOnMissingBean
public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
2.配置信息类:RabbitProperties
RabbitProperties中属性
2.配置相关属性
如果要和在Linux创建的RabbitMQ容器进行通信连接,那么还需要再配置文件application.yml配置相关属性
spring:
rabbitmq:
host: 47.95.13.238
username: guest
password: guest
port: 5672
通过源码配置类RabbitProperties中可以看到这些属性都有相应的默认值。host指向本机,用户名和密码都是默认为guest,访问的默认端口为5672。
3.在主程序上开启基于注解的RabbitMQ
/**
* 主程序
* @ SpringBootApplication :说明这是一个SpringBoot应用
*
* 开启基于RabbitMq
* 1.RabbitTemplate : 给RabbitMQ发送和接收消息
* 2.AmqpAdmin:RabbitMQ系统管理功能组件
* 用到的注解:@EnableRabbit, @RabbitListener()
*/
@SpringBootApplication
@EnableRabbit //开启基础注解的RabbitMQ
public class SellApplication {
4.编写测试类
package com.mark.other;
import com.google.common.collect.Maps;
import com.mark.BaseTest;
import com.mark.dataobject.mapper.SellerMapper;
import com.mark.entity.SellerInfo;
import org.junit.Test;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
import java.util.Map;
/**
* Created by Choisaaaa on 2018/7/5.
*/
public class RabbitMqTest extends BaseTest {
//在源码中可以了解到容器中已经注入RabbitTemplate,AmqpAdmin组价。
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private SellerMapper sellerMapper;
@Autowired
private AmqpAdmin amqpAdmin;
//使用AmqpAdmin创建交换机
@Test
public void createExchange(){
Exchange exchange = new DirectExchange("amqpAdmin.exchange");
amqpAdmin.declareExchange(exchange);
System.out.println("创建一个direct 类型的交换机");
}
//使用AmqpAdmin创建队列
@Test
public void createQueue(){
Queue queue = new Queue("amqpAdmin.queue",true);
amqpAdmin.declareQueue(queue);
System.out.println("创建队列");
}
//使用AmqpAdmin将交换机和消息队列进行绑定
@Test
public void createBinding(){
Binding binding = new Binding("amqpAdmin.queue", Binding.DestinationType.QUEUE,"amqpAdmin.exchange","amqp.routKey",null);
amqpAdmin.declareBinding(binding);
}
//使用RabbitTemplate发送消息
@Test
public void testExchangeDirect(){
Map<String,Object> meassageMap = Maps.newConcurrentMap();
meassageMap.put("msg", "这是我的第一个消息");
meassageMap.put("data", Arrays.asList("11","22","33"));
rabbitTemplate.convertAndSend("exchange.direct","mark",meassageMap);
}
//使用RabbitTemplate接收消息
@Test
public void testReceived(){
//参数为消息队列的队列
Object object = rabbitTemplate.receiveAndConvert("mark");
System.out.println(object.getClass());
System.out.println(object);
}
@Test
public void testSend(){
//exchange.fanout 为自己定义的交换机的名字
SellerInfo sellerInfo = sellerMapper.getSellerById("1530597001113441157");
rabbitTemplate.convertAndSend("exchange.fanout","",sellerInfo);
}
}
使用注解 @RabbitListener 接收消息的demo
package com.mark.service.impl;
import com.mark.entity.SellerInfo;
import com.mark.service.IRabbitMqService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
/**
* Created by Choisaaaa on 2018/7/5.
*/
@Service
public class RabbitMqServiceImpl implements IRabbitMqService {
@Override
@RabbitListener(queues = "mark.news")
public void receiver(SellerInfo sellerInfo) {
System.out.println(sellerInfo);
}
@Override
@RabbitListener(queues = "mark")
public void receiver2(Message message) {
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
package com.mark.message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Created by Choisaaaa on 2018/7/12.
* 消息接收
*/
@Slf4j
@Component
public class MqReceiver {
@RabbitListener(queues = "mark")
public void process(String message){
log.info("MqReceiver:{}",message);
}
//自动创建队列
@RabbitListener(queuesToDeclare = @Queue("myQueue"))
public void process2(String message){
log.info("MqReceiver:{}",message);
}
//交换机好队列自动绑定
@RabbitListener(bindings = @QueueBinding(
value = @Queue("myQueue"),
key = "myRoutingKey",
exchange =@Exchange("myExchange")
))
public void process3(String message){
log.info("MqReceiver:{}",message);
}
}
补充:可以自定义消息装换器,例如将消息内容为对象转化为JSON格式
package com.mark.config;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by Choisaaaa on 2018/7/5.
* 自定义消息转换器
*/
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}