<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 使用Google Cloud Pub / Sub進行消息傳遞 本指南將引導您逐步完成使用 在程序的不同部分之間或不同程序之間交換消息的過程 [Spring Integration通道適配器](https://docs.spring.io/spring-integration/reference/htmlsingle/#overview-endpoints-channeladapter) 和 [Google Cloud Pub / Sub](https://cloud.google.com/pubsub/) 作為底層消息交換機制 。 ## 你會建立什么 一個 [Spring Boot](https://spring.io/guides/gs/spring-boot/) Web應用程序,它向自身發送消息并處理這些消息。 ## 你需要什么 * 約15分鐘 * 最喜歡的文本編輯器或IDE * [JDK 1.8](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/) * [啟用了結算和發布/訂閱的Google Cloud Platform項目](https://cloud.google.com/pubsub/docs/quickstart-console) * [Google Cloud SDK](https://cloud.google.com/sdk/) ## 如何完成本指南 像大多數Spring 一樣 [入門指南](https://spring.io/guides) ,您可以從頭開始并完成每個步驟,也可以繞過您已經熟悉的基本設置步驟。 無論哪種方式,您最終都可以使用代碼。 要 **從頭開始** ,請繼續 [使用Gradle構建](https://spring.io/guides/gs/messaging-gcp-pubsub/#scratch) 。 要 **跳過基礎知識** ,請執行以下操作: * [下載](https://github.com/spring-guides/gs-messaging-gcp-pubsub/archive/master.zip) 并解壓縮本指南的源存儲庫,或使用 對其進行克隆 [Git](https://spring.io/understanding/Git) : `git clone [https://github.com/spring-guides/gs-messaging-gcp-pubsub.git](https://github.com/spring-guides/gs-messaging-gcp-pubsub.git)` * 光盤進入 `gs-messaging-gcp-pubsub/initial` * 繼續 [添加所需的依賴項](https://spring.io/guides/gs/messaging-gcp-pubsub/#initial) 。 **完成后** ,您可以根據中的代碼檢查結果 `gs-messaging-gcp-pubsub/complete`. ## 用Gradle構建 ## 用Maven編譯 ## 使用您的IDE進行構建 ## 添加所需的依賴項 將以下內容添加到您的 `pom.xml` 文件,如果您使用的是Maven: ~~~ <dependencies> ... <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-gcp-starter-pubsub</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> </dependency> ... </dependencies> ~~~ 或者,如果您使用的是Gradle: ~~~ dependencies { ... compile("org.springframework.cloud:spring-cloud-gcp-starter-pubsub:1.2.5.RELEASE") compile("org.springframework.integration:spring-integration-core") ... } ~~~ 如果您使用的是Maven,還強烈建議您使用Spring Cloud GCP物料清單來控制依賴項的版本: ~~~ <properties> ... <spring-cloud-gcp.version>1.2.5.RELEASE</spring-cloud-gcp.version> ... </properties> <dependencyManagement> <dependencies> ... <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-gcp-dependencies</artifactId> <version>${spring-cloud-gcp.version}</version> <type>pom</type> <scope>import</scope> </dependency> ... </dependencies> </dependencyManagement> ~~~ ## 設置Google Cloud Pub / Sub環境 您需要一個主題和一個訂閱,才能從Google Cloud Pub / Sub發送和接收消息。 您可以在 創建它們,也可以 [Google Cloud Console中](https://console.cloud.google.com/cloudpubsub) 使用 `PubSubAdmin` 班級。 在本練習中,創建一個名為“ testTopic”的主題,并為該主題創建一個名為“ testSubscription”的訂閱。 ## 創建應用程序文件 您將需要一個類來包括通道適配器和消息傳遞配置。 與Spring Boot應用程序一樣,使用@SpringBootApplication標頭創建PubSubApplication類。 `src/main/java/hello/PubSubApplication.java` ~~~ @SpringBootApplication public class PubSubApplication { public static void main(String[] args) throws IOException { SpringApplication.run(PubSubApplication.class, args); } } ~~~ 此外,由于您正在構建Web應用程序,因此請創建WebAppController類以在控制器和配置邏輯之間進行分隔。 `src/main/java/hello/WebAppController.java` ~~~ @RestController public class WebAppController { } ~~~ 我們仍然缺少HTML和屬性的兩個文件。 `src/main/resources/static/index.html` ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Spring Integration GCP sample</title> </head> <body> <div name="formDiv"> <form action="/publishMessage" method="post"> Publish message: <input type="text" name="message" /> <input type="submit" value="Publish!"/> </form> </div> </body> </html> ~~~ `src/main/resources/application.properties` ~~~ #spring.cloud.gcp.project-id=[YOUR_GCP_PROJECT_ID_HERE] #spring.cloud.gcp.credentials.location=file:[LOCAL_FS_CREDENTIALS_PATH] ~~~ Spring Cloud GCP Core Boot啟動器可以自動配置這兩個屬性并使它們成為可選屬性。 屬性文件中的屬性始終優先于Spring Boot配置。 Spring Cloud GCP Core Boot啟動器與Spring Cloud GCP Pub / Sub Boot啟動器捆綁在一起。 GCP項目ID是通過以下方式自動配置的: `GOOGLE_CLOUD_PROJECT`環境變量 [等](https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java#L287) 。 OAuth2憑據是通過 自動配置的 [GOOGLE\_APPLICATION\_CREDENTIALS](https://developers.google.com/identity/protocols/application-default-credentials) 環境變量 。 如果 了 [Google Cloud SDK](https://cloud.google.com/sdk/) 安裝 ,則可以通過運行 `gcloud auth application-default login`在與應用相同的過程中(或在父過程中)命令。 ## 創建入站通道適配器 入站通道適配器偵聽來自Google Cloud Pub / Sub訂閱的消息,并將其發送到應用程序中的Spring通道。 實例化入站通道適配器需要一個 `PubSubTemplate` 實例和現有訂閱的名稱。 `PubSubTemplate`是Spring訂閱Google Cloud Pub / Sub主題的抽象。 Spring Cloud GCP發布/訂閱啟動啟動器提供了自動配置的 `PubSubTemplate` 您可以簡單地將其作為方法參數注入的實例。 `src/main/java/hello/PubSubApplication.java` ~~~ @Bean public PubSubInboundChannelAdapter messageChannelAdapter( @Qualifier("pubsubInputChannel") MessageChannel inputChannel, PubSubTemplate pubSubTemplate) { PubSubInboundChannelAdapter adapter = new PubSubInboundChannelAdapter(pubSubTemplate, "testSubscription"); adapter.setOutputChannel(inputChannel); adapter.setAckMode(AckMode.MANUAL); return adapter; } ~~~ 默認情況下,消息確認模式在適配器中設置為自動。 如示例所示,此行為可能會被覆蓋。 實例化通道適配器后,必須配置適配器將接收到的消息發送到的輸出通道。 `src/main/java/hello/PubSubApplication.java` ~~~ @Bean public MessageChannel pubsubInputChannel() { return new DirectChannel(); } ~~~ 連接到入站通道的是服務激活器,用于處理傳入消息。 `src/main/java/hello/PubSubApplication.java` ~~~ @Bean @ServiceActivator(inputChannel = "pubsubInputChannel") public MessageHandler messageReceiver() { return message -> { LOGGER.info("Message arrived! Payload: " + new String((byte[]) message.getPayload())); BasicAcknowledgeablePubsubMessage originalMessage = message.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE, BasicAcknowledgeablePubsubMessage.class); originalMessage.ack(); }; } ~~~ 這 `ServiceActivator` 輸入頻道名稱(例如, `"pubsubInputChannel"`)必須與輸入通道方法名稱匹配。 每當有新消息到達該通道時,返回的消息都會對其進行處理 `MessageHandler`. 在此示例中,僅通過記錄其正文并確認該消息即可對其進行處理。 在手動確認中,使用 `BasicAcknowledgeablePubsubMessage` 對象,該對象附加到 `Message` 標頭,可以使用 `GcpPubSubHeaders.ORIGINAL_MESSAGE` 鑰匙。 ## 創建一個出站通道適配器 出站通道適配器偵聽來自Spring通道的新消息,并將其發布到Google Cloud Pub / Sub主題。 實例化出站通道適配器需要一個 `PubSubTemplate` 以及現有主題的名稱。 `PubSubTemplate`是Spring的抽象概念,用于將消息發布到Google Cloud Pub / Sub主題。 Spring Cloud GCP發布/訂閱啟動啟動器提供了自動配置的 `PubSubTemplate`實例。 `src/main/java/hello/PubSubApplication.java` ~~~ @Bean @ServiceActivator(inputChannel = "pubsubOutputChannel") public MessageHandler messageSender(PubSubTemplate pubsubTemplate) { return new PubSubMessageHandler(pubsubTemplate, "testTopic"); } ~~~ 您可以使用 `MessageGateway` 將消息寫入頻道并將其發布到Google Cloud Pub / Sub。 `src/main/java/hello/PubSubApplication.java` ~~~ @MessagingGateway(defaultRequestChannel = "pubsubOutputChannel") public interface PubsubOutboundGateway { void sendToPubsub(String text); } ~~~ 通過此代碼,Spring自動生成一個對象,然后可以將該對象自動連接到應用程序的私有字段中。 `src/main/java/hello/WebAppController.java` ~~~ @Autowired private PubsubOutboundGateway messagingGateway; ~~~ ## 添加控制器邏輯 向控制器添加邏輯,使您可以寫入Spring通道: `src/main/java/hello/WebAppController.java` ~~~ package hello; import hello.PubSubApplication.PubsubOutboundGateway; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.view.RedirectView; @RestController public class WebAppController { // tag::autowireGateway[] @Autowired private PubsubOutboundGateway messagingGateway; // end::autowireGateway[] @PostMapping("/publishMessage") public RedirectView publishMessage(@RequestParam("message") String message) { messagingGateway.sendToPubsub(message); return new RedirectView("/"); } } ~~~ ## 驗證 您的應用程序必須通過GOOGLE\_APPLICATION\_CREDENTIALS環境變量或通過 `spring.cloud.gcp.credentials.location` 財產。 如果您安裝了 [Google Cloud SDK](https://cloud.google.com/sdk/) ,則可以使用 `gcloud auth application-default login` 命令。 或者,您可以從 下載服務帳戶憑據文件, [Google Cloud Console](https://cloud.google.com/console) 然后將其指向 `spring.cloud.gcp.credentials.location` 物業 `application.properties` 歸檔到它。 作為 [春季資源](https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/resources.html) , `spring.cloud.gcp.credentials.location` 也可以從文件系統以外的其他位置獲得,例如URL,類路徑等。 ## 使應用程序可執行 盡管可以將該服務打包為傳統的 [WAR](https://spring.io/understanding/WAR) 文件以部署到外部應用程序服務器,但是下面演示的更簡單的方法創建了一個獨立的應用程序。 您將所有內容打包到一個由Java驅動的單個可執行JAR文件中 `main()`方法。 另外,您使用Spring的支持將 嵌入 [Tomcat](https://spring.io/understanding/Tomcat) servlet容器作為HTTP運行時 ,而不是部署到外部實例。 `@SpringBootApplication` 是一個方便注釋,它添加了以下所有內容: * `@Configuration`:將類標記為應用程序上下文的Bean定義的源。 * `@EnableAutoConfiguration`:告訴Spring Boot根據類路徑設置,其他bean和各種屬性設置開始添加bean。 例如,如果 `spring-webmvc` 在類路徑上,此注釋將應用程序標記為Web應用程序并激活關鍵行為,例如設置 `DispatcherServlet`. * `@ComponentScan`:告訴Spring在服務器中尋找其他組件,配置和服務 `hello` 包,讓它找到控制器。 這 `main()` 方法使用Spring Boot的 `SpringApplication.run()`啟動應用程序的方法。 您是否注意到沒有一行XML? 沒有 `web.xml`文件。 該Web應用程序是100%純Java,因此您無需處理任何管道或基礎結構。 ### 建立可執行的JAR 您可以使用Gradle或Maven從命令行運行該應用程序。 您還可以構建一個包含所有必需的依賴項,類和資源的可執行JAR文件,然后運行該文件。 生成可執行jar使得在整個開發生命周期中,跨不同環境等等的情況下,都可以輕松地將服務作為應用程序進行發布,版本控制和部署。 如果您使用Gradle,則可以通過使用以下命令運行該應用程序 `./gradlew bootRun`。 或者,您可以通過使用以下命令構建JAR文件: `./gradlew build` 然后運行JAR文件,如下所示: ~~~ java -jar build/libs/gs-messaging-gcp-pubsub-0.1.0.jar ~~~ 如果您使用Maven,則可以通過使用以下命令運行該應用程序 `./mvnw spring-boot:run`。 或者,您可以使用以下命令構建JAR文件: `./mvnw clean package` 然后運行JAR文件,如下所示: ~~~ java -jar target/gs-messaging-gcp-pubsub-0.1.0.jar ~~~ 此處描述的步驟將創建可運行的JAR。 您還可以 構建經典的WAR文件 。 顯示日志記錄輸出。 該服務應在幾秒鐘內啟動并運行。 ## 測試應用程序 現在該應用程序正在運行,您可以對其進行測試。 打開 [http:// localhost:8080](http://localhost:8080) ,在輸入文本框中鍵入一條消息,然后按“發布!” 按鈕并確認消息已正確記錄在您的過程終端窗口中。 ## 概括 恭喜你! 您剛剛開發了一個Spring應用程序,該應用程序使用Spring Integration GCP發布/訂閱通道適配器來交換消息!
                  <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>

                              哎呀哎呀视频在线观看