消息从生产者到达消费者(1)
消息从生产者到达消费者(2)
docker环境下安装
$docker pull rabbitmq
$docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq 479479d8e188
479479d8e188 代表的是镜像ID 可以用 docker images查询
登录客户端:192.168.2.223:15672
测试例子
springboot整合rabbitmq
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
自动配置
1.RabbitAutoConfigration
2.有自动配置的连接工厂ConnectionFactory
3.RabbitPropertiest 封装了RabbitMQ的配置
4.RabbitTemplate 给RabbitMQ发送和接受消息
5.AmqpAdmin:RabbitMQ的系统管理组件(例如创建或者删除队列、创建交换器等)
6.@RabbitListener(queues = "atguigu.news")//监听消息队列的内容+@EnableRabbit//开启基于注解的RabbitMQ
编写测试类
import com.pzh.myproject.domain.User;
import com.pzh.myproject.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class MyProjectApplicationTests {
@Autowired
private UserService userService;
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void contextLoads() {
User user = userService.findAllUser(3);
System.out.println(user);
}
/**
* 单播(点对点)
*
*/
@Test
void rabbitMQ(){
//Message需要自己构造一个;定制消息体内容和消息头
//rabbitTemplate.send(exchange,routeKey,message);
//只需要传入要发送的对象,自动序列化发送给rabbitmq
//rabbitTemplate.convertAndSend(exchange,routeKey,object);
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一个消息");
map.put("data", Arrays.asList("hello",123,true));
//对象被默认序列化发送出去
rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);
}
//接受数据,如何将数据自动的转为json发送出去
@Test
public void receive(){
Object o = rabbitTemplate.receiveAndConvert("atguigu.news");
System.out.println(o.getClass());
System.out.println(o);
}
}
编写序列化配置类
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 MyAMQPConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
配置监听器
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableRabbit//开启基于注解的RabbitMQ
@SpringBootApplication
@MapperScan("com.pzh.myproject.mapper")
public class MyProjectApplication {
public static void main(String[] args) {
SpringApplication.run(MyProjectApplication.class, args);
}
}
在Service层上面监听
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class BookServiceImpl {
@RabbitListener(queues = "atguigu.news")
public void receive(Map<String,Object> map){
System.out.println("收到消息:"+ map);
}
@RabbitListener(queues = "atguigu")
public void receive02(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
AmqpAdmin管理组件的使用
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange(){
amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
//创建绑定规则
amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));
System.out.println("创建完成");
}
amqpAdmin.declareBinding(new Binding("amqpadmin.queue", Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));
System.out.println("创建完成");
}