消息系统-RabbitMQ

消息系统-RabbitMQ

介绍

RabbitMQ 是一个高可用开源的分布式消息系统。

  • Erlang 语言开发的 AMQP 的开源实现
  • 异步消息传送,支持多种消息模式
  • 支持集群部署(high availability and throughput)
  • 管理&监控,HTTP-API, command line tool, and UI

消息发送模式

  • Work queues - Distributing tasks among workers (the competing consumers pattern)
  • Publish/Subscribe - Sending messages to many consumers at once
  • Routing - Receiving messages selectively
Work queues模式

 

Publish/Subscribe模式

  

Routing模式

  

安装RabbitMQ

wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.1/rabbitmq-server-3.6.1-1.noarch.rpm
sudo rpm --import https://www.rabbitmq.com/rabbitmq-signing-key-public.asc
sudo yum install rabbitmq-server-3.6.1-1.noarch.rpm

启动RabbitMQ&管理控制台

sudo systemctl start rabbitmq-server.service
sudo rabbitmq-plugins enable rabbitmq_management

http://148.70.158.155:15672/  

消息发送

    public final static String SFG_MESSAGE_QUEUE = "sfg-message-queue";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value="/send/{id}")
    public String sendMessage(@PathVariable String id) {
        log.info("Inside send message [{}]..",id);

        Map<String, String> actionmap = new HashMap<>();
        actionmap.put("id", id);
        log.info("Sending the index request through queue message");
        rabbitTemplate.convertAndSend(SFG_MESSAGE_QUEUE, actionmap);

        return "Hi...";
    }

消息接收

@Configuration
public class RabbitMQConfig {

    public final static String SFG_MESSAGE_QUEUE = "sfg-message-queue";

    @Bean
    Queue queue() {
        return new Queue(SFG_MESSAGE_QUEUE, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("spring-boot-exchange");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(SFG_MESSAGE_QUEUE);
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(SFG_MESSAGE_QUEUE);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(ProductMessageListener receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }
}

  

    /**
     * This method is invoked whenever any new message is put in the queue.
     * @param message
     */
    public void receiveMessage(Map<String, String> message) {
        log.info("Received <" + message + ">");
        Long id = Long.valueOf(message.get("id"));
        Product product = productRepository.findById(id).orElse(null);
        product.setMessageReceived(true);
        product.setMessageCount(product.getMessageCount() + 1);

        productRepository.save(product);
        log.info("Message processed...");
    }

 

发送消息

http://localhost:8072/send/3

Received <{id=3}>   

查看管理控制台

http://148.70.158.155:15672/#/queues

 参考文档

[1]: https://www.rabbitmq.com/getstarted.html

  

posted on 2020-12-07 11:33  rabbit-xf  阅读(63)  评论(0)    收藏  举报