一、生產(chǎn)者模塊
1.導入依賴
重點是這個依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 聲明springboot的版本號 -->
<spring-boot.version>2.2.9.RELEASE</spring-boot.version>
</properties>
<!-- 引入springboot官方提供的所有依賴的版本號定義,如果項目中使用相關依賴,可以不必寫版本號了-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
2.yml配置文件
spring:
rabbitmq:
host: 8.140.244.227
port: 6786
username: test
password: test
virtual-host: /test
server:
port: 8081
3.用接口方式實現(xiàn)生產(chǎn)者
通過
@Autowired RabbitTemplate rabbitTemplate;//這個模板
package com.qf.bootmq2302.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@RestController
public class TestController {
@Autowired
RabbitTemplate rabbitTemplate;
@GetMapping("/test1")
public String test1(String msg){
System.out.println(msg);
String exchangeName = "";//默認交換機
String routingkey = "hello";//隊列名字
//生產(chǎn)者發(fā)送消息
rabbitTemplate.convertAndSend(exchangeName,routingkey,msg);
return "ok";
}
@GetMapping("/test2")
public String test2(String name,Integer age){
TreeMap<String, Object> map = new TreeMap<String, Object>();
map.put("name",name);
map.put("age",age);
String exchangeName = "";//默認交換機
String routingkey = "work";//隊列名字
//生產(chǎn)者發(fā)送消息
rabbitTemplate.convertAndSend(exchangeName,routingkey,map);
return "ok";
}
}
4.主啟動類
package com.qf.bootmq2302;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootMqApp {
public static void main(String[] args) {
SpringApplication.run(BootMqApp.class,args);
}
}
二、消費者模塊
? 1.導入依賴
? ? ? ? ? ? ?和上一個一樣文章來源:http://www.zghlxwxcb.cn/news/detail-700533.html
?? 2.yml配置文件
spring:
rabbitmq:
host: 8.140.244.227
port: 6786
username: test
password: test
virtual-host: /test
#手動ACK
listener:
simple:
acknowledge-mode: manual
prefetch: 1 #等價于basicQos(1)
3.通過注解綁定 隊列名字
package com.qf.bootconsumer.consumer;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@Component
public class MyConsumer {
// @RabbitListener(queues = "hello")
// public void getMsg(Message message) throws UnsupportedEncodingException {
// byte[] body = message.getBody();
// String s = new String(body, "utf-8");
// System.out.println(s);
//
// }
// @RabbitListener(queues = "hello")
// public void getMsg(String msg) throws UnsupportedEncodingException {
//
// System.out.println(msg);
//
// }
@RabbitListener(queues = "hello")
public void getMsg(Map<String,Object> message) throws UnsupportedEncodingException {
System.out.println(message);
}
@RabbitListener(queues = "work")
public void getMsg1(Map<String,Object> data, Channel channel,Message message) throws IOException {
System.out.println(data);
//手動ack//若開啟手動ack,不給手動ack,就按照 prefetch: 1 #等價于basicQos(1)的量,就這么多,不會多給你了,因為你沒有確認。確認一條,就給你一條
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
}
}
4.通過配置類,創(chuàng)建隊列,交換機,綁定隊列給交換機和給予路由
?文章來源地址http://www.zghlxwxcb.cn/news/detail-700533.html
package com.qf.bootconsumer.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 在 該配置類中可以,通過@Bean 方法定義 mq內(nèi)部的交換機和隊列 ,及其綁定關系
*/
@Configuration
public class MyConfig {
@Bean
public TopicExchange exchange01(){
return new TopicExchange("boot-exchange",true,false);
}
@Bean
public Queue queue01(){
/**
* 第一個參數(shù):隊列名字
* 第二個參數(shù):true:代表服務重啟后,此隊列還存在
* 第三個參數(shù): true:排外,不能讓其他連接來訪問此隊列,只有創(chuàng)建此隊列的連接能訪問消費此隊列
* 第四個參數(shù): true:代表服務關閉時,RabbitMQ會自動把 此隊列刪除了。
*/
Queue queue = new Queue("boot-Queue", true, false, false);
return queue;
}
@Bean
public Binding binding01(TopicExchange exchange01,Queue queue01){
Binding binding = BindingBuilder.bind(queue01).to(exchange01).with("*.orange.*");
return binding;
}
@Bean
public Binding binding02(TopicExchange exchange01,Queue queue01){
Binding binding = BindingBuilder.bind(queue01).to(exchange01).with("*.*.rabbit");
return binding;
}
@Bean
public FanoutExchange exchange02(){
return new FanoutExchange("boot-fanout");
}
@Bean
public Queue queue02(){
return new Queue("boot-queue02",true,false,false);
}
@Bean
public Queue queue03(){
return new Queue("boot-queue03",true,false,false);
}
@Bean
public Binding binding03(FanoutExchange exchange02,Queue queue02){
Binding binding = BindingBuilder.bind(queue02).to(exchange02);
return binding;
}
@Bean
public Binding binding04(FanoutExchange exchange02,Queue queue03){
Binding binding = BindingBuilder.bind(queue03).to(exchange02);
return binding;
}
}
到了這里,關于RabbitMQ: SpringBoot 整合 RabbitMQ的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!