Spring Boot cloud GCP cannot connect to local Google PubSub emulator

孤街醉人 提交于 2020-01-06 07:26:49

问题


Trying to connect to local Google PubSub emulator from Spring boot application for tests.

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

Started emulator in local successfully on 8085, Also have set PUBSUB_EMULATOR_HOST=localhost:8085

Note: While connecting to the actual Google PubSub topic everything just works fine.


回答1:


When using the Pub/Sub emulator, use the FixedTransportChannelProvider and NoCredentialsProvider to create a Publisher or Subscriber. This is demonstrated in UsePubSubEmulatorSnippet.java:

String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext(true).build();
TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));

CredentialsProvider credentialsProvider = NoCredentialsProvider.create();

ProjectTopicName topicName = ...

// Use the channel and credentials provider when creating a Publisher or Subscriber.
Publisher publisher =
    Publisher.newBuilder(topicName)
        .setChannelProvider(channelProvider)
        .setCredentialsProvider(credentialsProvider)
        .build();



回答2:


  1. Create a file under src/test/resources/application.properties
  2. Set spring.cloud.gcp.pubsub.emulator-host=localhost:8085 inside the test application.properties.
  3. Annotate your test class so that Spring picks up your test application.properties:
@RunWith(SpringRunner::class)
@SpringBootTest

EDIT: I created an example project that shows how to use Spring with PubSub emulator in a test (which also requires creating the topic and subscription): https://github.com/nhartner/pubsub-emulator-demo



来源:https://stackoverflow.com/questions/51290024/spring-boot-cloud-gcp-cannot-connect-to-local-google-pubsub-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!