docker run -d --name rabbitmq-container -e RABBITMQ_DEFAULT_USER=peijiakai -e RABBITMQ_DEFAULT_PASS=peijiakai -p 5672:5672 -p 15672:15672 -v /path/on/host:/var/lib/rabbitmq rabbitmq:management
流程
=========================================================================
springAmqp
介绍: amqp是网路协议
springamqp是基于amqp协议定义的一套api规范,并且由rabbitmq官方对这一规范进行实现.
功能:有三个如下
1 自动声明队列、交换机及其绑定关系
2 基于注解的监听器模式,异步接收消息
3 封装了RabbitTemplate工具,用于发送消息
===================================================
具体使用方式
Basic Queue 简单队列模型
1.父工程引入依赖
org.springframework.boot
spring-boot-starter-amqp
2.aplication.yml配置
spring:
rabbitmq:
host: 192.168.199.130 # 主机名
port: 5672 # 端口
virtual-host: / # 虚拟主机
username: itcast # 用户名
password: 123321 # 密码
3进行消息发送和接收
3.1消息发送的测试类
public void testWorkQueue() throws InterruptedException {
// 队列名称
String queueName = "simple.queue";
// 消息
String message = "hello, message_";
rabbitTemplate.convertAndSend(queueName,message);
System.out.println("ok");
}
3.2 消息接收
@Component
public class SimpleQueueListener {
@RabbitListener(queues = "simple.queue")
public void listenWorkQueue1(String message){
System.out.println(message);
}
}
---------------------------------------------------------------------------------------------------------------------
1新建模块
2 conf类声明交换机和队列并进行绑定@Qualifier按照名称注入
@Configuration
public class FanoutConfig {
//建立交换机和队列的绑定关系
@Bean
public Exchange exchange(){
return ExchangeBuilder.fanoutExchange("fanout.exchange").build();
}
@Bean
public Queue queue1(){
return QueueBuilder.durable("fanout.queue1").build();
}
@Bean
public Queue queue2(){
return QueueBuilder.durable("fanout.queue2").build();
}
@Bean
public Binding exchangeBindingQueue1
(@Qualifier("fanout.exchange") Exchange exchange,@Qualifier("fanout.queue1") Queue queue){
return BindingBuilder.bind(queue).to(exchange).with("").noargs();
}
@Bean
public Binding exchangeBindingQueue2
(@Qualifier("fanout.exchange") Exchange exchange,@Qualifier("fanout.queue2") Queue queue){
return BindingBuilder.bind(queue).to(exchange).with("").noargs();
}
}
3 发消息
--------------------------------------------------------------------------------------------------------
路由模式
----------------------------------------------------------------------------------------------
通配符
===================================================================
消息转化器
springamqp的消息转化器操作;
1. 引入依赖
com.fasterxml.jackson.core
jackson-databind
2然后再生产者微服务和消费者微服务声明bean
@Bean
public MessageConverter js(){
return new Jackson2JsonMessageConverter();
}