您好,登錄后才能下訂單哦!
rabbitMq 是什么?
RabbitMQ是實(shí)現(xiàn)了高級(jí)消息隊(duì)列協(xié)議(AMQP)的開(kāi)源消息代理軟件(亦稱(chēng)面向消息的中間件)。RabbitMQ服務(wù)器是用Erlang語(yǔ)言編寫(xiě)的,而群集和故障轉(zhuǎn)移是構(gòu)建在開(kāi)放電信平臺(tái)框架上的。所有主要的編程語(yǔ)言均有與代理接口通訊的客戶(hù)端庫(kù)。
rabbitMq 可以做什么?
消息系統(tǒng)允許軟件、應(yīng)用相互連接和擴(kuò)展.這些應(yīng)用可以相互鏈接起來(lái)組成一個(gè)更大的應(yīng)用,或者將用戶(hù)設(shè)備和數(shù)據(jù)進(jìn)行連接.消息系統(tǒng)通過(guò)將消息的發(fā)送和接收分離來(lái)實(shí)現(xiàn)應(yīng)用程序的異步和解偶.
或許你正在考慮進(jìn)行數(shù)據(jù)投遞,非阻塞操作或推送通知?;蛟S你想要實(shí)現(xiàn)發(fā)布/訂閱,異步處理,或者工作隊(duì)列。所有這些都可以通過(guò)消息系統(tǒng)實(shí)現(xiàn)。
RabbitMQ是一個(gè)消息代理 - 一個(gè)消息系統(tǒng)的媒介。它可以為你的應(yīng)用提供一個(gè)通用的消息發(fā)送和接收平臺(tái),并且保證消息在傳輸過(guò)程中的安全。
如何使用:
spring boot 使用消息隊(duì)列
pom 引入依賴(lài)
<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>
application.yml
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
配置queue
@Configuration
public class RabbitConfig {
//queue
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
@Bean
public Queue topicMessage() {
return new Queue("topic.message");
}
@Bean
public Queue fanoutQueue() {
return new Queue("fanout.A");
}
//exchange
@Bean
TopicExchange exchange() {
return new TopicExchange("exchange");
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
//bind
//綁定單個(gè)topicExchange
@Bean
Binding bindingExchangeMessage(Queue topicMessage, TopicExchange exchange) {
return BindingBuilder.bind(topicMessage).to(exchange).with("topic.message");
}
//綁定多個(gè)topicExchange
@Bean
Binding bindingExchangeMessages(Queue topicMessage, TopicExchange exchange) {
return BindingBuilder.bind(topicMessage).to(exchange).with("topic.#");
}
//綁定單個(gè) fanoutExchange
@Bean
Binding bindingFanoutExchange(Queue fanoutQueue ,FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);
}
}
3.配置消費(fèi)者
@Component
@RabbitListener(queues= {"hello"})
public class HelloReceive {
@RabbitHandler
public void helloProcess(String message){
System.out.println(message);
}
}
配置生生產(chǎn)者
@Component
public class HelloSender {
@Autowired
AmqpTemplate amqpTemplate;
public void sendHello(String message){
amqpTemplate.convertAndSend("hello",message);
}
}
5 .測(cè)試接口
@RestController
@RequestMapping("/rabbit")
public class RabbitController {
@Autowired
HelloSender helloSender;
@RequestMapping("/hello")
public Object helloRabbit(String message){
helloSender.sendHello(message);
return "success";
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。