1. 程式人生 > >(十五)springboot實戰rabbitmq --- Fanout模式

(十五)springboot實戰rabbitmq --- Fanout模式

前幾篇文章介紹了rabbitmq的原理介紹,springboot整合rabbitmq的direct模式topic模式這篇文章我們來介紹一下Fanout廣播模式

rabbitmq的其他內容我就不介紹了,如果有直接使用廣播模式的小夥伴建議讀下前面的原理和其他模式的介紹這樣能加深你的理解。沒有理解原理的技術應用只是行屍走肉。

其中的pom配置和application.properties配置相同。

FanoutRabbitConfig的bean配置

package com.rabbit.produce.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FanoutRabbitConfig {

    /**
     * 定義佇列名稱
     */
    final static String message = "topic.A";
    final static String messages = "topic.B";

    @Bean
    public Queue queueA() {
        return new Queue(FanoutRabbitConfig.message);
    }

    @Bean
    public Queue queueB() {
        return new Queue(FanoutRabbitConfig.messages);
    }

    @Bean
    FanoutExchange exchange() {
        return new FanoutExchange("FanoutExchange");
    }

    @Bean
    Binding bindingExchangeMessage(@Qualifier("queueA")Queue queueMessage, FanoutExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange);
    }

    @Bean
    Binding bindingExchangeMessages(@Qualifier("queueB")Queue queueMessages, FanoutExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange);
    }
}

生產者傳送訊息配置

package com.rabbit.produce.controller;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class FanoutSender {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "hi, i am message all";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("FanoutExchange","topic.1", context);
    }

    public void send1() {
        String context = "hi, i am message 1";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("FanoutExchange", "topic.messages", context);
    }

    public void send2() {
        String context = "hi, i am messages 2";
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("FanoutExchange", "topic.messages", context);

    }
}

上面的程式碼童鞋們可能會有疑問了廣播模式不是不需要onvertAndSend的第二個引數(routing_key)麼,是的在廣播模式下寫上了routing_key也會被忽略的,那麼我問什麼新增呢,看下圖

在這裡插入圖片描述 可能是使用版本的問題,如果我省略了第二個引數不寫的話,預設的交換機位置會被自動認為routing_key引數,這樣就會找不到交換機,所以上面的程式碼中第二個引數都存在,當然你也可以放個空串什麼的,

消費者訊息接受

package com.rabbit.produce.controller;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author : lqf
 * @Description :
 * @date : Create in 18:37 2018/9/26
 */
@Component
public class FanoutReceiver2 {
    @RabbitListener(queues = "topic.B")
    public void process(String message) {
        System.out.println("TopicReceiverB  : " + message);
    }

    @RabbitListener(queues = "topic.A")
    public void process2(String message) {
        System.out.println("FanoutReceiverA  : " + message);
    }
}

測試

package com.rabbit.produce;

import com.rabbit.produce.controller.FanoutSender;
import com.rabbit.produce.controller.HelloSender;
import com.rabbit.produce.controller.HelloSender2;
import com.rabbit.produce.controller.TopicSender;
import com.rabbit.produce.entrty.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BootRabbitmqProduceApplicationTests {

    @Autowired
    private FanoutSender fanoutSender;

    @Test
    public void topicExchange() throws Exception {
        fanoutSender.send();
        fanoutSender.send1();
        fanoutSender.send2();
    }

}

測試結果

TopicReceiverB  : hi, i am messages 2
FanoutReceiverA  : hi, i am message all
TopicReceiverB  : hi, i am message 1
TopicReceiverB  : hi, i am message all
FanoutReceiverA  : hi, i am message 1
FanoutReceiverA  : hi, i am messages 2

到這裡springboot整合rabbitmq的4種模式我寫了3種基本上就整合完了,其中有一種header exchange(頭交換機)用法這種用法比較少用我就不在這裡贅述了,感興趣的小夥伴自行搜尋吧。