docker安装rabbitmq
docker run -di --name=rabbitmq -p 15672:15672 -p 5672:5672 \
-e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin rabbitmq
如果执行上面指令安装完成无法访问页面的话,进入容器执行如下命令即可
docker exec -it rabbitmq bash
#在容器里面执行如下命令,启用web管理插件
rabbitmq-plugins enable rabbitmq_management
#配置rabbitmq报500错误码问题
cd /etc/rabbitmq/conf.d/
#下面变量配置为false
echo management_agent.disable_metrics_collector = false > management_agent.disable_metrics_collector.conf
#退出容器
exit
#重启容器
docker restart rabbitmq
构建java环境,在pom文件里面导入如下依赖
<dependency>
<groupId>com.rabbitmqgroupId>
<artifactId>amqp-clientartifactId>
<version>5.16.0version>
dependency>
新建producer生产者
public class TestProducer {
public static void main(String[] args) throws Exception {
//获取连接
Connection connection = getConnection();
//创建通道
Channel channel = connection.createChannel();
while (true){
/**
* 参数详解
* 1-exchange 交换机名称 ""-代表默认交换机
* 2-routing-key 路由(队列名)
* 3-props 关联属性
* 4-body 消息体
*/
channel.basicPublish("","hello",null,"hello world!".getBytes());
System.out.println("消息发送成功");
Thread.sleep(1000);
}
}
public static Connection getConnection(){
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setVirtualHost("/test-1"); //v-host
connectionFactory.setUsername("admin");//用户名
connectionFactory.setPassword("admin");//密码
connectionFactory.setHost("192.168.252.129");//ip地址
connectionFactory.setPort(5672);//端口号
try{
return connectionFactory.newConnection();
}catch (IOException | TimeoutException e){
e.printStackTrace();
throw new RuntimeException("获取连接失败");
}
}
创建消费者consumer
public class TestConsumer {
public static void main(String[] args) throws Exception {
Connection connection = getConnection();
Channel channel = connection.createChannel();
//声明队列,防止生产者未启动导致队列不存在而报错
/**
* 1.queue 队列名称
* 2.durable 是否持久化
* 3.exclusive 是否独占连接,队列只允许在该连接中访问
* 4.autoDelete 自动删除
* 5.args 参数 队列允许设置一个自定义的扩展参数
*/
channel.queueDeclare("hello",true,false,false,null);
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收到的消息:"+new String(body));
}
};
/**
* 参数详解
* 1-queue 队列名称
* 2-autoAck 自动确认
* 3-callback 回调方法
*/
channel.basicConsume("hello",true, defaultConsumer);
//防止执行下面进行关闭退出
System.in.read();
channel.close();
connection.close();
}
public static Connection getConnection(){
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setVirtualHost("/test-1");
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setHost("192.168.252.129");
connectionFactory.setPort(5672);
try{
return connectionFactory.newConnection();
}catch (IOException | TimeoutException e){
e.printStackTrace();
throw new RuntimeException("获取连接失败");
}
}
}
pom文件导入如下依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.7.3version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
配置application.yml如下
server:
port: 9000
spring:
rabbitmq:
virtual-host: /test-1 #这个是自己登录rabbitmq后台新建的
port: 5672
host: 192.168.252.129
username: admin
password: admin
监听消费者
/**
* 使用用两个消费者监听同一个队列
*/
@Component
public class ListenConsumer {
@RabbitListener(queues = "hello")
@RabbitHandler
public void consumer(String msg, Channel channel, Message message){
System.out.println("1:"+msg);
}
@RabbitListener(queues = "hello")
@RabbitHandler
public void consumer2(String msg, Channel channel, Message message){
System.out.println("2:"+msg);
}
}
在控制器作为生产者
@RestController
public class RabbitmqController {
@Autowired
private RabbitTemplate rabbitTemplate;
@PostMapping("/sendMq")
public String sendMq(@RequestParam("message") String message){
rabbitTemplate.convertAndSend("hello",message);
return "send ok";
}
}