安装消息中间件
Windows安装ErLang
https://github.com/erlang/otp/releases/tag/OTP-25.0
Windows安装RabbitMq
https://www.rabbitmq.com/install-windows.html
安装RabbitMq UI界面
打开RabbitMQ Command Prompt 进入命令行
rabbitmqctl.bat status
rabbitmq-plugins enable rabbitmq_management
访问http://localhost:15672/ 默认账号密码guest/guest
安装延时消息插件
https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases 将 ez文件拷贝到安装目录rabbitmq_server-3.10.2\plugins下
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
SpringBoot整合
这里我直接用我先前建好的微服务 order-service作为消息发送者,storage-service作为消息接收者
消息发送端order-service
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml
spring:
rabbitmq:
username: guest
password: guest
host: 127.0.0.1
port: 5672
publisher-confirm-type: correlated
publisher-returns: true
RabbitMqConfig
package top.fate.config;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class RabbitMqConfig {
private static final Logger LOG = LogManager.getLogger();
public static final String DIRECT_QUEUE = "direct_queue";
public static final String DIRECT_EXCHANGE = "direct_exchange";
public static final String DIRECT_ROUTING_KEY = "direct_routing_key";
public static final String DELAY_QUEUE = "delay_queue";
public static final String DELAY_EXCHANGE = "delay_exchange";
public static final String DELAY_ROUTING_KEY = "delay_routing_key";
@Autowired
private CachingConnectionFactory connectionFactory;
@Bean
public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory)
{
RabbitTemplate rabbitTemplate = new RabbitTemplate();
rabbitTemplate.setConnectionFactory(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
rabbitTemplate.setMandatory(true);
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback()
{
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause)
{
LOG.info("\n确认消息送到交换机(Exchange)结果:");
LOG.info("相关数据:" + correlationData);
LOG.info("是否成功:" + ack);
LOG.info("错误原因:" + cause);
}
});
rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback()
{
@Override
public void returnedMessage(ReturnedMessage returnedMessage)
{
LOG.info("\n确认消息送到队列(Queue)结果:");
LOG.info("发生消息:" + returnedMessage.getMessage());
LOG.info("回应码:" + returnedMessage.getReplyCode());
LOG.info("回应信息:" + returnedMessage.getReplyText());
LOG.info("交换机:" + returnedMessage.getExchange());
LOG.info("路由键:" + returnedMessage.getRoutingKey());
}
});
return rabbitTemplate;
}
@Bean
public Jackson2JsonMessageConverter jsonMessageConverter()
{
return new Jackson2JsonMessageConverter();
}
@Bean
public DirectExchange directExchange()
{
return new DirectExchange(DIRECT_EXCHANGE, true, false);
}
@Bean
public Queue directQueue()
{
return new Queue(DIRECT_QUEUE, true, false, false, null);
}
@Bean
Binding directBinding(DirectExchange directExchange, Queue directQueue)
{
return BindingBuilder.bind(directQueue).to(directExchange).with(DIRECT_ROUTING_KEY);
}
@Bean
public CustomExchange delayExchange()
{
Map<String, Object> args = new HashMap<>();
args.put("x-delayed-type", "direct");
return new CustomExchange(DELAY_EXCHANGE, "x-delayed-message", true, false, args);
}
@Bean
public Queue delayQueue()
{
Queue queue = new Queue(DELAY_QUEUE, true);
return queue;
}
@Bean
public Binding delaybinding(Queue delayQueue, CustomExchange delayExchange)
{
return BindingBuilder.bind(delayQueue).to(delayExchange).with(DELAY_ROUTING_KEY).noargs();
}
}
实体对象
package top.fate.entity;
import lombok.Data;
@Data
public class TestEntity {
private String username;
private String password;
public TestEntity(String username, String password) {
this.username = username;
this.password = password;
}
public TestEntity() {
}
}
生产者服务接口
package top.fate.service;
import java.util.Map;
public interface ProducerService {
void sendTestJson(Object o);
void sendDelayTestMap(Map map);
}
生产者服务实现类
package top.fate.service.impl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.fate.config.RabbitMqConfig;
import top.fate.service.ProducerService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
@Service
public class ProducerServiceImpl implements ProducerService {
private static final Logger LOG = LogManager.getLogger();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void sendTestJson(Object o) {
rabbitTemplate.convertAndSend(RabbitMqConfig.DIRECT_EXCHANGE, RabbitMqConfig.DIRECT_ROUTING_KEY, o);
LOG.info("json格式的数据发送成功 发送时间为" + formatter.format(new Date()));
}
@Override
public void sendDelayTestMap(Map map) {
rabbitTemplate.convertAndSend(RabbitMqConfig.DELAY_EXCHANGE, RabbitMqConfig.DELAY_ROUTING_KEY, map, new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
message.getMessageProperties().setHeader("x-delay", 5000);
return message;
}
});
LOG.info("map格式的数据发送成功 发送时间为" + formatter.format(new Date()));
}
}
测试Controller
package top.fate.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.fate.entity.TestEntity;
import top.fate.service.ProducerService;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "producer")
public class ProducerController {
@Autowired
private ProducerService producerService;
@GetMapping("sendObject")
public void sendObject(){
producerService.sendTestJson(new TestEntity("user","123456"));
}
@GetMapping("sendMap")
public void sendMap(){
Map map = new HashMap();
map.put("user1",new TestEntity("user1","123"));
map.put("user2",new TestEntity("user2","123"));
map.put("user3",new TestEntity("user3","123"));
producerService.sendDelayTestMap(map);
}
}
消息接收端storage-service
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
RabbitMqConfig
package top.fate.config;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.fate.service.impl.AckReceiver;
@Configuration
public class RabbitMqConfig {
public static final String DIRECT_QUEUE = "direct_queue";
public static final String DELAY_QUEUE = "delay_queue";
@Autowired
private AckReceiver ackReceiver;
@Autowired
private CachingConnectionFactory connectionFactory;
@Bean
public SimpleMessageListenerContainer simpleMessageListenerContainer()
{
int DEFAULT_CONCURRENT = 10;
int DEFAULT_PREFETCH_COUNT = 50;
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
container.setConcurrentConsumers(DEFAULT_CONCURRENT);
container.setMaxConcurrentConsumers(DEFAULT_PREFETCH_COUNT);
container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
container.addQueues(new Queue(DIRECT_QUEUE,true));
container.addQueues(new Queue(DELAY_QUEUE,true));
container.setMessageListener(ackReceiver);
return container;
}
}
消息接收接口
package top.fate.service;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import java.io.IOException;
public interface ConsumerReceiver {
void receiverJson(Message message, Channel channel) throws IOException;
void receiverMap(Message message, Channel channel) throws IOException;
}
消息接收实现类
package top.fate.service.impl;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Service;
import top.fate.entity.TestEntity;
import top.fate.service.ConsumerReceiver;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
@Service
public class ConsumerReceiverImpl implements ConsumerReceiver {
private static final Logger LOG = LogManager.getLogger();
@Override
public void receiverJson(Message message, Channel channel) throws IOException {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
try
{
TestEntity testEntity = JSON.parseObject(message.getBody(), TestEntity.class);
LOG.info("接收者收到JSON格式消息:");
System.out.println("账号:" + testEntity.getUsername());
System.out.println("密码:" + testEntity.getPassword());
channel.basicAck(deliveryTag, true);
}
catch (Exception e)
{
channel.basicReject(deliveryTag, false);
e.printStackTrace();
}
}
@Override
public void receiverMap(Message message, Channel channel) throws IOException {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
try
{
Map map = JSON.parseObject(message.getBody(), Map.class);
LOG.info("接收者收到Map格式消息:");
LOG.info(map.get("user1"));
LOG.info(map.get("user2"));
LOG.info(map.get("user3"));
channel.basicAck(deliveryTag, true);
}
catch (Exception e)
{
channel.basicReject(deliveryTag, false);
e.printStackTrace();
}
}
}
消息分发处理类
package top.fate.service.impl;
import com.rabbitmq.client.Channel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.fate.config.RabbitMqConfig;
import top.fate.service.ConsumerReceiver;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class AckReceiver implements ChannelAwareMessageListener {
private static final Logger LOG = LogManager.getLogger();
@Autowired
private ConsumerReceiver consumerReceiver;
@Override
public void onMessage(Message message, Channel channel) throws Exception
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
LOG.info("消息接收成功,接收时间:" + dateFormat.format(new Date()) + "\n");
String queueName = message.getMessageProperties().getConsumerQueue();
if (queueName.equals(RabbitMqConfig.DIRECT_QUEUE))
{
consumerReceiver.receiverJson(message, channel);
}
if (queueName.equals(RabbitMqConfig.DELAY_QUEUE))
{
consumerReceiver.receiverMap(message, channel);
}
}
}
启动测试
项目启动的时候创建交换机绑定路由key,及创建队列
|