在SpringBoot中配置RabbitMQ

操作步骤

  1. 引入依赖
1
2
3
4
5
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>2.3.2.RELEASE</version>
</dependency>
  1. 准备配置文件
1
2
3
4
5
6
7
spring:
  rabbitmq:
    host: 192.168.30.174
    port: 5672
    username: admin
    password: 123456
    virtual-host: /
  1. 准备配置类
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package cn.watsons.mmp.brand.api.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitConfig {
    @Bean
    public Queue Queue() {
        return new Queue("hello");
    }
}
  1. 准备测试文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package cn.watsons.mmp.brand.api.rabbit;

import cn.watsons.mmp.brand.api.BrandMemberApiApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {BrandMemberApiApplication.class})
public class RabbitMQTest {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Test
    public void send() {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);

        this.rabbitTemplate.convertAndSend("hello", context);
    }
}

相关教程

  1. Spring Boot(八):RabbitMQ 详解