Springboot整合RabbitMQ
一:Springboot整合RabbitMQ
新建一个Module工程,工程命名为springboot-rabbitmq
在pom.xml
中引入如下依赖内容,其中spring-boot-starter-amqp
用于支持RabbitMQ。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
在application.xml中配置如下配置:
spring:
application:
name: rabbitmq-hello
rabbitmq:
port: 5672
username: guest
password: guest
host: 192.168.10.152
创建消息生产者Sender,
通过注入RabbitTemplate接口的实例来实现消息的发送,RabbitTemplate接口定义了一套针对AMQP协议的基础操作。在Spring Boot中会根据配置来注入其具体实现。在该生产者,我们会产生一个字符串,并发送到名为hello
的队列中。
package com.github.springbootrabbitmq.provider;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class Sender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(){
String context = "hello" + new Date();
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("hello",context);
}
}
创建消费者Receiver。通过@RabbitListener
注解定义该类对hello
队列的监听,并用@RabbitHandler
注解来指定对消息的处理方法。所以,该消费者实现了对hello
队列的消费,消费操作为输出消息的字符串内容。
package com.github.springbootrabbitmq.consumer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "hello")
public class Receiver {
@RabbitHandler
public void process(String hello){
System.out.println("Receiver : " + hello);
}
}
创建RabbitMQ的配置类RabbitConfig
,用来配置队列、交换器、路由等高级信息。这里我们以入门为主,先以最小化的配置来定义,以完成一个基本的生产和消费过程。
package com.github.springbootrabbitmq.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue(){
return new Queue("hello");
}
}
创建应用主类:
package com.github.springbootrabbitmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
}
}
创建单元测试类,用来调用消息生产:
package com.github.springbootrabbitmq;
import com.github.springbootrabbitmq.provider.Sender;
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 SpringbootRabbitmqApplicationTests {
@Test
public void contextLoads() {
}
@Autowired
private Sender sender;
@Test
public void hello(){
sender.send();
}
}
运行结果如下:
网站的控制面板如下: