1. 程式人生 > 實用技巧 >ES6 promise物件

ES6 promise物件

springboot整合rabbitmq

步驟

1. 匯入相應模組

2. 配置yml檔案

spring:
  application:
    name: springboot-rabbitmq # 工程名

  # 配置rabbitmq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    virtual-host: / # 虛擬主機

第一種hello world模式

一個提供者,一個消費者

  • 編寫提供者
@SpringBootTest
class Spriingboot07RabbitmqApplicationTests {

    //注入rabbitmq
    @Autowired
    RabbitTemplate rabbitTemplate;

    //1. 簡單模式
    @Test
    void hello_world() {
        //傳送訊息
        rabbitTemplate.convertAndSend("hello","你好,這是一條訊息");
    }
}    

  • 編寫消費者
@Component
//指定這個類是一個監聽者,queuesToDeclare代表沒有佇列則建立佇列,
//@Queue建立一個佇列,指定佇列的名字,引數等
//預設是持久化,不自動刪除,不獨佔一個連線
@RabbitListener(queuesToDeclare = @Queue(value = "hello",autoDelete = "false",exclusive = "false"))
public class HelloConsumer {

    @RabbitHandler //代表這是一個回撥函式,能接收佇列中的訊息
    public void resolverMsg(String message) {
        //message就是佇列中的訊息
        System.out.println("message = " + message);
    }

}

第二種work模式

一個提供者,兩個消費者

  • 編寫提供者
@SpringBootTest
class Spriingboot07RabbitmqApplicationTests {

    //注入rabbitmq
    @Autowired
    RabbitTemplate rabbitTemplate;
    
    @Test
    void work_model() {
        //傳送訊息
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","工作第"+(i+1)+"天");
        }
    }
}
  • 編寫消費者
@Component
public class WorkConsumer {

    //消費者1
    @RabbitListener(queuesToDeclare = @Queue(value = "work"))
    public void consumer1(String message) {
        System.out.println("message1 = " + message);
    }

    //消費者2
    @RabbitListener(queuesToDeclare = @Queue(value = "work"))
    public void consumer2(String message) {
        System.out.println("message2 = " + message);
    }

}

第三種釋出訂閱模式

一個提供者,一個交換機,兩個佇列,兩個消費者

  • 編寫提供者
@SpringBootTest
class Spriingboot07RabbitmqApplicationTests {

    //注入rabbitmq
    @Autowired
    RabbitTemplate rabbitTemplate;
    
        //3.廣播模式
    @Test
    void fanout_model() {
        //傳送訊息
        for (int i = 0; i < 10; i++) {
            //廣播模式不需要路由key
            rabbitTemplate.convertAndSend("fanoutExchange","","工作第"+(i+1)+"天");
        }
    }
}
  • 編寫消費者
package com.atguigu.fanout;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * ClassName: FanoutModel
 * Description:
 * date: 2020/9/6 17:26
 *
 * @author July
 * @since JDK 1.8
 */
@Component
public class FanoutModel {

    //建立一個消費者
    @RabbitListener(
            bindings = {@QueueBinding( //將佇列與交換機繫結
                    value = @Queue, //不指定佇列名字則為建立一個臨時佇列
                    exchange = @Exchange(value = "fanoutExchange", type = "fanout") //繫結交換機
            )
            })
    public void consumer1(String message) {
        System.out.println("message1 = " + message);
    }

    //建立消費者2
    @RabbitListener(
            bindings = {@QueueBinding( //將佇列與交換機繫結
                    value = @Queue, //不指定佇列名字則為建立一個臨時佇列
                    exchange = @Exchange(value = "fanoutExchange", type = "fanout") //繫結交換機
            )
            })
    public void consumer2(String message) {
        System.out.println("message2 = " + message);
    }

}

第四種路由模式

一個提供者,一個交換機,帶有路由key,兩個佇列,兩個消費者

  • 編寫提供者
@SpringBootTest
class Spriingboot07RabbitmqApplicationTests {

    //注入rabbitmq
    @Autowired
    RabbitTemplate rabbitTemplate;
   
      //4.路由模式
    @Test
    void route_model() {
        //傳送訊息
        for (int i = 0; i < 10; i++) {
            //傳送路由key 為 queue1的資訊
            rabbitTemplate.convertAndSend("routeExchange","queue1","工作第"+(i+1)+"天");
        }
    }
    
}  
  • 編寫消費者
@Component
public class RouteModel {

    //建立一個消費者
    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue("queue1"), //建立一個名為queue1的佇列
                            exchange = @Exchange(value = "routeExchange", type = ExchangeTypes.DIRECT),
                            //指定路由key,接收{"queue1","info","warn"}相關資訊
                            key = {"queue1","info","warn"}
                    )
            }
    )
    public void consumer1(String message) {
        System.out.println("message = " + message);
    }

    //建立一個消費者
    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue, //建立臨時佇列
                            //指定交換機
                            exchange = @Exchange(value = "routeExchange", type = ExchangeTypes.DIRECT),
                            //指定路由key,接收{"info","warn"}相關資訊
                            key = {"info","warn"}
                    )
            }
    )
    public void consumer2(String message) {
        System.out.println("message2 = " + message);
    }
}

第五種萬用字元模式

在路由key上加了萬用字元,通常用.分割單詞

  • #匹配一個或多個單詞:info.#匹配info.abc.log
  • *只匹配一個單詞:info.*匹配info.log

一個提供者,一個交換機,帶有路由key,兩個佇列,兩個消費者

  • 編寫提供者
@SpringBootTest
class Spriingboot07RabbitmqApplicationTests {

    //注入rabbitmq
    @Autowired
    RabbitTemplate rabbitTemplate;
    
        //5.萬用字元模式
    @Test
    void topics_model() {
        //傳送訊息
        for (int i = 0; i < 10; i++) {
            //傳送路由key 為 queue1的資訊
            rabbitTemplate.convertAndSend("topicsExchange","info.abc.log","工作第"+(i+1)+"天");
        }
    }

}
  • 編寫消費者
package com.atguigu.topics;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * ClassName: TopicsModel
 * Description:
 * date: 2020/9/6 18:05
 *
 * @author July
 * @since JDK 1.8
 */

@Component
public class TopicsModel {
    //消費者1
    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue,
                            //設定為路由模式ExchangeTypes.TOPIC
                            exchange = @Exchange(value = "topicsExchange", type = ExchangeTypes.TOPIC),
                            //接收任何多個單詞.log,abc.一個單詞
                            key = {"*.log", "#.log", "abc.*"}
                    )
            }
    )
    public void consumer1(String message) {
        System.out.println("message1 = " + message);
    }


    //消費者2
    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue,
                            //設定為路由模式ExchangeTypes.TOPIC
                            exchange = @Exchange(value = "topicsExchange", type = ExchangeTypes.TOPIC),
                            //設定路由key規則
                            key = {"*.info.*", "abc.log", "info.#"}
                    )
            }
    )
    public void consumer2(String message) {
        System.out.println("message2 = " + message);
    }

}