繁体   English   中英

如何禁用谷歌云平台集成?

[英]How to disable Google Cloud Platform integration?

我的 POM 文件中有这两个依赖项:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-trace</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-gcp-starter-logging</artifactId>
</dependency>

我想在某些配置文件中禁用这些 GCP 功能。 我需要在本地测试我的应用程序,但 GCP 一直在妨碍我。

Spring 在设置应用程序时依赖于自动配置。 在许多情况下,它会扫描类路径中的某些依赖项,如果它们存在,则会执行自动配置。 大多数情况下,可以通过提供某个条件来绕过自动配置。

在遍历 Spring cloud gcp 模块时,我遇到了StackdriverLoggingAutoConfiguration类 ( source ) 和StackdriverTraceAutoConfiguration ( source )。

StackdriverLoggingAutoConfiguration 有条件ConditionalOnProperty(value="spring.cloud.gcp.logging.enabled", matchIfMissing=true) ,而 StackdriverTraceAutoConfiguration 有条件@ConditionalOnProperty(value="spring.cloud.gcp.trace.enabled", matchIfMissing=true)

我不完全确定这些属性是否与您使用的模块的自动配置有关,但您可以通过将以下内容添加到您的 application-{localprofile}.properties 来禁用日志记录:

spring.cloud.gcp.logging.enabled=false
spring.cloud.gcp.trace.enabled=false

因为问题是

如何禁用 Google Cloud Platform 集成

我建议只更改我们的配置

spring:
  cloud:
    gcp:
      core:
        enabled: false

在我们的项目中禁用与 Spring Cloud GCP 相关的所有内容就足够了

接受的答案告诉您如何禁用我们项目中使用的部分 GCP 功能。 如果您有很多(日志记录、PubSub、存储...),禁用所有这些可能会很麻烦。 这是一次禁用所有这些的快捷方式;-)

您可以禁用跟踪、日志记录并提供一个假 ID,如下所示:

spring.cloud.gcp.project-id=fake-project-id
spring.cloud.gcp.logging.enabled=false
spring.cloud.gcp.trace.enabled=false

我知道它很旧,但您也可以通过注释禁用 AutoConfiguration 类。

举个例子:

@EnableAutoConfiguration(exclude = {GcpStorageAutoConfiguration.class})

我希望有一天能帮助别人:)

如果要禁用 gcp pubsud,则需要添加
spring.cloud.gcp.core.enabled=false
spring.cloud.gcp.pubsub.enabled=false

只是作为一个思考的食物。

如果您正在构建一个可以在不同环境中工作的应用程序(例如在我的情况下 - 本地,或在具有 GCP 资源的 GCP 上) - 只需避免使用spring-cloud-gcp-starter即可。

没有办法用这些来properly管理你的应用程序的生命周期,因为它们都是静态初始化的,并且总是在 GCP 模式下工作,或者没有它。 而已。

相反,我建议考虑使用良好的旧 Google 库(fe https://cloud.google.com/storage/docs/reference/libraries )构建您自己的GcpFileProvidergcp-whatever-provider ,而不是spring-starter库。

这种方法:

  1. 除了仅在需要时使用它们之外,无论如何都可以为您省去管理这些配置的痛苦。
  2. 使您免于以这种或另一种方式通过这些静态初始化进行黑客攻击(完全可行,但恕我直言,不值得付出努力,并且会导致非常简单的事情设置非常复杂)。
  3. 如果需要,您可以选择即时切换提供商。

最后一点实际上可以很好地帮助集成测试。

附言“我只希望它在 GCP 上运行 OOB ”的所有便利都可以完美地与这些谷歌库一起使用,就像它们与 spring 一样。

GCP Bucket reader 的小示例,在初始化时乐观地获取可用凭证,与 spring-libs 一样:

Gradle:

dependencies {
...
    implementation platform('com.google.cloud:libraries-bom:26.2.0')
    implementation 'com.google.cloud:google-cloud-storage'

}

供应商:

import com.google.cloud.storage.*;

...
    // .getDefaultInstance() actually does all the bootstrap for GCP services that you might need.
    Storage storage = StorageOptions.getDefaultInstance().getService();

    Blob blob = getStorage().get(BlobId.of(bucketName, itemSubPathAndName));
    ReadChannel reader = blob.reader();
    InputStream inputStream = Channels.newInputStream(reader);
...

然后,在我的测试中,我可以轻松地即时切换配置文件,甚至确保集成测试仅在 GCP 配置可用且有效时运行:

通用/本地:

...
@ActiveProfiles("localResources")
...
public class SomeLocalOrGenericTests {
...
}

GCP/集成:

...
@ActiveProfiles("gcpResources")
...
public class SomeIntegrationTest {
...
    private Boolean gcpCredsAreValid = false;
    @BeforeAll
    public void setup() {
        var project = "UNKNOWN";
        try {
            project = ((UserCredentials) GoogleCredentials.getApplicationDefault()).getQuotaProjectId();
            if (!project.isEmpty() && !project.contains("test")  && !project.equals("UNKNOWN")) {
                gcpCredsAreValid = true;
            }
        } catch (Exception e) {
            Logger.getGlobal().log(Level.SEVERE, String.format("gcpCredsAreValid is false. ProjectID is: %s%n", project));
        }
    }

    // And then

    @Test
    public void testingThatSomethingIsAvailableAtGcpBucket() {
        Assumptions.assumeTrue(gcpCredsAreValid, "Skipping GCP tests as GCP credentials are invalid");
        // Actual test of integrations, that will reliably run only if GCP is configured, and available
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM