개요
Netease에서 인터뷰를 할 때 면접관은 나에게 질문을하고
주문을 한 후 사용자가 지불하지 않고 주문을 취소 해야하는 경우 어떻게해야합니까?
당시의 대답은 시간이 정해진 작업을 사용하여 DB 테이블을 스캔하는 것이 었습니다. 면접관은 그다지 만족스럽지 않고 다음과 같이 물었습니다.
정확한 실시간 알림을 달성하기 위해 시간이 지정된 작업을 사용하는 다른 방법이 있습니까?
그 당시 내 대답은 다음과 같습니다.
대기열을 사용할 수 있습니다. 주문이 배치 된 후 메시지가 큐로 전송되고 만료 시간이 지정됩니다. 시간이 오면 콜백 인터페이스가 실행됩니다.
면접관이들은 후, 그는 묻지 않았다. 사실, 내 생각은 그 당시에 옳았지만 나는 그다지 전문적이지 않았습니다. 전문적인 말은 지연된 메시지를 사용하는 것입니다.
실제로, 시간이 정해진 작업을 사용하는 데 실제로 문제가 있습니다. 원래 비즈니스 시스템은 주문이 10 분 안에 지불되지 않으면 주문이 즉시 취소되고 제품 재고가 릴리스되기를 희망합니다. 그러나 데이터 볼륨이 커지면 무급 주문 데이터를 얻는 시간이 확장됩니다. 10 분 후에 일부 주문은 15 분, 20 분 등이 될 수 있습니다. 이런 식으로 재고는 제 시간에 해제되지 않으며 홀수에도 영향을 미칩니다. 지연 메시지를 사용하여 주문 취소 작업은 설정 시간에 따라 이론적으로 수행 될 수 있습니다.
현재 RabbitMQ를 사용하여 지연된 메시지를 구현하는 것에 대한 인터넷의 대부분의 기사는 RabbitMQ의 죽은 편지 대기열을 사용하여 구현하는 방법에 관한 것입니다. 구현 솔루션은 매우 복잡해 보이며 원래 RabbitMQ 클라이언트 API를 사용하여 구현됩니다.
Spring Boot는 RabbitMQ 클라이언트 API를 포장하여 사용하기가 훨씬 간단합니다. 다음은 지연된 메시지를 구현하기 위해 Rabbitmq_delayed_Message_Exchange 및 Spring Boot를 사용하는 방법에 대한 자세한 소개입니다.
소프트웨어 준비
Erlang
이 기사에 사용 된 버전은 다음과 같습니다. Erlang 20.3
RabbitMq
이 기사는 RabbitMQ의 창 버전을 사용하고 버전 번호는 다음과 같습니다. 3.7.4
Rabbitmq_delayed_message_exchange 플러그인
플러그인 다운로드 주소 : http://www.rabbitmq.com/community-plugins.html
URL을 열면 ctrl + f를 검색하고 Rabbitmq_delayed_message_exchange를 검색하십시오.
버전 번호를 선택해야합니다. RabbitMQ 3.7.4를 사용하고 있으므로 해당 RabbitMQ_DELAYED_MESSAGE_EXCHANGE 플러그인도 3.7.x를 선택해야합니다.
올바른 버전을 선택하지 않으면 지연된 메시지를 사용할 때 다양한 이상한 문제가 발생하며 인터넷에는 솔루션이 없습니다. 나는이 문제 때문에 밤새도록 고생했다. 올바른 플러그인 버전을 선택하십시오.
플러그인을 다운로드 한 후 RabbitMQ 설치 디렉토리 아래 플러그인 디렉토리에 배치하고 다음 명령을 사용하여 플러그인을 시작하십시오.
Rabbitmq-Plugins는 Rabbitmq_delayed_message_exchange를 활성화합니다
시작이 성공하면 다음 메시지가 나타납니다.
다음 플러그인이 활성화되었습니다 : Rabbitmq_delayed_message_exchange
플러그인이 성공적으로 출시 된 후 RabbitMQ를 다시 시작하여 적용하십시오.
통합 RabbitMq
이것은 매우 간단합니다. Maven 프로젝트의 pom.xml 파일에 추가하십시오.
<pectionency> <groupId> org.springframework.boot </groupid> <artifactid> 스프링 부트 스타터-amqp </artifactid> </fectionency>
스프링 부츠에 2.0.1. release를 사용하고 있습니다.
다음으로 Application.Properties 파일에 Redis 구성을 추가하십시오.
spring.rabbitmq.host = 127.0.0.1spring.rabbitmq.port = 5672spring.rabbitmq.username = guestspring.rabbitmq.password = Guest
ConnectionFactory 및 RabbitTemplate를 정의하십시오
또한 매우 간단합니다. 코드는 다음과 같습니다.
패키지 com.mq.rabbitmq; import org.springframework.amqp.rabbit.connection.cachingconnectionfactory; import org.sprampramework.amqp.rabbit.connection.connectionfactory; import org.sprampramp.rabbit.corbit.rabbittemplate; org.springframework.boot.context.properties.configurationProperties; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration;@configurationproperties (public classmcon = "spring.rabbitmq")). 주인; 개인 int 포트; 개인 문자열 사용자 이름; 개인 문자열 비밀번호; @Bean public connectionFactory ConnectionFactory () {CachingConnectionFactory CachingConnectionFactory = New CachingConnectionFactory (호스트, 포트); CachingConnectionFactory.SetUserName (사용자 이름); CachingConnectionFactory.SetPassword (비밀번호); CachingConnectionFactory.setVirtualHost ( "/"); CachingConnectionFactory.SetPublishErconFirms (True); CachingConnectionFactory를 반환합니다. } @Bean public RabbitTemplate RabbitTemplate () {RabbitTemplate RabbitTemplate = New RabbitTemplate (ConnectionFactory ()); RabbitTemplate를 반환합니다. } public String gethost () {return host; } public void sethost (문자열 호스트) {this.host = host; } public int getport () {반환 포트; } public void setport (int port) {this.port = 포트; } public String getUserName () {return username; } public void setusername (String username) {this.username = username; } public String getPassword () {return password; } public void setpassword (문자열 비밀번호) {this.password = password; }}교환 및 대기열 구성
패키지 com.mq.rabbitmq; import org.springframework.amqp.core.*; import org.springframework.context.annotation.bean; import org.spramework.context.annotation.configuration; import java.util.hashmap; import java.util.map; {@Bean public customexChange DelayExChange () {map <String, object> args = new Hashmap <> (); args.put ( "x-delayed-type", "direct"); 새로운 customexchange ( "test_exchange", "x-delayed-message", true, false, args)를 반환합니다. } @bean public queue queue () {큐 큐 큐 = 새로운 큐 ( "test_queue_1", true); 반환 대기열; } @bean public binding binding () {return bindingBuilder.bind (queue ()). }}여기서 customexchange가 DirecTexChange가 아닌 사용되며 customexChange의 유형은 x-delayed-message 여야한다는 점에 유의해야합니다.
메시지 보내기를 구현하십시오
패키지 com.mq.rabbitmq; import org.springframework.amqp.amqpexception; import org.springframework.amqp.core.message; import org.spramframework.amqp.core.messagepostprocessor; import org.spramp.rabbit.core.core org.springframework.beans.beans.annotation.autowired; import org.springframework.stereotype.service; import java.text.simpledateformat; import java.util.date; @servicepublic class ramesserviceimpl {@autow private rabbittemplate platper; public void sendmsg (String QueueName, String Msg) {simpledateformat sdf = new SimpledateFormat ( "yyyy-mm-dd hh : mm : ss"); System.out.println ( "메시지 보내기 시간 :"+sdf.format (new date ()); RabbitTemplate.convertandSend ( "test_exchange", queuename, msg, new MessagePostProcessor () {@override public message postprocessmessage (message message)가 amqpexception {message.getmessageProperties (). setheader ( "x-delay", 3000);}); }}보낼 때 헤더를 추가해야합니다
X- 델리
여기서 설정 한 지연 시간은 3 초입니다.
메시지 소비자
패키지 com.mq.rabbitmq; import org.springframework.amqp.rabbit.annotation.rabbithandler; import org.springframework.amqp.rabbit.annotation.rabbitlistener; import org.springframework.stereotyp.component; import java.sext.simpled format; java.util.date; @componentpublic class messagereceiver {@rabbitlistener (queues = "test_queue_1") public void lece 수신 (string msg) {simplededateformat sdf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); System.out.println ( "메시지 수신 시간 :"+sdf.format (new date ()); System.out.println ( "수신 된 메시지 :"+msg); }}Spring Boot 프로그램을 실행하고 메시지를 보냅니다
기본 메소드에서 스프링 부팅 프로그램을 직접 실행하면 Spring Boot가 자동으로 MessagerEceiver 클래스를 구문 분석합니다.
다음으로 Junit을 사용하여 메시지를 보내는 인터페이스를 실행하십시오.
패키지 com.mq.rabbitmq; import org.junit.test; import org.junit.runner.runwith; import org.springframework.bean.beans.annotation.autowired; import org.springframework.test.context.springboottest; 가져 오기; org.springframework.test.context.junit4.springrunner; @runwith (springrunner.class) @springboottestpublic class rabbitmqapplicationTests {@autowired private messagesViceimpl messageService; @test public void send () {messageService.sendmsg ( "test_queue_1", "hello 나는 지연 msg"); }} 실행 후 다음 정보를 볼 수 있습니다.
메시지 보내기 시간 : 2018-05-03 12:44:53
3 초 후에 Spring Boot 콘솔이 출력됩니다.
메시지 수신 시간 : 2018-05-03 12:44:56
받은 메시지 : 안녕하세요 저는 지연 MSG입니다
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.