# 2.AMQP（RabbitMQ）

## 一、添加依赖

```markup
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
```

## 二、配置文件

```java
# RABBIT (RabbitProperties)
#spring.rabbitmq.host=localhost
#spring.rabbitmq.port=5672
#spring.rabbitmq.password=
#spring.rabbitmq.username=
```

## 三、代码实现

启用注解： `@EnableRabbit`

加到main函数之上

具体实现

![](/files/-LfnTIMKozdS6H3uf-T6)

util/configuration/AmqpConfiguration.java

```java
package com.shuju.test2.util.configuration;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * amqp 队列配置
 */
@Configuration
public class AmqpConfiguration {

    @Bean
    public Queue queue() {
        return new Queue("demodemo.queue"); //创业队列
    }
}
```

component/DemoAmqpComponent.java

```java
package com.shuju.test2.component;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

    @Component
    public class DemoAmqpComponent {

        @Autowired
        private AmqpTemplate amqpTemplate;

        public void send(String msg) {
            this.amqpTemplate.convertAndSend("demodemo.queue", msg);
        }

        @RabbitListener(queues = "demodemo.queue")
        public void receiveQueue(String text) {
            System.out.println("接受到：" + text);
        }

    }
```

测试类

```java
package com.shuju.test2;

import com.shuju.test2.component.DemoAmqpComponent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class Test2ApplicationTests {

    @Autowired
    private DemoAmqpComponent demoAmqpComponent;

    @Test
    public void send() {
        demoAmqpComponent.send("hello world2");
    }
}
```

运行测试

![](/files/-LfnTIMNqBwfaBfYjuJO)

## 资料

RabbitMQ下载地址：<http://www.rabbitmq.com/download.html>

erlang 下载地址：<http://www.erlang.org/downloads>

[RabbitMQ详解](https://blog.csdn.net/ityouknow/article/details/70153640)

### docker rabbitmq

[RabbitMQ 通过Docker方式快速安装](https://blog.csdn.net/antma/article/details/81334932)

[docker 安装rabbitMQ](https://www.cnblogs.com/yufeng218/p/9452621.html)

-management结尾的镜像是带管理界面的

```
sudo docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 -v `pwd`/data:/work/docker/data/rabbitmq -e RABBITMQ_DEFAULT_VHOST=my_vhost -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin  rabbitmq:3-management
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://spring-boot.shujuwajue.com/yi-bu-xiao-xi-fu-wu/2amqprabbitmq.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
