Recently made a new demand, the user released the dynamic, the front desk needs to query, in order to read information for users to respond faster (MySQL is difficult to implement or slow to implement), so after the user dynamic release, using the message mechanism to build the Redis cache and Elasticsearch index asynchronously.
Development environment
RABBITMQ Service side, Docker installation
Pull rabbit-MQ mirrors Docker pull hub.c.163. COM/LIBRARY/RABBITMQ:3.6.Ten-Management running mirrored Docker run-D--name RABBITMQ--publish5671:5671--publish5672:5672--publish4369:4369--publish25672:25672--publish15671:15671--publish15672:15672HUB.C.163. COM/LIBRARY/RABBITMQ:3.6.Ten-Management Back office Address: http://192.168.1.8:15672
Message production side (PHP):
Composer Install RABBITMQ client composer require PHP-amqplib/php-amqplib production Broadcast message official DEMOHTTPS:// github.com/php-amqplib/php-amqplib/blob/master/demo/amqp_publisher_fanout.php
In-app code
<?PHP/** User: [email protected] * DATE:2018/6/18 * Time: PM 1:54*/namespace App\thirdparty\message; Usephpamqplib\connection\amqpstreamconnection; UsePhpamqplib\message\amqpmessage;classamqppublisher{ Public functionSend$content) { $exchange= ' Message.fanout.exchange '; //Create a connection $connection=Newamqpstreamconnection (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 a exchange with the S Ame name exists Durable:false//The Exchange won ' t survive server restarts Auto_delete:true//the Exchange would be deleted once the channel is closed. */ $channel->exchange_declare ($exchange, ' Fanout ',false,true,false); $messageBody=$content; $message=NewAmqpmessage ($messageBody,Array(' content_type ' = ' text/plain ')); $channel->basic_publish ($message,$exchange); //Close Channel $channel-Close (); //Close Connection $connection-Close (); }}
Message consumption End (Java):
Introducing Maven Dependencies < Dependency > < groupId >org.springframework.boot</groupId> < Artifactid>spring-boot-starter-amqp</artifactid> </ Dependency >
Configuring broadcast queue Information
PackageCn.taxiong.release.config;Importcn.taxiong.release.constant.QueueConstants;Importlombok.extern.slf4j.Slf4j;Importorg.springframework.amqp.core.Binding;ImportOrg.springframework.amqp.core.BindingBuilder;ImportOrg.springframework.amqp.core.FanoutExchange;ImportOrg.springframework.amqp.core.Queue;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;/*** Rabbitmqfanout Mode configuration * *@author[email protected] * @create 2018-06-18 pm 4:04 **/@Slf4j @configuration Public classRabbitmqfanoutconfig {@Bean PublicQueue Createfanoutqueuecache () {Log.info ("The Fanoutqueue cache cache queue was created" ); return NewQueue (queueconstants.message_queue_release_cache_name); } @Bean PublicQueue Createfanoutqueueindex () {Log.info ("The Fanoutqueue index cache queue was created" ); return NewQueue (queueconstants.message_queue_release_index_name); } @Bean PublicFanoutexchange fanoutexchangerelease () {Log.info ("Created a fanoutexchange switch" ); return NewFanoutexchange (Queueconstants.message_fanout_exchange); } @Bean PublicBinding fanoutexchangecachequeuebinding () {Log.info ("Bind the Fanoutqueue cache queue to switch Fanoutexchange" ); returnBindingbuilder.bind (Createfanoutqueuecache ()). to (Fanoutexchangerelease ()); } @Bean PublicBinding fanoutexchangeindexqueuebinding () {Log.info ("Bind Fanoutqueue index queue to switch Fanoutexchange" ); returnBindingbuilder.bind (Createfanoutqueueindex ()). to (Fanoutexchangerelease ()); }}
Queue constant Information
Packagecn.taxiong.release.constant;/*** Queue Constants * *@author[email protected] * @create 2018-06-14 pm 7:02 **/ Public Interfacequeueconstants {/*** Message Exchange*/String Message_fanout_exchange= "Message.fanout.exchange"; /*** Publish cache Message queue name*/String Message_queue_release_cache_name= "Message.release.cache.queue"; /*** Publish index Message queue name*/String Message_queue_release_index_name= "Message.release.index.queue";}
Caching (cache) service consumption message:
PackageCn.taxiong.release.message;Importcn.taxiong.release.constant.QueueConstants;ImportCn.taxiong.release.service.OperateReleaseService;Importlombok.extern.slf4j.Slf4j;Importorg.springframework.amqp.rabbit.annotation.*;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.messaging.handler.annotation.Payload;Importorg.springframework.stereotype.Component;/*** Message consumption * *@author[email protected] * @create 2018-06-14 pm 7:14 **/@Slf4j @component@rabbitlistener (Queues=queueconstants.message_queue_release_cache_name) Public classMessageconsumer {@AutowiredPrivateOperatereleaseservice Operatereleaseservice; @RabbitHandler Public voidHandler (@Payload String message) {//operatereleaseservice.storereleaserediscache (message);Log.info ("Cached cache message consumption 1:{}", message); }}
Index Service consumption message:
PackageCn.taxiong.release.message;Importcn.taxiong.release.constant.QueueConstants;ImportCn.taxiong.release.service.OperateReleaseService;Importlombok.extern.slf4j.Slf4j;ImportOrg.springframework.amqp.rabbit.annotation.RabbitHandler;ImportOrg.springframework.amqp.rabbit.annotation.RabbitListener;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.messaging.handler.annotation.Payload;Importorg.springframework.stereotype.Component;/*** Message consumption * *@author[email protected] * @create 2018-06-14 pm 7:14 **/@Slf4j @component@rabbitlistener (Queues=queueconstants.message_queue_release_index_name) Public classMessageConsumer2 {@AutowiredPrivateOperatereleaseservice Operatereleaseservice; @RabbitHandler Public voidHandler (@Payload String message) {Log.info ("Indexed message index consumption 2:{}", message); }}
RABBITMQ application, Laravel production broadcast messages, Springboot consumer messages