3. spring boot集成AWS DynamoDB

Table of Contents

使用serverless部署一个dynamodb的common表

创建一个deploy bucket

在父模块的根目录下创建config.yml文件

在dto子模块的根目录下创建serverless.yml文件

使用serverless命令部署表

配置aws sdk、spirng以及工具包

在父模块中添加aws sdk以及一些工具包的版本管理

向dto子模块中添加aws dynamodb以及一些工具包的依赖

向dao子模块中添加spring包的依赖

向service子模块中添加spring和lombok包的依赖

dto子模块代码实现

创建DynamoDBUtil

创建KeyGenerationUtil

创建BasicItem

创建UserItem

创建DynamoDB的配置文件

dao子模块代码实现

创建BasicOperationDao

创建UserDao

service子模块代码实现

创建UserService

front子模块代码实现

实现UserController对user的增删改查的逻辑

参考


使用serverless部署一个dynamodb的common表

创建一个deploy bucket

这里注意指定一个自己的bucket name,因为s3的bucket名称要求是唯一的

$aws s3api create-bucket --bucket develop-social-network-deploy --region ap-northeast-1 --create-bucket-configuration LocationConstraint=ap-northeast-1

回显如下内容表示创建成功:

{
    "Location": "http://develop-social-network-deploy.s3.amazonaws.com/"
}

在父模块的根目录下创建config.yml文件

social-network-serverless/config.yml

使用第一步中创建的bucket name

这里配置文件的bucket name引入了一个stage的变量,主要是用于多环境部署,也可以直接写成第一步创建的名字。

variant: jessica

stage: develop

# The region you will deploy your aws service to
region: ap-northeast-1

# The bucket your upload your resource to, specify your own bucket name
deploymentBucket: ${self:provider.stage}-social-network-deploy

在dto子模块的根目录下创建serverless.yml文件

social-network-serverless/social-network-serverless-dto/serverless.yml

service: social-network-dynamodb
frameworkVersion: ">=1.2.0 <2.0.0"
custom:
  configFile: ${file(../config.yml)}

provider:
  name: aws
  stage: ${self:custom.configFile.stage}
  region: ${self:custom.configFile.region}
  variant: ${self:custom.configFile.variant}
  stackName: ${self:provider.stage}-${self:provider.variant}-${self:service}
  deploymentBucket: ${self:custom.configFile.deploymentBucket}

resources:
  Parameters:
    HashKeyElementName:
      Description: HashType PrimaryKey Name
      Type: String
      Default: hashKey
      AllowedPattern: '[a-zA-Z0-9]*'
      MinLength: '1'
      MaxLength: '2048'
      ConstraintDescription: must contain only alphanumberic characters

    HashKeyElementType:
      Description: HashType PrimaryKey Type
      Type: String
      Default: S
      AllowedPattern: S
      MinLength: '1'
      MaxLength: '1'
      ConstraintDescription: must be S

    RangeKeyElementName:
      Description: RangeType PrimaryKey Name
      Type: String
      Default: rangeKey
      AllowedPattern: '[a-zA-Z0-9]*'
      MinLength: '1'
      MaxLength: '2048'
      ConstraintDescription: must contain only alphanumberic characters

    RangeKeyElementType:
      Description: RangeType PrimaryKey Type
      Type: String
      Default: S
      AllowedPattern: S
      MinLength: '1'
      MaxLength: '1'
      ConstraintDescription: must be S

    ReadCapacityUnits:
      Description: Provisioned read throughput
      Type: Number
      Default: '1'
      MinValue: '1'
      MaxValue: '2'
      ConstraintDescription: must be between 1 and 2
    WriteCapacityUnits:
      Description: Provisioned write throughput
      Type: Number
      Default: '1'
      MinValue: '1'
      MaxValue: '2'
      ConstraintDescription: must be between 1 and 2

  Resources:
    CommonTable:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        TableName: ${self:provider.stage}.common
        AttributeDefinitions:
          - AttributeName: !Ref HashKeyElementName
            AttributeType: !Ref HashKeyElementType
          - AttributeName: !Ref RangeKeyElementName
            AttributeType: !Ref RangeKeyElementType
        KeySchema:
          - AttributeName: !Ref HashKeyElementName
            KeyType: HASH
          - AttributeName: !Ref RangeKeyElementName
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: !Ref ReadCapacityUnits
          WriteCapacityUnits: !Ref WriteCapacityUnits

  # Print out the name of the table that is created
  Outputs:
    TableName:
      Value: !Ref CommonTable
      Description: Table name of the newly created DynamoDB table

该模版定义了一个DynamoDB的通用表,表的名字为develop.common,表的primary key由partion key和sort key两部分组成,partion key的类型为字符串,名字为hashKey, sort key的类型为字符串,名字为rangeKey.

使用serverless命令部署表

$ sls deploy -v

有如下回显表示创建成功

sls deploy -v
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Validating template...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
CloudFormation - CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - develop-jessica-social-network-dynamodb
CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - CommonTable
CloudFormation - CREATE_IN_PROGRESS - AWS::DynamoDB::Table - CommonTable
CloudFormation - CREATE_COMPLETE - AWS::DynamoDB::Table - CommonTable
CloudFormation - CREATE_COMPLETE - AWS::CloudFormation::Stack - develop-jessica-social-network-dynamodb
Serverless: Stack create finished...
Service Information
service: social-network-dynamodb
stage: develop
region: ap-southeast-1
stack: develop-jessica-social-network-dynamodb
resources: 1
api keys:
  None
endpoints:
  None
functions:
  None
layers:
  None

Stack Outputs
TableName: develop.common
ServerlessDeploymentBucketName: develop-social-network-deploy

配置aws sdk、spirng以及工具包

在父模块中添加aws sdk以及一些工具包的版本管理

在dependencyManagement的dependencies中添加对com.amazonaws:aws-java-sdk-bom的依赖

social-network-serverless/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值