1. 서문
메시지 미들웨어에는 메시지 중개인 및 대상이라는 두 가지 중요한 개념이 있습니다. 메시지 발신자가 메시지를 보낼 때 메시지 브로커가 메시지를 인수하여 메시지가 지정된 대상으로 전달되도록합니다.
일반적으로 사용되는 메시지 중개인에는 JMS 및 AMQP 사양이 포함됩니다. 이에 따라, 공통 구현은 ActiveMQ 및 RabbitMQ입니다.
2. ActiveMQ를 통합합니다
2.1 종속성 추가
<pectionency> <groupid> org.springframework.boot </groupid> <artifactid> Spring-Boot-Starter-ActiveMq </artifactid> </fectionement> <!-연결 풀을 구성 해야하는 경우 다음 종속성을 추가해야합니다.> <pependency> <groupId> org.apache.Accake.Accake </groupicid> actiifactid> </의존성>
2.2 구성 추가
# ActiveMQ 구성 Spring.ActiveMQ.Broker-URL = TCP : //192.168.2.12 : 61616Spring.ActiveMq.user = Adminspring.activeMq.Password = Adminspring.ActiveMq.pool.enabled = falsespring.actespring.plivemq.pool.pool.pool.pool.pool.pool.pool.connection = 50# 구성이 truespring.jms.pub-sub-domain = false로 설정해야합니다
여기서 spring.activemq.pool.enabled = false는 연결 풀을 닫는다는 것을 의미합니다.
2.3 코딩
구성 클래스 :
@ConfigurationPublic 클래스 JMSConfirguration {public static final String queue_name = "activemq_queue"; public static final string topic_name = "activemq_topic"; @bean public queue queue () {return new activemqqueue (queue_name); } @bean public topic topic () {return new activemqtopic (elove_name); }}대기열과 주제를 작성하는 책임이 있습니다.
메시지 제작자 :
@ComponentPublic Class JMSSender {@autowired 개인 대기열 큐; @autowired 개인 주제 주제; @autowired private jmsmessagingtemplate jmstemplate; public void sendbyqueue (문자열 메시지) {this.jmstemplate.convertandSend (큐, 메시지); } public void sendbytopic (문자열 메시지) {this.jmstemplate.convertandSend (주제, 메시지); }}메시지 소비자 :
@ComponentPublic Class jmsReceiver {@jmslistener (대상 = jmsconfirguration.queue_name) public void inceptyByqueue (문자열 메시지) {system.out.println ( "큐 큐 메시지 :" + 메시지); } @jmslistener (대상 = jmsconfirguration.topic_name) public void lecebebytopic (문자열 메시지) {system.out.println ( "주제 메시지 :" + 메시지); }}메시지 소비자는 @jmslistener 주석을 사용하여 메시지를 듣습니다.
2.4 테스트
@runwith (springrunner.class) @springboottestpublic class jmstest {@autowired private jmssender sender; @test public void testsendbyqueue () {for (int i = 1; i <6; i ++) {this.sender.sendbyqueue ( "hello activemq queue"+i); }} @test public void testsendBytopic () {for (int i = 1; i <6; i ++) {this.sender.sendbytopic ( "hello activemq topic"+i); }}}인쇄 결과 :
큐를 받으십시오 : Hello ActiveMQ 대기열 1
큐를 받으십시오 : hello activemq 대기열 2
대기열 메시지를 받으십시오 : Hello ActiveMQ 큐 3
큐를 받으십시오 : hello activemq 큐 4
큐 메시지 수신 : hello activemq 큐 5
spring.jms.pub-sub-domain = publish/subscribe 모드를 테스트 할 때 true를 설정하십시오
주제 메시지 받기 : hello activemq 주제 1
주제 메시지 받기 : hello activemq 주제 2
주제 메시지 받기 : hello activemq 주제 3
주제 메시지 받기 : hello activemq 주제 4
주제 메시지 받기 : hello activemq 주제 5
3. RabbitMQ 통합
3.1 종속성 추가
<pectionency> <groupId> org.springframework.boot </groupid> <artifactid> 스프링 부트 스타터-amqp </artifactid> </fectionency>
3.2 구성 추가
spring.rabbitmq.host = 192.168.2.30spring.rabbitmq.port = 5672spring.rabbitmq.username = lightspring.rabbitmq.password = lightspring.rabbitmq.virtual-host =/test
3.3 코딩
구성 클래스 :
@Configurationpublic class AmqpConfirguration { //================================================= public static final String SIMPLE_QUEUE = "simple_queue"; @bean public queue queue () {return new queue (simple_queue, true); } // ================ 공개 정적 최종 문자열 ps_queue_1 = "ps_queue_1"; 공개 정적 최종 문자열 ps_queue_2 = "ps_queue_2"; 공개 정적 최종 문자열 fanout_exchange = "fanout_exchange"; @bean public equeue psqueue1 () {return new queue (ps_queue_1, true); } @bean public equeue psqueue2 () {return new queue (ps_queue_2, true); } @Bean 공개 fanOutExChange fanOutExChange () {return new fanOutexChange (fanout_exchange); } @Bean public binding fanOutBinding1 () {return bindingBuilder.bind (psqueue1 ()). } @bean public binding fanoutbinding2 () {return bindingBuilder.bind (psqueue2 ()). } // ========================= 공개 정적 최종 문자열 Routing_Queue_1 = "Routing_queue_1"; 공개 정적 최종 문자열 Routing_queue_2 = "Routing_queue_2"; 공개 정적 최종 문자열 direct_exchange = "direct_exchange"; @bean public equeue louringqueue1 () {return new queue (Routing_queue_1, true); } @bean public queue louringqueue2 () {return new queue (Routing_queue_2, true); } @Bean public DirectExChange DirectExChange () {return new DirectExchange (Direct_exChange); } @bean public binding DirectBinding1 () {return bindingBuilder.bind (Routingqueue1 ()). to (DirectExchange ()). } @bean public binding directBinding2 () {return bindingBuilder.bind (Routingqueue2 ()). to (DirectExchange ()). } // ============= 테마 모드 ================== 공개 정적 최종 문자열 topic_queue_1 = "topic_queue_1"; public static final string topic_queue_2 = "topic_queue_2"; public static final string topic_exchange = "topic_exchange"; @bean public equeue topicqueue1 () {return new queue (topic_queue_1, true); } @bean public equeue topicqueue2 () {return new queue (topic_queue_2, true); } @bean public topicexchange topicexchange () {return new topicexchange (topic_exchange); } @bean public binding topicbinding1 () {return bindingBuilder.bind (topicqueue1 ()). to (topicexchange ()). } @bean public binding topicbinding2 () {return bindingbuilder.bind (topicqueue2 ()). to (topicexchange ()). }}RabbitMQ에는 여러 작업 모드가 있으므로 많은 구성이 있습니다. 관련 콘텐츠에 대해 알고 싶은 독자는 "RabbitMQ 작업 모드 소개"또는 Baidu 관련 정보 자체를 확인할 수 있습니다.
메시지 제작자 :
@componentpublic class amqpsender {@autowired private amqptemplate amqptemplate; / ** * 간단한 모드 보내기 * * @param 메시지 */ public void simplesend (문자열 메시지) {this.amqptemplate.convertandSend (amqpconfirguration.simple_queue, message); }/** * 게시/구독 모드 보내기 * * @param 메시지 */public void pssend (문자열 메시지) {this.amqptemplate.convertandSend (amqpconfirguration.fanout_exchange, "", 메시지); } / ** * 라우팅 모드를 보내십시오 * * @Param 메시지 * / public void louringendend (문자열 라우팅 키, 문자열 메시지) {this.amqptemplate.convertAndSend (amqpconfirguration.direct_exchange, louringkey, message); } / ** * 테마 모드 보내기 * * @Param RoutingKey * @param 메시지 * / public void topicsend (문자열 라우팅 키, 문자열 메시지) {this.amqptemplate.convertAndsend (amqpconfirguration.topic_exchange, Routingkey, Message); }}메시지 소비자 :
@ComponentPublic Class AMQPreceiver { / ** * Simple Mode Reception * * @param 메시지 * / @RabbitListener (queues = amqpconfirguration.simple_queue) public void simplereceive (문자열 메시지) {System.out.println ( "메시지 수신); }/** * 게시/가입 모드 리셉션 * * @param 메시지 */@RabbitListener (queues = amqpconfirguration.ps_queue_1) public void psreceive1 (문자열 메시지) {system.out.println (amqpconfirguration.ps_queue_1 + "메시지); } @RabbitListener (queues = amqpconfirguration.ps_queue_2) public void psreceive2 (문자열 메시지) {system.out.println (amqpconfirguration.ps_queue_2 + "메시지 수신 :" + 메시지); } / ** * 라우팅 모드 리셉션 * * @Param 메시지 * / @RabbitListener (Queues = amqpconfirguration.routing_queue_1) public void RoutingReceive1 (문자열 메시지) {System.out.println (amqpconfirguration.routing_queue_1 + "메시지 :" + 메시지); } @RabBitListener (queues = amqpconfirguration.routing_queue_2) public void louringreceive2 (문자열 메시지) {system.out.println (amqpconfirguration.routing_queue_2 + "메시지 받기 :" + 메시지); } / ** * 주제 모드 리셉션 * * @param 메시지 * / @RabbitListener (queues = amqpconfirguration.topic_queue_1) public void topicreceive1 (string message) {system.out.println (amqpconfirguration.topic_queue_1 + "메시지 :" + 메시지); } @RabbitListener (queues = amqpconfirguration.topic_queue_2) public void topicreceive2 (문자열 메시지) {system.out.println (amqpconfirguration.topic_queue_2 + "메시지 :" + 메시지); }}메시지 소비자는 @RabbitListener 주석을 사용하여 메시지를 듣습니다.
3.4 테스트
@runwith (springrunner.class) @springboottestpublic class amqptest {@autowired private amqpsender sender; @test public void testimplesend () {for (int i = 1; i <6; i ++) {this.sender.simplesend ( "test simplesend"+i); }} @test public void testpssend () {for (int i = 1; i <6; i ++) {this.sender.pssend ( "test pssend"+i); }} @test public void testpssend () {for (int i = 1; i <6; i ++) {this.sender.pssend ( "test pssend"+i); }} @test public void testroutingsend () {for (int i = 1; i <6; i ++) {this.sender.routingsend ( "Order", "Test Routingsend"+i); }} @test public void testOpicsend () {for (int i = 1; i <6; i ++) {this.sender.topicsend ( "user.add", "테스트 주제"+i); }}}테스트 결과가 건너 뜁니다. . .
알림 1 : Access_Refused Login이 인증 메커니즘을 사용하여 거부되었습니다.
해결책:
1) 사용자 이름과 비밀번호가 올바른지 확인하십시오. 사용자 이름과 비밀번호의 값에 공백 또는 탭이 포함되어 있는지 여부를 주목하는 것이 중요합니다 (비밀번호에 탭 문자가 하나 더 있었기 때문에 저자는 테스트하여 인증 실패를 일으켰습니다).
2) 테스트 계정이 게스트를 사용하는 경우 RabbitMq.conf 파일을 수정해야합니다. "loopback_users = none"구성을 파일에 추가하십시오.
리스너를 위해 대기열을 준비 할 수 없습니다. 대기열이 존재하지 않거나 브로커는 우리가 그것을 사용할 수 없습니다.
해결책:
RabbitMQ Management 인터페이스에 로그인하고 대기열 옵션에 해당 대기열을 수동으로 추가 할 수 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.