1. 程式人生 >實用技巧 >其它 >微服務使用MQ資料傳輸

微服務使用MQ資料傳輸


MQ傳輸的方法有7種,常用的有5種。

  簡單、工作、廣播、路由、萬用字元。

如果是使用的 Linux 需要啟動和安裝 MQ 映象:

匯入依賴:

        <!--AMQP依賴,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

配置 application.yml 配置:

spring:
  rabbitmq:
    host: 192.168.30.134 # 主機名
    port: 5672 # 埠
    virtual-host: / # 虛擬主機
    username: peng # 使用者名稱
    password: 123456 # 密碼

生產者,也就是訊息傳送者,消費者也就是訊息接收者。

定義消費者,建立 SpringRabbitFanout 類,裡面配置交換機和佇列:

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName 定義交換機
 **/
@Configuration
public class SpringRabbitFanout {
    /**
     * @Description:交換機
     **/
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("peng.fanout");
    }

    /**
     * @Description:佇列1
     **/
    @Bean
    public Queue fanoutQueue1(){
        return new Queue("fanout.queue1");
    }

    /**
     * @Description:佇列和交換機繫結
     **/
    @Bean
    public Binding bindingQueue(Queue fanoutQueue1,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }

    /**
     * @Description:佇列2
     **/
    @Bean
    public Queue fanoutQueue2(){
        return new Queue("fanout.queue2");
    }

    /**
     * @Description:佇列和交換機繫結
     **/
    @Bean
    public Binding bindingQueue2(Queue fanoutQueue2,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }
}

定義 SpringFanout 類接收資料,這裡我定義了 3種:廣播 、路由也叫定向、萬用字元

/**
 * @ClassName 廣播方式接收資料
 **/
@Component
public class SpringFanout {
    //廣播==========================

    @RabbitListener(queues = "fanout.queue1")
    public void listFanoutQueue1(String msg){
        System.out.println("1--廣播佇列接收到:"+msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listFanoutQueue2(String msg){
        System.out.println("2--廣播佇列接收到:"+msg);
    }


    //定向=====================

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange [email protected](name = "peng.direct",type = ExchangeTypes.DIRECT),
            key = {"bey","bylist"}
    ))
    public void listDirectQueue1(String msg){
        System.out.println("1--定向佇列接收到:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name ="peng.direct",type = ExchangeTypes.DIRECT),
            key = {"bey"}
    ))
    public void listDirectQueue2(String msg){
        System.out.println("2--定向佇列接收到:"+msg);
    }


    //萬用字元==========================

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange [email protected](name = "peng.topic",type = ExchangeTypes.TOPIC),
            key = {"bey.#","#.bylist"}
    ))
    public void listTopicQueue1(String msg){
        System.out.println("1--萬用字元佇列接收到:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name ="peng.topic",type = ExchangeTypes.TOPIC),
            key = {"#.bey"}
    ))
    public void listTopicQueue2(String msg){
        System.out.println("2--萬用字元佇列接收到:"+msg);
    }

}

定義測試類傳送資料:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSimpleQueue() {
        //定義佇列
        String dui="peng.queue";
        //資料
        String shu="java,heoll~";

        //傳送資訊
        rabbitTemplate.convertAndSend(dui,shu);
    }

    @Test
    public void testFanoutQueue() {
        //定義佇列
        String dui="peng.fanout1";
        //資料
        String shu="java,heoll~";

            //傳送資訊
            rabbitTemplate.convertAndSend(dui,"bylist",shu);
    }

    @Test
    public void testTopicQueue() {
        //定義佇列
        String dui="peng.topic";
        //資料
        String shu="java,heoll~";

        //傳送資訊
        rabbitTemplate.convertAndSend(dui,"bey.bylist",shu);
    }
}