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

Spring boot Spring Boot cloud GCP无法连接到本地Google PubSub仿真器,spring-boot,google-cloud-pubsub,Spring Boot,Google Cloud Pubsub,正在尝试从Spring启动应用程序连接到本地Google PubSub emulator进行测试 使用下面的配置 spring.cloud.gcp.pubsub.emulator主机=localhost:8085 在8085上成功地在本地启动了emulator,还设置了 PUBSUB\u EMULATOR\u HOST=localhost:8085 注意:当连接到实际的Google PubSub主题时,一切正常。当使用Pub/Sub模拟器时,使用FixedTransportChannelProv

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

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

在8085上成功地在本地启动了emulator,还设置了
PUBSUB\u EMULATOR\u HOST=localhost:8085


注意:当连接到实际的Google PubSub主题时,一切正常。

当使用Pub/Sub模拟器时,使用
FixedTransportChannelProvider
NoRedentialsProvider
创建
发布者
订户
。这表现在:

  • src/test/resources/application.properties下创建一个文件
  • 在test application.properties中设置
    spring.cloud.gcp.pubsub.emulator host=localhost:8085
  • 注释您的测试类,以便Spring选择您的测试应用程序。属性:
  • 编辑:我创建了一个示例项目,展示了如何在测试中使用Spring和PubSub emulator(这也需要创建主题和订阅):

    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();
    
    @RunWith(SpringRunner::class)
    @SpringBootTest