SpringBoot 集成 DynamoDB 多数据源问题

文章同步到个人站点 Daniel Notes

背景

在多数据源工程中集成 DynamoDB 时遇到了 DynamoDB 的不支持多数据源的问题 😶

SpringBoot 集成 DynamoDB

大概讲一下我集成 DynamoDB 的方式,如果你的数据源只有 DynamoDB 那么下面的集成方式则可以正常工作。

首先引入 Maven 依赖

    <dependency>
      <groupId>com.github.derjust</groupId>
      <artifactId>spring-data-dynamodb</artifactId>
      <version>5.1.0</version>
    </dependency>

DynamodbConfig 配置类

@Configuration
@EnableDynamoDBRepositories(basePackages = "DynamoDB Repository 所在的包路径")
public class DynamodbConfig {
  @Value("${xxx.xxxx.connection-timeout:10000}")
  private int connectionTimeout;

  @Value("${xxx.xxxx.socket-timeout:30000}")
  private int socketTimeout;

  @Value("${xxx.xxxx.max-error-retry:0}")
  private int maxErrorRetry;

  @Bean(name = "amazonDynamoDB")
  public AmazonDynamoDB amazonDynamodb() {
    return AmazonDynamoDBClientBuilder.standard().withClientConfiguration(
        new ClientConfiguration().withConnectionTimeout(connectionTimeout)
            .withSocketTimeout(socketTimeout).withMaxErrorRetry(maxErrorRetry)).build();
  }
}

构建表的实体类

@Data
@DynamoDBTable(tableName = "UserInfo")
public class UserInfo implements Serializable {

  @DynamoDBHashKey(attributeName = "udid")
  private String uid;
}

创建 Repository

@EnableScan
public interface UserInfoRepository extends CrudRepository<UserInfo, String> {

}

自此 SpringBoot 集成 DynamoDB 的部分已经完成,和 JPA 类似你可以在你的业务逻辑中使用 @Autowired 注入 UserInfoRepository 进行数据操作。

多数据源问题

我们的工程一般都不仅仅使用单一的数据源,DynamoDB 与关系型数据库「MySQL」结合是很常见的,但是集成方式和上面的有些需要注意的地方,否则会遇到如下所示的错误,这里补充一下,我同时在工程中使用了 JPA 的方式集成 MySQL。

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userInfoRepository in xxx required a bean of type 'xxx.xxxx.UserInfoRepository' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.csulb.md.repo.UserInfoRepository' in your configuration.

这个问题的原因启动日志里面有提到,往上翻翻启动日志,你会看到如下的提示:

Spring Data DynamoDB does not support multi-store setups!.

也就是说 Spring Data DynamoDB 不支持多数据源,其实是因为 EnableDynamoDBRepositoriesEnableJpaRepositories 的自动扫描存在冲突,导致扫描不到 DynamoDB 的 Repository,为了解决这个问题,应该修改 SpringBoot 的扫描范围,将 DynamodbConfig 改成如下模式,记得引入 org.springframework.stereotype.Repository

import org.springframework.stereotype.Repository;

@EnableDynamoDBRepositories(basePackages = "DynamoDB Repository 所在的包路径",
    includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
        classes = Repository.class))

同时在 Repository 上加上 @Repository ,接着你的工程又能正常工作了😄

import org.springframework.stereotype.Repository;

@Repository
@EnableScan
public interface UserInfoRepository extends CrudRepository<UserInfo, String> {

}

自此上述问题解决,两个数据源都能正常工作,enjoy 😊 bye~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值