<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 使用RabbitMQ進行消息傳遞 本指南將引導您完成設置RabbitMQ AMQP服務器以發布和訂閱消息的過程,并創建一個Spring Boot應用程序與該RabbitMQ服務器進行交互的過程。 ## 你會建立什么 您將使用Spring AMQP構建一個發布消息的應用程序 `RabbitTemplate` 并通過使用以下命令訂閱POJO上的消息 `MessageListenerAdapter`. ## 你需要什么 * 約15分鐘 * 最喜歡的文本編輯器或IDE * [JDK 11](http://www.oracle.com/technetwork/java/javase/downloads/index.html) 或更高版本 * [Gradle 4+](http://www.gradle.org/downloads) 或 [Maven 3.2+](https://maven.apache.org/download.cgi) * 您還可以將代碼直接導入到IDE中: * [彈簧工具套件(STS)](https://spring.io/guides/gs/sts) * [IntelliJ IDEA](https://spring.io/guides/gs/intellij-idea/) * 設置RabbitMQ服務器。 請參閱 [設置RabbitMQ Broker](https://spring.io/guides/gs/messaging-rabbitmq/#scratch) 。 ## 如何完成本指南 像大多數Spring 一樣 [入門指南](https://spring.io/guides) ,您可以從頭開始并完成每個步驟,也可以繞過您已經熟悉的基本設置步驟。 無論哪種方式,您最終都可以使用代碼。 要 **從頭開始** ,請繼續 [設置RabbitMQ Broker](https://spring.io/guides/gs/messaging-rabbitmq/#scratch) 。 要 **跳過基礎知識** ,請執行以下操作: * [下載](https://github.com/spring-guides/gs-messaging-rabbitmq/archive/master.zip) 并解壓縮本指南的源存儲庫,或使用 對其進行克隆 [Git](https://spring.io/understanding/Git) : `git clone [https://github.com/spring-guides/gs-messaging-rabbitmq.git](https://github.com/spring-guides/gs-messaging-rabbitmq.git)` * 光盤進入 `gs-messaging-rabbitmq/initial` * 跳到 [從Spring Initializr開始](https://spring.io/guides/gs/messaging-rabbitmq/#initial) 。 **完成后** ,您可以根據中的代碼檢查結果 `gs-messaging-rabbitmq/complete`. ## 設置RabbitMQ經紀人 在構建消息傳遞應用程序之前,需要設置服務器以處理接收和發送消息。 RabbitMQ是AMQP服務器。 該服務器可從 免費獲得 [https://www.rabbitmq.com/download.html](https://www.rabbitmq.com/download.html) 。 您可以手動下載它,或者,如果您將Mac與Homebrew一起使用,則可以在終端窗口中運行以下命令來下載它: ~~~ brew install rabbitmq ~~~ 通過在終端窗口中運行以下命令來打開服務器包裝并使用默認設置啟動它: ~~~ rabbitmq-server ~~~ 您應該看到類似于以下內容的輸出: ~~~ RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc. ## ## Licensed under the MPL. See https://www.rabbitmq.com/ ## ## ########## Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log ###### ## /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log ########## Starting broker... completed with 6 plugins. ~~~ 也可以使用 [Docker, Docker Compose](https://docs.docker.com/compose/) 如果您在本地運行 快速啟動RabbitMQ服務器。 有一個 `docker-compose.yml` 在 `complete`Github中的項目。 這很簡單,如下面的清單所示: ~~~ rabbitmq: image: rabbitmq:management ports: - "5672:5672" - "15672:15672" ~~~ 使用此文件在當前目錄中,您可以運行 `docker-compose up` 使RabbitMQ在容器中運行。 ## 從Spring Initializr開始 如果您使用Maven,請訪問 [Spring Initializr](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=messaging-rabbitmq&name=messaging-rabbitmq&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.messaging-rabbitmq&dependencies=amqp) 以生成具有所需依賴項的新項目(Spring for RabbitMQ)。 以下清單顯示了 `pom.xml` 選擇Maven時創建的文件: ~~~ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>messaging-rabbitmq</artifactId> <version>0.0.1-SNAPSHOT</version> <name>messaging-rabbitmq</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ~~~ 如果您使用Gradle,請訪問 [Spring Initializr](https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=messaging-rabbitmq&name=messaging-rabbitmq&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.messaging-rabbitmq&dependencies=amqp) 以生成具有所需依賴項的新項目(Spring for RabbitMQ)。 以下清單顯示了 `build.gradle` 選擇Gradle時創建的文件: ~~~ plugins { id 'org.springframework.boot' version '2.4.3' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-amqp' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.amqp:spring-rabbit-test' } test { useJUnitPlatform() } ~~~ ### 手動初始化(可選) 如果要手動初始化項目而不是使用前面顯示的鏈接,請按照以下步驟操作: 1. 導航到 [https://start.spring.io](https://start.spring.io) 。 該服務提取應用程序所需的所有依賴關系,并為您完成大部分設置。 2. 選擇Gradle或Maven以及您要使用的語言。 本指南假定您選擇了Java。 3. 單擊 **Dependencies** 并 選擇 **為RabbitMQ Spring** 。 4. 點擊 **生成** 。 5. 下載生成的ZIP文件,該文件是使用您的選擇配置的Web應用程序的存檔。 如果您的IDE集成了Spring Initializr,則可以從IDE中完成此過程。 ## 創建RabbitMQ消息接收器 對于任何基于消息傳遞的應用程序,您需要創建一個響應已發布消息的接收器。 以下清單(來自 `src/main/java/com.example.messagingrabbitmq/Receiver.java`)顯示了如何執行此操作: ~~~ package com.example.messagingrabbitmq; import java.util.concurrent.CountDownLatch; import org.springframework.stereotype.Component; @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; } } ~~~ 這 `Receiver`是一個POJO,它定義了一種接收消息的方法。 注冊它以接收消息時,可以隨意命名。 為方便起見,此POJO還具有一個 CountDownLatch。 這使其發出已接收到該消息的信號。 這是您不太可能在生產應用程序中實現的東西。 ## 注冊偵聽器并發送消息 春季AMQP `RabbitTemplate`提供使用RabbitMQ發送和接收消息所需的一切。 但是,您需要: * 配置消息偵聽器容器。 * 聲明隊列,交換以及它們之間的綁定。 * 配置組件以發送一些消息以測試偵聽器。 Spring Boot自動創建一個連接工廠和一個RabbitTemplate,從而減少了您必須編寫的代碼量。 您將使用 `RabbitTemplate` 發送消息,您將注冊一個 `Receiver`與消息偵聽器容器一起接收消息。 連接工廠既驅動這兩個驅動器,又使它們連接到RabbitMQ服務器。 以下清單(來自 `src/main/java/com.example.messagingrabbitmq/MessagingRabbitApplication.java`)展示了如何創建應用程序類: ~~~ package com.example.messagingrabbitmq; 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 MessagingRabbitmqApplication { static final String topicExchangeName = "spring-boot-exchange"; static final String queueName = "spring-boot"; @Bean Queue queue() { return new Queue(queueName, false); } @Bean TopicExchange exchange() { return new TopicExchange(topicExchangeName); } @Bean Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#"); } @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) throws InterruptedException { SpringApplication.run(MessagingRabbitmqApplication.class, args).close(); } } ~~~ `@SpringBootApplication` 是一個方便注釋,它添加了以下所有內容: * `@Configuration`:將類標記為應用程序上下文的Bean定義的源。 * `@EnableAutoConfiguration`:告訴Spring Boot根據類路徑設置,其他bean和各種屬性設置開始添加bean。 例如,如果 `spring-webmvc` 在類路徑上,此注釋將應用程序標記為Web應用程序并激活關鍵行為,例如設置 `DispatcherServlet`. * `@ComponentScan`:告訴Spring在服務器中尋找其他組件,配置和服務 `com/example` 包,讓它找到控制器。 這 `main()` 方法使用Spring Boot的 `SpringApplication.run()`啟動應用程序的方法。 您是否注意到沒有一行XML? 沒有 `web.xml`文件。 該Web應用程序是100%純Java,因此您無需處理任何管道或基礎結構。 在中定義的bean `listenerAdapter()` 方法被注冊為容器中的消息偵聽器(在 `container()`)。 它在 `spring-boot`隊列。 因為 `Receiver` 類是一個POJO,需要包裝在 `MessageListenerAdapter`,在其中指定它調用的位置 `receiveMessage`. JMS隊列和AMQP隊列具有不同的語義。 例如,JMS僅將排隊的消息發送給一個使用者。 盡管AMQP隊列執行相同的操作,但AMQP生產者不會將消息直接發送到隊列。 而是將消息發送到交換機,該交換機可以轉到單個隊列,也可以扇出到多個隊列,以模擬JMS主題的概念。 消息偵聽器容器和接收者Bean就是您偵聽消息所需的全部。 要發送消息,您還需要一個Rabbit模板。 這 `queue()`方法創建一個AMQP隊列。 這 `exchange()`方法創建主題交流。 這 `binding()` 方法將這兩者綁定在一起,定義了在以下情況下發生的行為 `RabbitTemplate` 發布到交易所。 Spring AMQP要求 Queue, 這 TopicExchange和 Binding 被聲明為頂級Spring Bean,以便正確設置。 在這種情況下,我們使用主題交換,并且隊列與路由鍵綁定 `foo.bar.#`,這意味著任何以以下開頭的路由鍵發送的郵件 `foo.bar.` 被路由到隊列。 ## 發送測試信息 在此示例中,測試消息是通過以下方式發送的: `CommandLineRunner`,它也等待接收器中的閂鎖并關閉應用程序上下文。 以下清單(來自 `src/main/java/com.example.messagingrabbitmq/Runner.java`)顯示其工作原理: ~~~ package com.example.messagingrabbitmq; import java.util.concurrent.TimeUnit; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class Runner implements CommandLineRunner { private final RabbitTemplate rabbitTemplate; private final Receiver receiver; public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) { this.receiver = receiver; this.rabbitTemplate = rabbitTemplate; } @Override public void run(String... args) throws Exception { System.out.println("Sending message..."); rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!"); receiver.getLatch().await(10000, TimeUnit.MILLISECONDS); } } ~~~ 請注意,該模板使用以下路由鍵將消息路由到交換機 `foo.bar.baz`,它與綁定匹配。 在測試中,您可以模擬運行器,以便可以單獨測試接收器。 ## 運行應用程序 這 `main()`方法通過創建Spring應用程序上下文來啟動該過程。 這將啟動消息偵聽器容器,該容器開始偵聽消息。 有一個 `Runner`bean,然后自動運行。 它檢索 `RabbitTemplate` 從應用程序上下文中發送一個 `Hello from RabbitMQ!` 上的訊息 `spring-boot`隊列。 最后,它關閉Spring應用程序上下文,然后應用程序結束。 ## 建立可執行的JAR 您可以使用Gradle或Maven從命令行運行該應用程序。 您還可以構建一個包含所有必需的依賴項,類和資源的可執行JAR文件,然后運行該文件。 生成可執行jar使得在整個開發生命周期中,跨不同環境等等的情況下,都可以輕松地將服務作為應用程序進行發布,版本控制和部署。 如果您使用Gradle,則可以通過使用以下命令運行該應用程序 `./gradlew bootRun`。 或者,您可以通過使用以下命令構建JAR文件: `./gradlew build` 然后運行JAR文件,如下所示: ~~~ java -jar build/libs/gs-messaging-rabbitmq-0.1.0.jar ~~~ 如果您使用Maven,則可以通過使用以下命令運行該應用程序 `./mvnw spring-boot:run`。 或者,您可以使用以下命令構建JAR文件: `./mvnw clean package` 然后運行JAR文件,如下所示: ~~~ java -jar target/gs-messaging-rabbitmq-0.1.0.jar ~~~ 此處描述的步驟將創建可運行的JAR。 您還可以 構建經典的WAR文件 。 您應該看到以下輸出: ~~~ Sending message... Received <Hello from RabbitMQ!> ~~~ ## 概括 恭喜你! 您剛剛使用Spring和RabbitMQ開發了一個簡單的發布和訂閱應用程序。 使用 可以做的事情 [Spring和RabbitMQ](https://docs.spring.io/spring-amqp/reference/#_introduction) 比這里介紹的要多,但是本指南應該為您提供一個良好的開端。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看