簡體   English   中英

如何在 Spring Boot 中連接 Amazon QLDB 數據庫?

[英]How to connect Amazon QLDB database in Spring boot?

我在 AWS qLdB 中有一個基於分類帳的數據庫“演示”。 所以我想通過 Spring Boot Web 應用程序連接到該數據庫。

首先,我授予用戶對 QLDB 的權限,例如

在此處輸入圖片說明

然后我將以下 maven 依賴項添加到 pom.xml 中。

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-qldb -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-qldb</artifactId>
            <version>1.12.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/software.amazon.ion/ion-java -->
        <dependency>
            <groupId>software.amazon.ion</groupId>
            <artifactId>ion-java</artifactId>
            <version>1.5.1</version>
        </dependency>

        <dependency>
            <groupId>software.amazon.qldb</groupId>
            <artifactId>amazon-qldb-driver-java</artifactId>
            <version>2.3.1</version>
        </dependency>

在 Application.yml 中,我保留了這些屬性。

cloud:
  aws:
    credentials:
      access-key: 
      secret-key: 
    region: US East (Ohio)

然后我創建一個配置類來連接到 QLDB 之類的。

@Configuration
public class QldbConfigs {
    public static IonSystem ionSys = IonSystemBuilder.standard().build();
    public static QldbDriver qldbDriver;

    @Bean
    public void setupDb(){
        System.out.println("Initializing the driver");
        qldbDriver = QldbDriver.builder()
                .ledger("demo")
                .transactionRetryPolicy(RetryPolicy
                        .builder()
                        .maxRetries(3)
                        .build())
                .sessionClientBuilder(QldbSessionClient.builder())
                .build();
    }
}

如果我運行這個,它會給出一個異常

software.amazon.awssdk.core.exception.SdkClientException:無法從鏈中的任何提供商加載區域 software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@6076c66:[software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider @57b1ec84:無法從系統設置加載區域。 區域必須通過環境變量 (AWS_REGION) 或系統屬性 (aws.region) 指定。,software.amazon.awssdk.regions.providers.AwsProfileRegionProvider@5a82bc58:配置文件中未提供區域:默認,software.amazon.awssdk.regions .providers.InstanceProfileRegionProvider@5d37aa0f:無法聯系 EC2 元數據服務。]

我該如何解決這個問題?

AWS 開發工具包將在一組預定義的位置查找一些憑證,以便在連接時提供給服務。 根據Spring Boot 文檔

啟用 Spring Boot 屬性 cloud.aws.region.auto 后,Spring Cloud for AWS 可以根據您的環境或堆棧自動檢測到這一點。

您還可以為您的應用程序以靜態方式設置區域:

cloud:
 aws:
   region:
     static: eu-central-1
     auto: false

這與您在示例中配置區域的方式不同。 為了完全確定,您可以直接在QldbSessionClient.builder上設置區域,如下所示。

qldbDriver = QldbDriver.builder()
  .ledger("demo")
  .transactionRetryPolicy(RetryPolicy
    .builder()
    .maxRetries(3)
    .build())
  .sessionClientBuilder(
    QldbSessionClient.builder().region(Region.of("us-east-1")))
  .build();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM