RabbitMQ
优点:
1.应用解耦
2.异步提速
3.削峰填谷
缺点:
1.一致性
2.复杂性提高
3.可用性降低
特点:
1.可靠性
2.灵活的路由
3.高可用(集群)
4.多种协议
5.多语言客户端
6.管理界面
7.跟踪机制
rabbitmq启动
双击rabbitmq-server.bat启动rabbitmq
访问:localhost:15672
连接端口: 5672
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";
@Bean
public FanoutExchange exchange(){
return new FanoutExchange(EXCHANGE_NAME,true,false);
}
@Bean
public Queue queue(){
return new Queue(QUEUE_NAME,true,false,false);
}
@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";
@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";
@Bean
public TopicExchange exchange(){
return new TopicExchange(EXCHANGE_NAME,true,false);
}
@Bean
public Queue queue(){
return new Queue(QUEUE_NAME,true,false,false);
}
@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";
@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);
}
}
注解形式开发
producer
spring:
rabbitmq:
host: localhost
port: 5672
username: test
password: 123456
virtual-host: /test_rabbit
@RestController
public class ProducerController {
@Resource
private RabbitTemplate rabbitTemplate;
@RequestMapping("/send")
public String sendMessage(String message){
Map map = new HashMap();
map.put("message",message);
map.put("id","001");
map.put("type","test");
rabbitTemplate.convertAndSend("exchange_test","send_test", JSON.toJSONString(map));
return "success";
}
}
consumer
spring:
rabbitmq:
host: localhost
port: 5672
username: test
password: 123456
virtual-host: /test_rabbit
@Component
public class RabbitmqConsumer {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "queue_test"),
exchange = @Exchange(name = "exchange_test",type = ExchangeTypes.TOPIC),
key = "send_test"
))
public void consumer(Message message){
String s = new String(message.getBody());
Map map = JSON.parseObject(s, Map.class);
map.forEach((key,value) -> System.out.println(key+ ":"+map.get(key)));
}
}