简体   繁体   English

如何在 Spring Boot 中连接 Amazon QLDB 数据库?

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

I have a ledger-based database "demo" inside AWS qLdB.我在 AWS qLdB 中有一个基于分类帐的数据库“演示”。 So I want to connect to that database through the Spring boot web application.所以我想通过 Spring Boot Web 应用程序连接到该数据库。

First of all, I gave user permissions to QLDB like首先,我授予用户对 QLDB 的权限,例如

在此处输入图片说明

Then I added the following maven dependencies to pom.然后我将以下 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>

In Application.yml i kept these properties.在 Application.yml 中,我保留了这些属性。

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

Then I create a config class to connect to QLDB like.然后我创建一个配置类来连接到 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();
    }
}

If I run this, it is giving an exception like如果我运行这个,它会给出一个异常

software.amazon.awssdk.core.exception.SdkClientException: Unable to load region from any of the providers in the chain software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@6076c66: [software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider@57b1ec84: Unable to load region from system settings. software.amazon.awssdk.core.exception.SdkClientException:无法从链中的任何提供商加载区域 software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@6076c66:[software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider @57b1ec84:无法从系统设置加载区域。 Region must be specified either via environment variable (AWS_REGION) or system property (aws.region)., software.amazon.awssdk.regions.providers.AwsProfileRegionProvider@5a82bc58: No region provided in profile: default, software.amazon.awssdk.regions.providers.InstanceProfileRegionProvider@5d37aa0f: Unable to contact EC2 metadata service.]区域必须通过环境变量 (AWS_REGION) 或系统属性 (aws.region) 指定。,software.amazon.awssdk.regions.providers.AwsProfileRegionProvider@5a82bc58:配置文件中未提供区域:默认,software.amazon.awssdk.regions .providers.InstanceProfileRegionProvider@5d37aa0f:无法联系 EC2 元数据服务。]

How can I fix this issue?我该如何解决这个问题?

The AWS SDK will look in a set of predefined places to find some credentials to supply to the service when it connects. AWS 开发工具包将在一组预定义的位置查找一些凭证,以便在连接时提供给服务。 According to the Spring Boot documentation :根据Spring Boot 文档

Spring Cloud for AWS can automatically detect this based on your environment or stack once you enable the Spring Boot property cloud.aws.region.auto.启用 Spring Boot 属性 cloud.aws.region.auto 后,Spring Cloud for AWS 可以根据您的环境或堆栈自动检测到这一点。

You can also set the region in a static fashion for your application:您还可以为您的应用程序以静态方式设置区域:

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

Which is different from the way you are configuring your region in your example.这与您在示例中配置区域的方式不同。 To be completely sure, you can set the region directly on the QldbSessionClient.builder as follows.为了完全确定,您可以直接在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