从 PCF 连接 pub/sub 时出现问题

问题描述 投票:0回答:1

我正在尝试从 PCF 环境(Spring boot 应用程序)连接 google Pub/Sub。应用程序已成功从credentials.json 文件加载凭据并打印项目ID。但启动后打印以下错误。

c.g.a.oauth2.ComputeEngineCredentials:无法检测我们是否在 Google Compute Engine 上运行。 错误 13 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] 在路径 [] 的上下文中抛出异常 [请求处理失败;嵌套异常是 java.lang.IllegalStateException:预期服务 InnerService [FAILED] 正在运行,但服务已 FAILED] 且根本原因 java.io.IOException:应用程序默认凭据不可用。如果在 Google Compute Engine 中运行,则它们可用。否则,必须定义环境变量 GOOGLE_APPLICATION_CREDENTIALS 指向定义凭据的文件。请参阅 https://developers.google.com/accounts/docs/application-default-credentials 了解更多信息。

这是我正在使用的代码,我从示例休息控制器调用此方法。

public static void subscribeAsyncExample(String projectId, String subscriptionId) {
ProjectSubscriptionName subscriptionName =
    ProjectSubscriptionName.of(projectId, subscriptionId);
System.out.println("Processing messages...");
MessageReceiver receiver =
    (message, consumer) -> {
      System.out.println("Id : " + message.getMessageId());
      System.out.println("Data : " + message.getData().toStringUtf8());
      consumer.ack();
    };
Subscriber subscriber = null;
try {
  subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
  subscriber.startAsync().awaitRunning();
  subscriber.awaitTerminated();
} finally {
  if (subscriber != null) {
    subscriber.stopAsync();
  }
}

}

pom.xml

Spring 启动:2.3.2.RELEASE spring-cloud-gcp.版本:1.1.3.RELEASE

`

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>


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



<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-storage</artifactId>
</dependency>

`

日志: 默认项目 ID 为 ***** 服务帐户的默认凭据提供程序 *****-client-account@********.gserviceaccount.com 默认凭据使用的范围:[https://www.googleapis.com/auth/pubsub、https://www.googleapis.com/auth/trace.append、https://www.googleapis.com/auth/ spanner.admin,https://www.googleapis.com/auth/cloudruntimeconfig,https://www.googleapis.com/auth/sqlservice.admin,https://www.googleapis.com/auth/devstorage.read_only, https://www.googleapis.com/auth/devstorage.read_write,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/spanner.data,https: //www.googleapis.com/auth/datastore,https://www.googleapis.com/auth/cloud-vision]

spring-boot google-cloud-pubsub pcf
1个回答
0
投票

使用此依赖项列表,您还必须添加身份验证依赖项,请使用以下列表:

<dependency>
    <groupId>com.google.auth</groupId>
    <artifactId>google-auth-library-oauth2-http</artifactId>
    <version>1.23.0</version>
</dependency>

<dependency>
    <groupId>com.google.auth</groupId>
    <artifactId>google-auth-library-credentials</artifactId>
    <version>1.23.0</version>
</dependency>
<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-pubsub</artifactId>
    <version>1.128.1</version>
</dependency>
<dependency>
<groupId>com.google.api</groupId>
<artifactId>api-common</artifactId>
<version>2.29.1</version>
© www.soinside.com 2019 - 2024. All rights reserved.