簡體   English   中英

如何將Spring gcp PubSubTemplate連接到本地實例?

[英]How to connect spring gcp PubSubTemplate to local instance?

我正在使用spring boot starter spring-cloud-gcp-starter-pubsubPubSubTemplate 它與實際的GCP完美配合,但是現在我想將其用於本地Google PubSub模擬器的測試中。 如何提供我的自定義網址,端口和沒有憑據?

或者我必須像本例一樣恢復使用純Google sdk


編輯

我的應用程序中只有一個bean:

@Service
class TestLocalPubsub(pubSubTemplate: PubSubTemplate)

application.properties:

spring.cloud.gcp.pubsub.emulator-host=localhost:8085

當我運行該應用程序時,我得到:

原因:java.io.IOException:應用程序默認憑據不可用。 如果它們在Google Compute Engine中運行,則可用。 否則,必須定義環境變量GOOGLE_APPLICATION_CREDENTIALS,指向指向定義憑據的文件。 有關更多信息,請參見https://developers.google.com/accounts/docs/application-default-credentials 在com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:132)〜[google-auth-library-oauth2-http-0.12.0.jar:na] ...

但是當然,鏈接顯示的是gcloud sdk,而不是pubsubtemplate

如您所鏈接的文章中所述,有幾種方法可以使spring連接到仿真器:

  • 設置環境變量SPRING_CLOUD_GCP_PUBSUB_EMULATORHOST=locahost:8085 (如果使用默認的仿真器端口
  • 或在Spring應用程序屬性中設置spring.cloud.gcp.pubsub.emulator-host=localhost:8085

對於測試,可以通過在test / main / resources中創建application.properties並設置spring.cloud.gcp.pubsub.emulator-host=localhost:8085來使用第二種方法。 您無需覆蓋任何自定義憑據。

為了使您的測試能夠與模擬器一起使用,您需要提前在模擬器中創建主題和訂閱。 GCP文檔顯示了如何通過安裝Python Google Cloud Client庫手動執行此操作: https : //cloud.google.com/pubsub/docs/emulator 否則,您的測試需要使用SDK創建它們。

這是將Spring與pubsub模擬器一起使用並使用Java SDK創建主題/訂閱的完整示例: https : //github.com/nhartner/pubsub-emulator-demo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM