如何使用在本地系统上运行的Spring Boot应用程序连接到AWS上的DynamoDB?

时间:2019-03-19 06:59:40

标签: java amazon-web-services spring-boot amazon-dynamodb microservices

我正在研究基于Spring Boot微服务REST的应用程序,backedn数据库当前是本地Dynamodb数据库。我可以使用Postman对本地运行的应用程序进行REST调用,并可以使用本地dynamodb数据库执行CRUD操作。 现在,我想更改配置,以便每当我在本地运行的应用程序上执行类似的CRUD操作时,都应在运行于AWS而非本地的实际DynamoDB上保存/更新数据。基本上,我想将应用程序指向远程DynamoDB端点。可能吗?是否有样本参考指南可用于进行此类配置更改?

当前,这是我的配置类和属性文件:-

package com.user.profile.jpa;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.util.StringUtils;
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

//@Profile("dev")
@Configuration
@EnableDynamoDBRepositories(basePackages = "com.user.profile.jpa")
public class DynamoDbConfig {

    @Value("${amazon.dynamodb.endpoint}")
    private String amazonDynamoDBEndpoint;

    @Value("${amazon.dynamodb.region}")
    private String amazonDynamoDBRegion;

    @Value("${amazon.aws.accesskey}")
    private String accessKey;

    @Value("${amazon.aws.secretkey}")
    private String secretKey;

    @Bean
    public AmazonDynamoDB amazonDynamoDB() {
        AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(amazonAWSCredentials());

        if (!StringUtils.isNullOrEmpty(amazonDynamoDBEndpoint)) {
            dynamoDB.setEndpoint(amazonDynamoDBEndpoint);
        }

        return dynamoDB;
    }

    @Bean
    public AWSCredentials amazonAWSCredentials() {
        return new BasicAWSCredentials(accessKey, secretKey);
    }
}



amazon.dynamodb.endpoint=http://localhost:8000/
amazon.aws.accesskey=JGDSGJSGS8729224
amazon.aws.secretkey=HJKAHDD83ERWRnHAHDJASD8782rlGb0F1k2Ijg
amazon.dynamodb.region="us-east-1"

spring.main.allow-bean-definition-overriding=true
spring.profiles.active=dev

server.port=8090

请协助。谢谢

3 个答案:

答案 0 :(得分:1)

您是否尝试将dynamoDB端点设置为正确的系统,请参见https://docs.aws.amazon.com/general/latest/gr/rande.html

在您的情况下,可能是dynamodb.us-east-1.amazonaws.com

您最好使用正确的accessKey和secretAccessKey。

答案 1 :(得分:1)

只需更改端点(对远程而言为空)和凭据/密钥:

amazon.dynamodb.endpoint=[leave empty if using AWS, or http://localhost:[dynamodb port] if using local ]
amazon.aws.accesskey=[your AWS access key if using AWS or arbitrary text if using local]
amazon.aws.secretkey=[your AWS secret key if using AWS or arbitrary text if using local]

您在这里有一些示例: https://tech.smartling.com/getting-started-with-amazon-dynamodb-and-java-universal-language-850fa1c8a902

答案 2 :(得分:0)

要从本地系统连接到AWS上的DynamoDB,请使用以下配置。下面的示例连接到US_EAST_1区域。替换为运行DynamoDb的aws区域。

@Configuration
@EnableDynamoDBRepositories(basePackageClasses = YourRepository.class)
public class DynamoDBConfiguration {

    @Value("${aws.access.key}")
    private String awsAccessKey;
    
    @Value("${aws.secret.key}")
    private String awsSecretKey;

    @Bean
    public AmazonDynamoDB amazonDynamoDB() {
        return AmazonDynamoDBClientBuilder.standard()
            .withCredentials(awsCredentialsProvider())
            .withRegion(Regions.US_EAST_1).build();
    }
    
    private AWSCredentialsProvider awsCredentialsProvider(){
        return new AWSStaticCredentialsProvider(
               new BasicAWSCredentials(awsAccessKey, awsSecretKey));
    }

}

将以下属性添加到您的application.properties

aws.access.key = <YOUR_AWS_ACCESS_KEY>
aws.secret.key = <YOUR_AWS_SECRET_KEY>

注意:仅当您的IAM角色有权使用上述凭据访问服务时,以上代码才有效。

如果要在aws EC2实例中部署微服务,则就像用aws访问DynamoDB。在大多数情况下,它将使用相同的aws帐户和相同的aws区域。在这种情况下,您无需提供任何详细信息。请参阅下面的配置类。这就是您所需要的。

import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.company.project.dao.YourRepository;

@Configuration
@EnableDynamoDBRepositories(basePackageClasses = YourRepository.class)
public class DynamoDBConfiguration {
    
    @Bean
    public AmazonDynamoDB amazonDynamoDB() {
        return AmazonDynamoDBClientBuilder.standard().build();
    }

}

但是当区域更改时,则必须在如下所示的配置中传递区域值。

@Configuration
@EnableDynamoDBRepositories(basePackageClasses = YourRepository.class)
public class DynamoDBConfiguration {

    @Bean
    public AmazonDynamoDB amazonDynamoDB() {
        return AmazonDynamoDBClientBuilder.standard()
            .withRegion(Regions.US_EAST_1).build();
    }

}

关于授权,将不需要accessKey和secretKey。必须在EC2端配置IAM角色才能访问DynamoDB实例。