作者:一一哥
我们按照之前的经验,我们这里可以创建3个web程序,一个用作消息生产者,一个作为消费者,还有一个作为存放通用代码类,并将之改造成Spring Boot项目,具体过程略。
整个项目目录结构如下:

这个项目中很简单,只有一个User类,并将该类实现序列化。
package com.yyg.boot.domain;
import lombok.Data;
import lombok.ToString;
import java.io.Serializable;
/**
* @Author 一一哥Sun
* @Date Created in 2020/4/14
* @Description Description
*/
@Data
@ToString
public class User implements Serializable {
private Long id;
private String username;
private String password;
}
org.springframework.boot
spring-boot-starter-amqp
com.yyg.boot
demo38_amqp_common
1.0-SNAPSHOT
spring:
application:
name: amqp-producer
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
package com.yyg.boot.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author 一一哥Sun
* @Date Created in 2020/4/16
* @Description Description
*/
@Configuration
public class RabbitMQConfig {
//交换机和队列要进行绑定:
//默认的交换机是DirectExchange,
//每个交换机都需要利用路由键来和队列绑定在一起.
//如果采用的是DirectExchange交换机,默认情况下,队里的名字就是路由键的名字.
//该交换机是一对一的,一个消息被发送者发送出去之后,只能被一个消费者接受.
@Bean
public Queue queue(){
//firstRabbit,是队列的名字,
return new Queue("firstRabbit");
}
@Bean
public Queue objectQueue(){
//firstRabbit,是队列的名字,
return new Queue("firstObject");
}
}
package com.yyg.boot.mq;
import com.yyg.boot.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @Author 一一哥Sun
* @Date Created in 2020/4/16
* @Description Description
*/
@Slf4j
@Component
public class RabbitProducer {
@Autowired
private AmqpTemplate template;
public void sendMsg() {
//Message msg=new Message();
//template.send(msg);
//第一个参数是路由键
String content="hello,"+new Date();
log.warn("消息发送--->content={}",content);
template.convertAndSend("firstRabbit", content);
}
//发送对象
public void sendUser() {
User user=new User();
user.setId(1L);
user.setUsername("syc");
user.setPassword("123");
log.warn("消息发送--->User={}",user.toString());
template.convertAndSend("firstObject", user);
}
}
package com.yyg.boot;
import com.yyg.boot.mq.RabbitProducer;
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;
/**
* @Author 一一哥Sun
* @Date Created in 2020/4/16
* @Description Description
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ProducerApplication.class)
public class FirstRabbitTest {
@Autowired
private RabbitProducer producer;
@Test
public void firstSendTest() {
for (int i = 0; i < 10; i++) {
producer.sendMsg();
}
}
}
package com.yyg.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author 一一哥Sun
* @Date Created in 2020/4/16
* @Description Description
*/
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args){
SpringApplication.run(ProducerApplication.class,args);
}
}

执行测试类中的firstSendTest()测试方法.看到控制台打印如下日志.

并且此时RabbitMQ中的firstRabbit队列中有10条待消费的消息.

spring:
application:
name: amqp-consumer
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
利用@RabbitListener和@RabbitHandler这两个注解,实现消息接收的监听。
package com.yyg.boot.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Author 一一哥Sun
* @Date Created in 2020/4/16
* @Description Description
*/
@Slf4j
@Component
public class RabbitConsumer {
@RabbitListener(queues = "firstRabbit")
@RabbitHandler
public void receiveMsg(String msg) {
log.warn("接受消息---->content:{}", msg);
}
}
package com.yyg.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author 一一哥Sun
* @Date Created in 2020/4/16
* @Description Description
*/
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args){
SpringApplication.run(ConsumerApplication.class,args);
}
}
此时可以看到,RabbitMQ控制台中firstRabbit队列中的消息已经被全部消费掉了,消息数量为0.

这里我们简单讲解了RabbitMQ的消息发送,还有更高级的功能,以后再讲解。