首先我们需要在配置文件中添加相应的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
在配置文件中加入rabbitmq 相关连接参数
server:
port: 9999
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
创建队列,交换机,并且进行绑定
package com.example.rabbitdemo.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
/**
*
* @Description:创建一个队列
*/
@Bean
public Queue queueForSingleMode() {
return new Queue("quene.testquene");
}
/**
*
* @Description:创建一个主题交换机
*/
@Bean
public TopicExchange createTopicExchange() {
return new TopicExchange("opic.testexchange");
}
/**
*
* @Description:把队列与交换机进行绑定,并且routingKey值为topictest。这个没有使用通配符相当于是一个直连交换机
*
*/
@Bean
public Binding binding() {
return BindingBuilder.bind(queueForSingleMode()).to(createTopicExchange()).with("topictest");
}
/**
* 使用 JSON 序列化化消息
* @param connectionFactory
* @return
*/
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//数据转换为json存入消息队列
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
}
创建生产者,用于发送信息
package com.example.rabbitdemo.service;
import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RabbitProduce {
@Autowired
private RabbitTemplate rabbitTemplate;
public String sendMsg() {
Map<String, Object> msg = new HashMap<>(8);
msg.put("msg", "这是一条测试信息");
msg.put("type", "json");
msg.put("version", 123);
//交换机的名称及routingKey与config中保持一致
rabbitTemplate.convertAndSend("opic.testexchange", "topictest", msg);
return "";
}
}
创建消费者,用于接收信息
package com.example.rabbitdemo.service;
import java.io.IOException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
public class RabbitReceiver {
@RabbitListener(queues = "quene.testquene")
public void consume(Message message, Channel channel) throws IOException {
log.info("DirectConsumeListener,收到消息: {}", new String(message.getBody()));
}
}
建立测试类运行
package com.example.rabbitdemo;
import org.junit.Before;
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.SpringJUnit4ClassRunner;
import com.example.rabbitdemo.service.RabbitProduce;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = RabbitdemoApplication.class)
public class ProduceTest {
@Autowired
private RabbitProduce rabbitProduce;
@Before
public void before() {
}
@Test
public void testStartUp() {
rabbitProduce.sendMsg();
}
}
运行结果
源码地址: