1、下载镜像
#官网镜像地址 :
https://hub.docker.com/_/rabbitmq?tab=tags&page=1&ordering=last_updated
#下载镜像
docker pull rabbitmq:management
2、安装
#运行镜像
docker run --name myrabbit -d -p 5672:5672 -p 15672:15672 80070f1f0c8f
Q1:
重启系统
3、开发端口号
1-1、查看端口:firewall-cmd --list-all
1-2、开启端口:firewall-cmd --zone=public --add-port=15672/tcp --permanent
1-3、重启防火墙:systemctl restart firewalld
4、网页客户端
浏览器输入:http://192.168.2.170:15672/
账号:guest
密码:guest
4、网页客户端测试
#创建交换机
#创建队列
#交换机绑定队列
#发送消息
#接收消息
(以上所有操作 见下图)
5、整合至springboot
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 RbbitConfig {
/**
* 用于序列化格式
*/
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
@SpringBootTest
class SpringbootRabbitApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private AmqpAdmin amqpAdmin;
/**
* 发送消息
*/
@Test
void sendMessage() {
HashMap<Object, Object> msg = new HashMap<>();
msg.put("code","200");
msg.put("msg","你好啊");
rabbitTemplate.convertAndSend("smExchanges1","queue1",msg);
}
/**
* 接收消息
*/
@Test
void receviceMessage() {
Object queue1 = rabbitTemplate.receiveAndConvert("queue1");
System.out.println(queue1.getClass());
System.out.println(queue1);
}
/**
* 创建交换器
*/
@Test
void CreateExchanage(){
amqpAdmin.declareExchange(new DirectExchange("bootCreate"));
}
/**
* 创建队列
*/
@Test
void CreateQueue(){
amqpAdmin.declareQueue(new Queue("bootCreateQueue",true));
}
/**
* 交换器绑定队列
*/
@Test
void CreateBinding(){
amqpAdmin.declareBinding(new Binding("bootCreateQueue", Binding.DestinationType.QUEUE,"bootCreate","bootCreateQueue",null));
}