springboot + rabbitmq 2 시간 입문 (4): fanout 교환기

개요:
방송 교환기, routingkey 를 무시 하고 현재 교환기 에 보 내 는 모든 메 시 지 를 현재 교환기 와 연 결 된 모든 대기 열 로 보 냅 니 다.
  application.properties :
spring.rabbitmq.host=localhost

# TCP/IP   5672,http   15672
spring.rabbitmq.port=5672

spring.rabbitmq.username=root

spring.rabbitmq.password=root

생산자:
package com.example.rabbitmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitMQController {
    
    //      RabbitTemplate   ,    AmqpTemplate,    RabbitTemplate。
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/helloRabbit2")
    public String sendMQ2(){
        rabbitTemplate.convertAndSend("myExchange2", "","fanoutExchange");
        return "success";
    }
}

소비자:
package com.example.rabbitmq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
public class Receiver {

    /**
     * @Exchange type   ExchangeTypes.DIRECT,     ExchangeTypes.FANOUT
     *      routingKey,      ,     
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "myQueue2"),
                    exchange = @Exchange(value = "myExchange2", type =ExchangeTypes.FANOUT)
            ))
    public void process2(Message message){
        System.out.println("myQueue2:" + new String(message.getBody()));
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "myQueue3"),
                    exchange = @Exchange(value = "myExchange2", type =ExchangeTypes.FANOUT)
            ))
    public void process3(Message message) {
        System.out.println("myQueue3:" + new String(message.getBody()));
    }

 
시작 항목, 접근http://localhost:8080/helloRabbit2콘 솔 인쇄
myQueue3:fanoutExchange myQueue2:fanoutExchange

좋은 웹페이지 즐겨찾기