rabbitmq學習記錄3
阿新 • • 發佈:2021-08-19
rabbitmq學習記錄
1.topic 主題模式
rounting key滿足如下規則
單詞.單詞
可以代表一個任意單詞 #可以代表多個任意單詞
當一個佇列的繫結鍵為#就變成fanout
當一個佇列沒有#和繫結型別就是direct
public class EmitLogTopic { public static final String EXCHANGE_NAME = "topic_logs"; public static void main(String[] args) throws Exception{ Channel channel = RabbitMqUtils.getChannel(); /** * */ Map<String,String> bindingKeyMap= new HashMap<String,String>(); bindingKeyMap.put("quick.orange.rabbit","被佇列Q1Q2接收到了"); bindingKeyMap.put("lazy.orange.elephant","被佇列Q1Q2接收到"); bindingKeyMap.put("quick.orange.fox","被佇列Q1接收到"); bindingKeyMap.put("lazy.brown.fox","被佇列Q2接收到"); bindingKeyMap.put("lazy.pink.rabbit","雖然滿足兩個繫結但只被佇列Q2接受一次"); bindingKeyMap.put("quick.brown.fox","不匹配任何繫結不會被任何佇列接受到,會被丟棄"); bindingKeyMap.put("quick.orange.nale.rabbit","是四個單詞,不匹配任何繫結,會被丟棄"); bindingKeyMap.put("lazy.orange.male.rabbit","是四個單詞但匹配Q2"); //高階迴圈 for (Map.Entry<String, String> bindkingKeyEntry : bindingKeyMap.entrySet()) { String routingKey = bindkingKeyEntry.getKey(); String message = bindkingKeyEntry.getValue(); channel.basicPublish(EXCHANGE_NAME,routingKey,null,message.getBytes(StandardCharsets.UTF_8)); System.out.println("生產者傳送訊息"); } } }
public class ReceiveLogsTopic02 { //交換機名稱 public static final String EXCHANGE_NAME = "topic_logs"; public static void main(String[] args) throws Exception{ Channel channel = RabbitMqUtils.getChannel(); //宣告交換機 channel.exchangeDeclare(EXCHANGE_NAME,"topic"); //宣告佇列 String queueName = "Q2"; /** * 1.名稱 * 2.持久化 * 3.共享 * 4.自動刪除 * 5。引數 */ channel.queueDeclare(queueName,false,false,false,null); channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit"); channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#"); System.out.println("等待接收訊息"); //接收訊息 DeliverCallback deliverCallback = (consumerTag, message) -> { System.out.println("控制檯1列印接收到的訊息" + new String(message.getBody())); System.out.println("接收佇列:" + queueName + "繫結鍵" + message.getEnvelope().getRoutingKey()); }; channel.basicConsume(queueName,true,deliverCallback,consumerTag ->{}); } }
Springboot與rabbitmq整合
新增依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> </dependencies>
server:
port: 8021
spring:
#應用的名字,在微服務中比較重要
application:
name: demo2
#rabbitmq的資訊
rabbitmq:
host: 202.200.231.14
port: 15672
username:
password:
Springboot中提供母版物件,rabbittemplate,使用時直接在專案中注入即可使用。
rabbitmq整合springboot
引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
配置檔案
server:
port: 8021
spring:
#給專案來個名字
application:
name: rabbitmq-provider
#配置rabbitMq 伺服器
rabbitmq:
host:
port:
username:
password:
virtual-host: /ems