英文:
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:
<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>
答案1
得分: 0
我在参考其他帖子和GitHub问题时找到了解决方案。
凭证提供程序没有发布消息到区域端点所需的范围。我们需要明确添加必要的范围。请参见下面的示例。
@Bean
public CredentialsProvider credentialsProvider() {
return () ->
ServiceAccountCredentials.fromStream(Files.newInputStream(Paths.get(credentialsFilePath)))
.createScoped(PublisherStubSettings.getDefaultServiceScopes());
}
参考
- https://stackoverflow.com/questions/74200414/google-pub-sub-api-gets-oauth2-authentication-failure-when-trying-to-connect-to/74210618
- 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 () ->
ServiceAccountCredentials.fromStream(Files.newInputStream(Paths.get(credentialsFilePath)))
.createScoped(PublisherStubSettings.getDefaultServiceScopes());
}
Reference
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论