rabbitMQ應用,laravel生產廣播訊息,springboot消費訊息

來源:互聯網
上載者:User

標籤:安裝   官方   dock   nts   開發環境   art   star   docker安裝   lease   

最近做一個新需求,使用者發布了動態,前台需要查詢,為了使用者讀取資訊響應速度更快(MySQL很難實現或者說實現起來很慢),所以在使用者動態發布成功後,利用訊息機制非同步構建 redis緩衝 和 elasticsearch索引 。

開發環境

rabbitMQ服務端,docker安裝

拉取rabbit-mq鏡像docker pull hub.c.163.com/library/rabbitmq:3.6.10-management運行鏡像docker run -d --name rabbitmq --publish 5671:5671  --publish 5672:5672 --publish 4369:4369 --publish 25672:25672 --publish 15671:15671 --publish 15672:15672  hub.c.163.com/library/rabbitmq:3.6.10-management後台地址:http://192.168.1.8:15672

 

訊息生產端(PHP):

composer 安裝 rabbitmq用戶端composer require php-amqplib/php-amqplib生產廣播訊息官方demohttps://github.com/php-amqplib/php-amqplib/blob/master/demo/amqp_publisher_fanout.php

應用中代碼

<?php/** * User: [email protected] * Date: 2018/6/18 * Time: 下午1:54 */namespace App\ThirdParty\Message;use PhpAmqpLib\Connection\AMQPStreamConnection;use PhpAmqpLib\Message\AMQPMessage;class AmqpPublisher{    public function send($content)    {        $exchange = ‘message.fanout.exchange‘;        // 建立串連        $connection = new AMQPStreamConnection(            config(‘app.mq_host‘),            config(‘app.mq_port‘),            config(‘app.mq_user‘),            config(‘app.mq_pass‘),            config(‘app.mq_vhost‘)        );        $channel = $connection->channel();        /*            name: $exchange            type: fanout            passive: false // don‘t check is an exchange with the same name exists            durable: false // the exchange won‘t survive server restarts            auto_delete: true //the exchange will be deleted once the channel is closed.        */        $channel->exchange_declare($exchange, ‘fanout‘, false, true, false);        $messageBody = $content;        $message = new AMQPMessage($messageBody, array(‘content_type‘ => ‘text/plain‘));        $channel->basic_publish($message, $exchange);        // 關閉通道        $channel->close();        // 關閉串連        $connection->close();    }}

 

 

 

訊息消費端(Java):

 

引入maven依賴<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-amqp</artifactId></dependency>    

 

配置廣播隊列資訊

package cn.taxiong.release.config;import cn.taxiong.release.constant.QueueConstants;import lombok.extern.slf4j.Slf4j;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.FanoutExchange;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * RabbitMQFanout模式配置 * * @author [email protected] * @create 2018-06-18 下午4:04 **/@Slf4j@Configurationpublic class RabbitMQFanoutConfig {    @Bean    public Queue createFanoutQueueCache() {        log.info( "建立了FanoutQueue cache 緩衝 隊列" );        return new Queue(QueueConstants.MESSAGE_QUEUE_RELEASE_CACHE_NAME);    }    @Bean    public Queue createFanoutQueueIndex() {        log.info( "建立了FanoutQueue index 緩衝 隊列" );        return new Queue(QueueConstants.MESSAGE_QUEUE_RELEASE_INDEX_NAME);    }    @Bean    public FanoutExchange fanoutExchangeRelease() {        log.info( "建立了fanoutExchange交換器" );        return new FanoutExchange( QueueConstants.MESSAGE_FANOUT_EXCHANGE);    }    @Bean    public Binding fanoutExchangeCacheQueueBinding() {        log.info( "將FanoutQueue cache 隊資料行繫結到交換器fanoutExchange" );        return BindingBuilder.bind( createFanoutQueueCache() ).to( fanoutExchangeRelease() );    }    @Bean    public Binding fanoutExchangeIndexQueueBinding() {        log.info( "將FanoutQueue index 隊資料行繫結到交換器fanoutExchange" );        return BindingBuilder.bind( createFanoutQueueIndex() ).to( fanoutExchangeRelease() );    }}

 

隊列常量資訊

package cn.taxiong.release.constant;/** * 隊列常量 * * @author [email protected] * @create 2018-06-14 下午7:02 **/public interface QueueConstants {/**     * 訊息交換     */    String MESSAGE_FANOUT_EXCHANGE = "message.fanout.exchange";    /**     * 發布緩衝訊息佇列名稱     */    String MESSAGE_QUEUE_RELEASE_CACHE_NAME = "message.release.cache.queue";    /**     * 發布索引訊息佇列名稱     */    String MESSAGE_QUEUE_RELEASE_INDEX_NAME = "message.release.index.queue";}


緩衝(cache)服務消費訊息:

package cn.taxiong.release.message;import cn.taxiong.release.constant.QueueConstants;import cn.taxiong.release.service.OperateReleaseService;import lombok.extern.slf4j.Slf4j;import org.springframework.amqp.rabbit.annotation.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.handler.annotation.Payload;import org.springframework.stereotype.Component;/** * 訊息消費 * * @author [email protected] * @create 2018-06-14 下午7:14 **/@Slf4j@Component@RabbitListener(queues = QueueConstants.MESSAGE_QUEUE_RELEASE_CACHE_NAME)public class MessageConsumer {    @Autowired    private OperateReleaseService operateReleaseService;    @RabbitHandler    public void handler(@Payload String message) {        // operateReleaseService.storeReleaseRedisCache(message);        log.info("緩衝cache訊息消費1:{}", message);    }}

 


索引(index)服務消費訊息:

package cn.taxiong.release.message;import cn.taxiong.release.constant.QueueConstants;import cn.taxiong.release.service.OperateReleaseService;import lombok.extern.slf4j.Slf4j;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.handler.annotation.Payload;import org.springframework.stereotype.Component;/** * 訊息消費 * * @author [email protected] * @create 2018-06-14 下午7:14 **/@Slf4j@Component@RabbitListener(queues = QueueConstants.MESSAGE_QUEUE_RELEASE_INDEX_NAME)public class MessageConsumer2 {    @Autowired    private OperateReleaseService operateReleaseService;    @RabbitHandler    public void handler(@Payload String message) {        log.info("索引訊息 index 消費2:{}", message);    }}

 

 

rabbitMQ應用,laravel生產廣播訊息,springboot消費訊息

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.