SpringCloud分布式微服務(wù)b2b2c電子商務(wù)(十三)Springboot整合RabbitMQ

這篇文章帶你了解怎么整合RabbitMQ服務(wù)器,并且通過它怎么去發(fā)送和接收消息。我將構(gòu)建一個(gè)springboot工程,通過

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供云縣網(wǎng)站建設(shè)、云縣做網(wǎng)站、云縣網(wǎng)站設(shè)計(jì)、云縣網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、云縣企業(yè)網(wǎng)站模板建站服務(wù),十年云縣做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

RabbitTemplate去通過MessageListenerAdapter去訂閱一個(gè)POJO類型的消息。

準(zhǔn)備工作

15min

IDEA

maven 3.0

在開始構(gòu)建項(xiàng)目之前,機(jī)器需要安裝rabbitmq,你可以去官網(wǎng)下載,http://www.rabbitmq.com/download.html ,如果

你是用的Mac(程序員都應(yīng)該用mac吧),你可以這樣下載:

brew install rabbitmq

安裝完成后開啟服務(wù)器:

rabbitmq-server

開啟服務(wù)器成功,你可以看到以下信息:

brew install rabbitmq
安裝完成后開啟服務(wù)器:
rabbitmq-server
開啟服務(wù)器成功,你可以看到以下信息:

構(gòu)建工程
構(gòu)架一個(gè)SpringBoot工程,其pom文件依賴加上spring-boot-starter-amqp的起步依賴:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

創(chuàng)建消息接收者
在任何的消息隊(duì)列程序中,你需要?jiǎng)?chuàng)建一個(gè)消息接收者,用于響應(yīng)發(fā)送的消息。

@Component
public class Receiver {
    private CountDownLatch latch = new CountDownLatch(1);
    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }
    public CountDownLatch getLatch() {
        return latch;
    }
}

消息接收者是一個(gè)簡單的POJO類,它定義了一個(gè)方法去接收消息,當(dāng)你注冊它去接收消息,你可以給它取任何的名字。了解springcloud架構(gòu)可以加求求:三五三六二四七二五九。其中,它有CountDownLatch這樣的一個(gè)類,它是用于告訴發(fā)送者消息已經(jīng)收到了,你不需要在應(yīng)用程序中具體實(shí)現(xiàn)它,只需要latch.countDown()就行了。

創(chuàng)建消息監(jiān)聽,并發(fā)送一條消息

在spring程序中,RabbitTemplate提供了發(fā)送消息和接收消息的所有方法。你只需簡單的配置下就行了:

需要一個(gè)消息監(jiān)聽容器

聲明一個(gè)quene,一個(gè)exchange,并且綁定它們

一個(gè)組件去發(fā)送消息

代碼清單如下:

package com.forezp;
import com.forezp.message.Receiver;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringbootRabbitmqApplication {
     final static String queueName = "spring-boot";
    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("spring-boot-exchange");
    }
    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }
    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }
    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringbootRabbitmqApplication.class, args);
    }
}

當(dāng)前名稱:SpringCloud分布式微服務(wù)b2b2c電子商務(wù)(十三)Springboot整合RabbitMQ
當(dāng)前鏈接:http://muchs.cn/article34/ihjjse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站設(shè)計(jì)公司、網(wǎng)站內(nèi)鏈、域名注冊網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)