代码之家  ›  专栏  ›  技术社区  ›  donm

Spring Boot cloud GCP无法连接到本地Google PubSub仿真器

  •  0
  • donm  · 技术社区  · 6 年前

    正在尝试从Spring启动应用程序连接到本地Google PubSub emulator进行测试。

    使用下面的配置
    spring.cloud.gcp.pubsub.emulator-host=localhost:8085

    在8085上成功地在本地启动了emulator,还设置了 PUBSUB_EMULATOR_HOST=localhost:8085

    2 回复  |  直到 6 年前
        1
  •  2
  •   neildo    6 年前
    1. src/test/resources/application.properties
    2. 设置 spring.cloud.gcp.pubsub.emulator-host=localhost:8085
    3. 注释您的测试类,以便Spring获取您的测试应用程序。特性:
    @RunWith(SpringRunner::class)
    @SpringBootTest
    

    编辑:我创建了一个示例项目,展示了如何在测试中使用Spring和PubSub emulator(这也需要创建主题和订阅): https://github.com/nhartner/pubsub-emulator-demo

        2
  •  1
  •   MrtN    6 年前

    使用发布/订阅仿真器时,请使用 FixedTransportChannelProvider NoCredentialsProvider 创建 Publisher Subscriber 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();