简体   繁体   中英

How to connect spring gcp PubSubTemplate to local instance?

i'm using spring boot starter spring-cloud-gcp-starter-pubsub and PubSubTemplate . it works perfectly with actual GCP but now i want to use it for my tests with local Google PubSub emulator. 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


EDIT :

i have only one bean in my app:

@Service
class TestLocalPubsub(pubSubTemplate: PubSubTemplate)

and

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. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information. at 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

As mentioned in the post you linked, there are couple ways to get spring to connect to emulator:

  • Set an environment variable SPRING_CLOUD_GCP_PUBSUB_EMULATORHOST=locahost:8085 (if using the default emulator port
  • Or set spring.cloud.gcp.pubsub.emulator-host=localhost:8085 in your Spring application properties

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 . 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 . Otherwise your tests need to create them using the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM