1、安装(windows)
需要前置安装Erlang语言
Mq下载安装:http://www.rabbitmq.com/download.html
设置set ERLANG_HOME=D:\erl-24.3.4
需要重新启动服务
启动管理工具:.\rabbitmq-plugins enable rabbitmq_management
增加用户:.\rabbitmqctl.bat add_user local local
增加权限:.\rabbitmqctl.bat set_permissions -p / local ".*" ".*" ".*"
设置管理员:.\rabbitmqctl.bat set_user_tags local administrator
查看用户列表:.\rabbitmqctl.bat list_users
修改用户密码:.\rabbitmqctl.bat change_password local 123456
删除用户:.\rabbitmqctl.bat delete_user local
2、管理
Overview:总览
Connections:连接
Channels:通道
Exchanges:交换机
Queues:队列
Admin:管理
Virtual Hosts:虚拟主机
3、交换机类型
Direct(路由模式):完全匹配>当消息的routing-key与exchange和queue间的binding-key完全匹配时,将消息分发到该queue。
Fanout(订阅模式):与binding-key和routing-key无关,将接受到的消息分发给有绑定关系的所有队列(不论binding-key和routing-key是什么)。
Topic(通配符模式):用消息的routing-key 与 exchange和queue间的binding-key 进行模式匹配,当满足规则时,分发到满足规则的所有队列。
4、简单运用
4.1、maven坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
4.2、生产者项目
- 配置文件
#对于rabbitMQ的支持
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=vHost_test
spring.rabbitmq.connection-timeout=10000
spring.rabbitmq.publisher-returns=true
#如果你们在配置确认回调,测试发现无法触发回调函数,那么存在原因也许是因为版本导致的配置项不起效,
#可以把publisher-confirms: true 替换为 publisher-confirm-type: correlated
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-confirm-type=correlated
- 连接
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory factory = new CachingConnectionFactory(host, port);
factory.setUsername(username);
factory.setPassword(password);
factory.setVirtualHost(vHost);
factory.setPublisherConfirmType(confirmType);
// factory.setPublisherConfirms(publisherConfirms);
factory.setPublisherReturns(publisherReturns);
factory.setConnectionTimeout(timeout);
return factory;
}
- RabbitTemplate注入对象
@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}
- 队列、交换机、路由(绑定)
@Bean
public Queue qa() {
return new Queue("queueA");
}
@Bean
public Queue qb() {
return new Queue("queueB");
}
@Bean
public Queue qc() {
return new Queue("queueC");
}
@Bean
public DirectExchange directExchange() {
return new DirectExchange("direct");
}
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanout");
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topic");
}
@Bean
public Binding directBindA() {
return BindingBuilder.bind(qa()).to(directExchange()).with("sysMsg");
}
@Bean
public Binding fanoutBindA() {
return BindingBuilder.bind(qa()).to(fanoutExchange());
}
@Bean
public Binding fanoutBindB() {
return BindingBuilder.bind(qb()).to(fanoutExchange());
}
@Bean
public Binding fanoutBindC() {
return BindingBuilder.bind(qc()).to(fanoutExchange());
}
@Bean
public Binding topicBindC() {
return BindingBuilder.bind(qc()).to(topicExchange()).with("#.notice.#");
}
- 发送消息
@Autowired
private RabbitTemplate rabbitTemplate;
@RequestMapping("/sendMsg")
public void sendMsg() {
rabbitTemplate.convertAndSend("交换机", "路由键,没有就null", "发送的json数据");
}
4.3、消费者项目
- 配置文件
#对于rabbitMQ的支持
spring:
rabbitmq:
addresses: 127.0.0.1:5672
username: guest
password: guest
virtual-host: vHost_test
listener:
simple:
acknowledge-mode: manual #手动确认消息
prefetch: 1 #每次处理一条消息
- 监听队列(一个队列可以有多个消费者,“能者多劳”模式)
@RabbitListener(queues = "queueA")
public void onMsg(String msg, Channel channel, Message message) throws Exception {
try {
Thread.sleep(1000);
System.out.println("消费者1消息处理成功:" + msg);
//确认当前(一条)消息消费成功
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
//当前消息重新入队
channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
}
}
@RabbitListener(queues = "queueA")
public void onMsg(String msg, Channel channel, Message message) throws Exception {
try {
Thread.sleep(5000);
System.out.println("消费者2消息处理成功:" + msg);
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
}
}

本文详细介绍了RabbitMQ在Windows上的安装步骤,包括环境变量设置、管理工具启用、用户管理等。同时,讲解了RabbitMQ的四种交换机类型及其工作模式。接着,通过Spring Boot应用展示了生产者和消费者的配置及消息发送、接收示例。最后,提供了消费者项目的配置和消息监听方法。
50万+

被折叠的 条评论
为什么被折叠?



