[英]How to connect spring gcp PubSubTemplate to local instance?
i'm using spring boot starter spring-cloud-gcp-starter-pubsub
and PubSubTemplate
. 我正在使用spring boot starter spring-cloud-gcp-starter-pubsub
和PubSubTemplate
。 it works perfectly with actual GCP but now i want to use it for my tests with local Google PubSub emulator. 它与实际的GCP完美配合,但是现在我想将其用于本地Google PubSub模拟器的测试中。 how can i provide my custom url, port and no credentials? 如何提供我的自定义网址,端口和没有凭据?
or i have to revert back to using plain google sdk as in this example 或者我必须像本例一样恢复使用纯Google sdk
EDIT : 编辑 :
i have only one bean in my app: 我的应用程序中只有一个bean:
@Service
class TestLocalPubsub(pubSubTemplate: PubSubTemplate)
and 和
application.properties: application.properties:
spring.cloud.gcp.pubsub.emulator-host=localhost:8085
and when i run the app i got: 当我运行该应用程序时,我得到:
Caused by: java.io.IOException: The Application Default Credentials are not available. 原因:java.io.IOException:应用程序默认凭据不可用。 They are available if running in Google Compute Engine. 如果它们在Google Compute Engine中运行,则可用。 Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. 否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向指向定义凭据的文件。 See https://developers.google.com/accounts/docs/application-default-credentials for more information. 有关更多信息,请参见https://developers.google.com/accounts/docs/application-default-credentials 。 at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:132) ~[google-auth-library-oauth2-http-0.12.0.jar:na] ... 在com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:132)〜[google-auth-library-oauth2-http-0.12.0.jar:na] ...
but of course the link says about gcloud sdk, not pubsubtemplate 但是当然,链接显示的是gcloud sdk,而不是pubsubtemplate
As mentioned in the post you linked, there are couple ways to get spring to connect to emulator: 如您所链接的文章中所述,有几种方法可以使spring连接到仿真器:
SPRING_CLOUD_GCP_PUBSUB_EMULATORHOST=locahost:8085
(if using the default emulator port 设置环境变量SPRING_CLOUD_GCP_PUBSUB_EMULATORHOST=locahost:8085
(如果使用默认的仿真器端口 spring.cloud.gcp.pubsub.emulator-host=localhost:8085
in your Spring application properties 或在Spring应用程序属性中设置spring.cloud.gcp.pubsub.emulator-host=localhost:8085
For tests, you can use the second method by creating an application.properties in test/main/resources and setting spring.cloud.gcp.pubsub.emulator-host=localhost:8085
. 对于测试,可以通过在test / main / resources中创建application.properties并设置spring.cloud.gcp.pubsub.emulator-host=localhost:8085
来使用第二种方法。 You shouldn't need to override any custom credentials. 您无需覆盖任何自定义凭据。
In order for your tests to work with emulator, you need to create your topics and subscriptions in the emulator ahead of time. 为了使您的测试能够与模拟器一起使用,您需要提前在模拟器中创建主题和订阅。 The GCP docs show how to do this manually by installing Python Google Cloud Client library: https://cloud.google.com/pubsub/docs/emulator . GCP文档显示了如何通过安装Python Google Cloud Client库手动执行此操作: https : //cloud.google.com/pubsub/docs/emulator 。 Otherwise your tests need to create them using the SDK. 否则,您的测试需要使用SDK创建它们。
Here is a complete example of using Spring with pubsub emulator and creating the topic/subscription using Java SDK: https://github.com/nhartner/pubsub-emulator-demo 这是将Spring与pubsub模拟器一起使用并使用Java SDK创建主题/订阅的完整示例: https : //github.com/nhartner/pubsub-emulator-demo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.