安装Rabbitmq
本人是在Linux上安装的docker
启动docker,然后执行命令:docker pull rabbitmq:management 安装Rabbitmq容器
启动Rabbitmq:docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq 30e33de9be86 最后一个是id(使用docker ps -a查看)
输入端口+15672进入Management页面
创建工程
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置
application.properties
spring.rabbitmq.host=192.168.32.128
spring.rabbitmq.username=guest
spring.activemq.password=guest
spring.freemarker.view-names=/
配置类 序列化转json格式
@Configuration
public class RabbitCfg {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
Bean
public class Student implements Serializable {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
简单Test类
@SpringBootTest
class RabbitmqDemoApplicationTests {
@Test
void contextLoads() {
}
@Autowired
RabbitTemplate rabbitTemplate;
//点对点direct 需要具体 routingKey
@Test
public void ptp(){
rabbitTemplate.convertAndSend("qyc.direct","py","987654");
}
@Test
public void receive1(){
String message = (String) rabbitTemplate.receiveAndConvert("py");
System.out.println(message);
}
//广播fanout 不需要路由 发送转换器下全体queue
@Test
public void guangbo(){
rabbitTemplate.convertAndSend("qyc.fanout","","123456");
}
@Test
public void receive2(){
String s = (String) rabbitTemplate.receiveAndConvert("py");
String s1 = (String) rabbitTemplate.receiveAndConvert("py.news");
String s2 = (String) rabbitTemplate.receiveAndConvert("py.emps");
System.out.println(s+" "+s1+" "+s2);
}
//订阅 topic
@Test
public void dinyue(){
LocalDateTime localDateTime = LocalDateTime.now();
Student student = new Student("强月城",22 );
rabbitTemplate.convertAndSend("qyc.topic","py.*",student);
}
@Test
public void receive3(){
Student student = (Student) rabbitTemplate.receiveAndConvert("py.emps");
System.out.println(student);
}
}
注解
开启注解
@SpringBootApplication
@EnableRabbit
public class RabbitmqDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitmqDemoApplication.class, args);
}
}
Service
@Service
public class SericeDemo1 {
@RabbitListener(queues = "py.news")
public void getMessage(Student student){
System.out.println(student);
}
//获得消息
@RabbitListener(queues = "py.emps")
public void getMessage2(Message message){
System.out.println("py.emp Body:"+message.getBody());
System.out.println("py.emp Properties:"+message.getMessageProperties());
}
}
创建 Exchange queue Binding
//创建Exchange(转换器) queue(队) Binding(绑定)
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange(){
amqpAdmin.declareExchange(new DirectExchange("directExchange",true,false));
System.out.println("创建完成");
}
@Test
public void createFanout(){
amqpAdmin.declareExchange(new FanoutExchange("fanoutExchange",true,false));
System.out.println("创建完成");
}
@Test
public void createTopic(){
amqpAdmin.declareExchange(new TopicExchange("topicExchange",true,false));
System.out.println("创建完成");
}
//创建队列
@Test void createQueue(){
//drable 持久化 默认不自动删除
amqpAdmin.declareQueue(new Queue("py.emps.qyc",true));
}
//创建绑定 topic
@Test void createBinding(){
amqpAdmin.declareBinding(new Binding("py.news", Binding.DestinationType.QUEUE,
"topicExchange","py.*",null));
amqpAdmin.declareBinding(new Binding("py.emps", Binding.DestinationType.QUEUE,
"topicExchange","py.*",null));
amqpAdmin.declareBinding(new Binding("py.emps.qyc", Binding.DestinationType.QUEUE,
"topicExchange","py.emps.*",null));
amqpAdmin.declareBinding(new Binding("py.emps.qyc", Binding.DestinationType.QUEUE,
"topicExchange","py.#",null));
}
路由
个人理解:路由相当于一个链接,一对一模式和订阅模式需要路由,因为在发送消息的时候(参数:交换器,路由,消息),Exchange交换器要定位导具体的queue,
订阅模式支持 * #