简体   繁体   中英

How to connect Amazon QLDB database in Spring boot?

I have a ledger-based database "demo" inside AWS qLdB. So I want to connect to that database through the Spring boot web application.

First of all, I gave user permissions to QLDB like

在此处输入图片说明

Then I added the following maven dependencies to pom.

<!-- 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.

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

Then I create a config class to connect to QLDB like.

@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. 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.]

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. According to the Spring Boot documentation :

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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