티스토리 뷰

Language/JAVA

Spring Boot DynamoDb연동하기

KyeongRok Kim 2018. 5. 8. 14:56

Spring Boot DynamoDb연동하기

 

ServerModule.java

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

public abstract class ServerModule {
    @Value("$")
    String awsRegion;

    public abstract AWSCredentialsProvider getCredentialsProvider();

    @Bean
    public AmazonDynamoDB dynamoDB(AWSCredentialsProvider credentialsProvider) {
        return AmazonDynamoDBClientBuilder
                .standard()
                .withCredentials(credentialsProvider)
                .withRegion(this.awsRegion)
                .build();
    }
}
​

 

 

LocalServerModule.java

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.*;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@Profile({"local"})
@Slf4j
@Configuration
@EnableAsync
@EnableScheduling
public class LocalServerModule extends ServerModule {

    @Bean
    @Override
    public AWSCredentialsProvider getCredentialsProvider() {
        log.warn("[DefaultAWSCredentialsProviderChain]");
        return DefaultAWSCredentialsProviderChain.getInstance();
    }
}
 
 
application-local.yml
1
2
aws:
  region: us-west-2
cs
 
 
AwsDynamoDbTest.java
@ActiveProfiles("local")
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class AwsDynamoDbTest {

    @Autowired
    AmazonDynamoDB amazonDynamoDB;

    @Autowired
    AWSCredentialsProvider awsCredentialsProvider;

    private Worker worker;

    @Test
    public void name() {

        String tableName = "<table_name>";
        DescribeTableResult describeTableResult = amazonDynamoDB.describeTable(tableName);
        System.out.println(describeTableResult);

        String streamArn = describeTableResult.getTable().getLatestStreamArn();
        log.info("streamArn:{}",streamArn);
    }
}
​
 
end.
 
 

 

728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함