简体   繁体   English

如何将Spring gcp PubSubTemplate连接到本地实例?

[英]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-pubsubPubSubTemplate 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连接到仿真器:

  • Set an environment variable SPRING_CLOUD_GCP_PUBSUB_EMULATORHOST=locahost:8085 (if using the default emulator port 设置环境变量SPRING_CLOUD_GCP_PUBSUB_EMULATORHOST=locahost:8085 (如果使用默认的仿真器端口
  • Or set 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.

相关问题 如何在 Spring Boot Dockerised 应用程序中连接到特定的本地 MongoDB 实例? - How to connect to specific local MongoDB instance in Spring Boot Dockerised application? 如何创建 PubSubTemplate 的多个 beans - How to create multiple beans of PubSubTemplate 如何连接到在 GCP 虚拟机实例上运行的 Grakn 服务器 - How to connect to a Grakn Server running on a GCP VM instance 如何在 Java Spring 引导中从本地计算机连接到 Ignite 集群? - How to connect to Ignite cluster from Local machine in Java Spring boot? 套接字实例不会连接到本地服务器 - Socket instance wont connect to local server Spring数据ElasticSearch无法连接到ElasticSearch实例 - Spring data elasticsearch unable to connect to elasticsearch instance Spring boot 和 GCP - 使用 spring-cloud-gcp-starter-sql-postgresql 连接 Cloud SQL 实例尝试 SSL 并且它延迟启动 - Spring boot and GCP - Connecting Cloud SQL instance with spring-cloud-gcp-starter-sql-postgresql tries SSL and it delays to start Spring cron 工作在本地工作但不在亚马逊实例中工作 - Spring cron job working in local but not in amazon instance 如何更改spring cloud gcp的依赖版本 - How to change dependency version of spring cloud gcp 如何使用Spring连接到Tomcat上的多实例队列管理器? - How do I connect to a Multi-Instance Queue Manager on Tomcat using Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM