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

我正在开发基于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;

    }


湖上湖
浏览 259回答 3
3回答

慕桂英546537

您是否尝试过将 dynamoDB 终端节点设置为正确的系统,请参阅 https://docs.aws.amazon.com/general/latest/gr/rande.html。在您的情况下,它可能是.dynamodb.us-east-1.amazonaws.com您也可以使用正确的访问密钥和秘密访问密钥。

繁星淼淼

只需更改端点(对于远程数据库为空)和凭据/密钥: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

胡说叔叔

要从本地系统连接到 AWS 上的 DynamoDB,请使用以下配置。下面的示例连接到US_EAST_1区域。替换为运行 DynamoDb 的 aws 区域。@Configuration@EnableDynamoDBRepositories(basePackageClasses = YourRepository.class)public class DynamoDBConfiguration {&nbsp; &nbsp; @Value("${aws.access.key}")&nbsp; &nbsp; private String awsAccessKey;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @Value("${aws.secret.key}")&nbsp; &nbsp; private String awsSecretKey;&nbsp; &nbsp; @Bean&nbsp; &nbsp; public AmazonDynamoDB amazonDynamoDB() {&nbsp; &nbsp; &nbsp; &nbsp; return AmazonDynamoDBClientBuilder.standard()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .withCredentials(awsCredentialsProvider())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .withRegion(Regions.US_EAST_1).build();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private AWSCredentialsProvider awsCredentialsProvider(){&nbsp; &nbsp; &nbsp; &nbsp; return new AWSStaticCredentialsProvider(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new BasicAWSCredentials(awsAccessKey, awsSecretKey));&nbsp; &nbsp; }}将以下属性添加到应用程序.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 {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @Bean&nbsp; &nbsp; public AmazonDynamoDB amazonDynamoDB() {&nbsp; &nbsp; &nbsp; &nbsp; return AmazonDynamoDBClientBuilder.standard().build();&nbsp; &nbsp; }}但是,当区域更改时,您必须在配置中传递区域值,如下所示。@Configuration@EnableDynamoDBRepositories(basePackageClasses = YourRepository.class)public class DynamoDBConfiguration {&nbsp; &nbsp; @Bean&nbsp; &nbsp; public AmazonDynamoDB amazonDynamoDB() {&nbsp; &nbsp; &nbsp; &nbsp; return AmazonDynamoDBClientBuilder.standard()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .withRegion(Regions.US_EAST_1).build();&nbsp; &nbsp; }}关于授权,不需要访问密钥和密钥。必须在 EC2 端配置 IAM 角色才能访问 DynamoDB 实例。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java