GCP Pub/Sub | 向区域端点发布消息

huangapple go评论66阅读模式
英文:

GCP Pub/Sub | Publish Messages to a regional endpoint

问题

我正在使用基于spring-boot的应用程序向GCP主题发布消息。我想利用消息排序,因此我想将消息发布到区域性端点。

我已经启用了消息排序,使用 spring.cloud.gcp.pubsub.publisher.enable-message-ordering=true 并使用 spring.cloud.gcp.pubsub.publisher.endpoint=us-east1-pubsub.googleapis.com:443 来发布消息到特定区域。

然而,在发布时我遇到了以下错误。

com.google.api.gax.rpc.UnauthenticatedException: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.

然而,当我不使用 spring.cloud.gcp.pubsub.publisher.endpoint=us-east1-pubsub.googleapis.com:443 并将消息发布到全局端点时,就没有异常。

我在这里漏掉了什么?

依赖项:

<dependency>
 <groupId>com.google.cloud</groupId>
 <artifactId>spring-cloud-gcp-starter</artifactId>
</dependency>
<dependency>
 <groupId>com.google.cloud</groupId>
 <artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
</dependency>

<dependencyManagement>
 <dependencies>
  <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>spring-cloud-gcp-dependencies</artifactId>
   <version>3.5.0</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
 </dependencies>
</dependencyManagement>
英文:

I am using spring-boot based app to publish messages to a GCP topic. I want to leverage the message ordering and hence I want to publish the messages to a regional endpoint.

I have enabled the message ordering using spring.cloud.gcp.pubsub.publisher.enable-message-ordering=true and using spring.cloud.gcp.pubsub.publisher.endpoint=us-east1-pubsub.googleapis.com:443 to publish messages to a specific region.

However, I am getting the following error while publishing.

com.google.api.gax.rpc.UnauthenticatedException: io.grpc.StatusRuntimeException: UNAUTHENTICATED: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.

However, when I do not use spring.cloud.gcp.pubsub.publisher.endpoint=us-east1-pubsub.googleapis.com:443 and publish messages to a global endpoint instead, there is no exception.

What am I missing here?

Dependencies:

&lt;dependency&gt;
 &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
 &lt;artifactId&gt;spring-cloud-gcp-starter&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
 &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
 &lt;artifactId&gt;spring-cloud-gcp-starter-pubsub&lt;/artifactId&gt;
&lt;/dependency&gt;

&lt;dependencyManagement&gt;
 &lt;dependencies&gt;
  &lt;dependency&gt;
   &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
   &lt;artifactId&gt;spring-cloud-gcp-dependencies&lt;/artifactId&gt;
   &lt;version&gt;3.5.0&lt;/version&gt;
   &lt;type&gt;pom&lt;/type&gt;
   &lt;scope&gt;import&lt;/scope&gt;
  &lt;/dependency&gt;
 &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;

答案1

得分: 0

我在参考其他帖子和GitHub问题时找到了解决方案。

凭证提供程序没有发布消息到区域端点所需的范围。我们需要明确添加必要的范围。请参见下面的示例。

  @Bean
  public CredentialsProvider credentialsProvider() {
    return () ->
        ServiceAccountCredentials.fromStream(Files.newInputStream(Paths.get(credentialsFilePath)))
            .createScoped(PublisherStubSettings.getDefaultServiceScopes());
  }

参考

  1. https://stackoverflow.com/questions/74200414/google-pub-sub-api-gets-oauth2-authentication-failure-when-trying-to-connect-to/74210618
  2. https://github.com/googleapis/java-pubsub/issues/930
英文:

I have found the solution while referring to other threads and GitHub issues.

The credentials provider does not have the necessary scopes to publish a message to a regional endpoint. We need to add the necessary scopes explicitly. See below.

  @Bean
  public CredentialsProvider credentialsProvider() {
    return () -&gt;
        ServiceAccountCredentials.fromStream(Files.newInputStream(Paths.get(credentialsFilePath)))
            .createScoped(PublisherStubSettings.getDefaultServiceScopes());
  }

Reference

  1. https://stackoverflow.com/questions/74200414/google-pub-sub-api-gets-oauth2-authentication-failure-when-trying-to-connect-to/74210618
  2. https://github.com/googleapis/java-pubsub/issues/930

huangapple
  • 本文由 发表于 2023年6月27日 18:37:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定