RabbitMQ集成SpringBoot
rabbit大致分为4大块,分别是producer,exchange,queue,consumer。
producer只需要把数据放入交换机中,其他不用管
exchange会把数据放入一个或者多个queue中,exchange在topic模式下,会根据route-key将对应数据放入对应queue中。
consumer会到queue中取出对应的数据。
优点:
1.应用解耦
2.异步提速
3.削峰填谷
缺点:
1.一致性
2.复杂性提高
3.可用性降低
特点:
1.可靠性
2.灵活的路由
3.高可用(集群)
4.多种协议
5.多语言客户端
6.管理界面
7.跟踪机制
SpringBoot集成RabbitMQ
依赖
<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>
fanout模式(先在rabbitmq中新建一个virtual-host)
consumer
server:
port: 8081
spring:
rabbitmq:
host: localhost
port: 5672
username: test
password: 123456
virtual-host: /test_rabbit
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyRabbitConfig {
private static String EXCHANGE_NAME = "my_fanout";
private static String QUEUE_NAME = "my_boot_fanout_queue";
/**
* 注册一个广播形式的交换机
* @return
*/
@Bean
public FanoutExchange exchange(){
return new FanoutExchange(EXCHANGE_NAME,true,false);
}
/**
* 注册一个队列
* @return
*/
@Bean
public Queue queue(){
return new Queue(QUEUE_NAME,true,false,false);
}
/**
* 将交换机和队列绑定起来
* @param queue
* @param fanoutExchange
* @return
*/
@Bean
public Binding queueBinding(Queue queue,FanoutExchange fanoutExchange){
return BindingBuilder.bind(queue).to(fanoutExchange);
}
}
@Component
public class MyConsumer {
@RabbitListener(queues = "my_boot_fanout_queue")
public void process(Message message){
byte[] body = message.getBody();
System.out.println("接收到的消息:"+new String(body));
}
}
producer(生产者端只需要注册一个交换机即可)
server:
port: 8082
spring:
rabbitmq:
host: localhost
port: 5672
username: test
password: 123456
virtual-host: /test_rabbit
@Configuration
public class MyRabbitConfig {
private static String EXCHANGE_NAME = "my_fanout";
/**
* fanout 广播形式
* @return
*/
@Bean
public FanoutExchange exchange(){
return new FanoutExchange(EXCHANGE_NAME,true,false);
}
}
test类
@Resource
private RabbitTemplate rabbitTemplate;
@Test
public void sendMessage(){
String message = "hello world rabbit";
rabbitTemplate.convertAndSend("amq.fanout","",message);
}
topic模式
consumer
server:
port: 8081
spring:
rabbitmq:
host: localhost
port: 5672
username: test
password: 123456
virtual-host: /test_rabbit
@Configuration
public class MyRabbitConfig {
private static String EXCHANGE_NAME = "my_boot-topic_exchange";
private static String QUEUE_NAME = "my_boot_topic_queue";
/**
* 注册一个广播形式的交换机
* @return
*/
@Bean
public TopicExchange exchange(){
return new TopicExchange(EXCHANGE_NAME,true,false);
}
/**
* 注册一个队列
* @return
*/
@Bean
public Queue queue(){
return new Queue(QUEUE_NAME,true,false,false);
}
/**
* 将交换机和队列绑定起来
* @param queue
* @param fanoutExchange
* @return
*/
@Bean
public Binding queueBinding(Queue queue,FanoutExchange fanoutExchange){
return BindingBuilder.bind(queue).to(fanoutExchange).with("product.*");
}
}
@Component
public class MyConsumer {
@RabbitListener(queues = "my_boot_topic_queue")
public void process(Message message){
byte[] body = message.getBody();
System.out.println("接收到的消息:"+new String(body));
}
}
producer
server:
port: 8082
spring:
rabbitmq:
host: localhost
port: 5672
username: test
password: 123456
virtual-host: /test_rabbit
@Configuration
public class MyRabbitConfig {
private static String EXCHANGE_NAME = "my_boot_topic_exchange";
/**
* fanout 广播形式
* @return
*/
@Bean
public TopicExchange exchange(){
return new TopicExchange(EXCHANGE_NAME,true,false);
}
}
test类
@Resource
private RabbitTemplate rabbitTemplate;
@Test
public void sendMessage(){
String message = "hello world rabbit";
rabbitTemplate.convertAndSend("amq.fanout","product.test",message);
}
ack的使用
server:
port: 8083
spring:
rabbitmq:
host: localhost
port: 5672
username: test
password: 123456
virtual-host: /test_rabbit
# 修改成手动ack
listener:
simple:
acknowledge-mode: manual
// 消息消费方
@Component
public class MyConsumer {
@RabbitListener(queues = "my_boot_topic_queue")
public void process(Message message, Channel channel) throws IOException {
System.out.println(new String(message.getBody()));
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
}
> 这是笔者第一次写csdn文章,如有不足之处,还请各位大佬斧正。如果大家有什么疑惑也可以在评论区留言,互相交流一下。