1 集成springboot
2 maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
3 配置文件
server:
port: 8801
spring:
application:
name: cloud-stream-provider
rabbitmq:
host: 192.168.1.100
port: 5672
password: admin
username: admin
virtual-host: my_vhost
publisher-returns: true
publisher-confirms: true
listener:
simple:
acknowledge-mode: manual
publisher-confirms true 代表 发送到交换机 确认信息
publisher-returns: true 代表 交换机发送到队列 确认信息
acknowledge-mode: manual 代表 接受者手工接收消息
4 发送代码
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void send3(){
User user = new User();
user.setName("name");
user.setPassword("123");
user.setAddress("aaaaa");
rabbitTemplate.setConfirmCallback((data, ack, cause) -> {
if (!ack) {
System.out.println("消息发送失败!" + cause + data.toString());
} else {
System.out.println("消息发送成功,消息ID:" + (data != null ? data.getId() : null));
}
});
rabbitTemplate.setReturnCallback((Message message, int replyCode, String replyText, String exchange, String routingKey)->{
log.info("returnedMessage ===> replyCode={} ,replyText={} ,exchange={} ,routingKey={}", replyCode, replyText, exchange, routingKey);
System.out.println("=============returnCallback触发。消息路由到queue失败===========");
System.out.println("msg="+new String(message.getBody()));
System.out.println("replyCode="+replyCode);
System.out.println("replyText="+replyText);
System.out.println("exchange="+exchange);
System.out.println("routingKey="+routingKey);
});
CorrelationData correlationData = new CorrelationData();
correlationData.setId(UUID.randomUUID().toString());
//rabbitTemplate.convertAndSend("exchange_2", "aaaaa.weather", user,correlationData);
rabbitTemplate.convertAndSend("exchange_2", "aaaaa", user,correlationData);
}
发送的两个确认代码
rabbitTemplate.setConfirmCallback
rabbitTemplate.setReturnCallback
5 接收代码
import java.io.IOException;
@Component
@RabbitListener(queues = "Queue3")
public class Consumer {
@RabbitHandler
public void listen(User user, Message message, Channel channel) throws IOException {
// 采用手动应答模式, 手动确认应答更为安全稳定
channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
System.out.println("Listener received: " + user);
}
}
6 配置config
package com.dkf.springcloud.config;
import org.springframework.amqp.core.*;
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;
@Configuration
public class RabbitConfig {
public static final String ONE_NAME_1="one_name_1";
public static final String TWO_NAME_2="two_name_2";
public static final String THREE="Queue3";
public static final String EXCHANGE_1="exchange_1";
public static final String EXCHANGE_2="exchange_2";
//所有队列必须持久化(Queue默认durable:true),防止mq服务重启后消息丢失
@Bean
public Queue oneQueue(){
/**
durable="true" 持久化消息队列 , rabbitmq重启的时候不需要创建新的队列
auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
exclusive 表示该消息队列是否只在当前connection生效,默认是false
*/
return new Queue(ONE_NAME_1,true,false,false);//等同于 return new Queue(ONE_NAME_1);
}
@Bean
public Queue twoQueue(){
return new Queue(TWO_NAME_2);
}
@Bean
public Queue threeQueue(){
return new Queue(THREE);
}
/**
* 1.定义direct exchange,绑定first_exchange
* 2.durable="true" 持久化交换机, rabbitmq重启的时候不需要创建新的交换机
* 3.direct交换器相对来说比较简单,匹配规则为:如果路由键匹配,消息就被投送到相关的队列
* fanout交换器中没有路由键的概念,他会把消息发送到所有绑定在此交换器上面的队列中。
* topic交换器你采用模糊匹配路由键的原则进行转发消息到队列中
*/
@Bean
public DirectExchange EXCHANGE_1(){
DirectExchange exchange=new DirectExchange(EXCHANGE_1,true,false);
return exchange;
}
@Bean
public TopicExchange EXCHANGE_2(){
TopicExchange exchange=new TopicExchange(EXCHANGE_2,true,false);
return exchange;
}
@Bean
public Binding bind(){
Binding binding = BindingBuilder.bind(oneQueue()).to(EXCHANGE_1()).with(ONE_NAME_1);
return binding;
}
@Bean
public Binding bind2(){
Binding binding = BindingBuilder.bind(threeQueue()).to(EXCHANGE_2()).with("#.weather");
return binding;
}
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
消息--> 交换机->绑定->队列
交换机 分为
- 点对点交换机
- 广播交换机
- 发布订阅交换机