All Projects → jbkzty → Dynamic Datasource Starter

jbkzty / Dynamic Datasource Starter

springboot 动态切换数据的基本思想与实现方法

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Dynamic Datasource Starter

dynamic-datasource-starter
springboot 动态切换数据的基本思想与实现方法
Stars: ✭ 12 (-80.95%)
Mutual labels:  dynamic, datasource
Mgs Skinnedmesh
Unity plugin for create flexible hose in scene.
Stars: ✭ 58 (-7.94%)
Mutual labels:  dynamic
Bindsnet
Simulation of spiking neural networks (SNNs) using PyTorch.
Stars: ✭ 837 (+1228.57%)
Mutual labels:  dynamic
Plant
Trait-Driven Models of Ecology and Evolution 🌲
Stars: ✭ 39 (-38.1%)
Mutual labels:  dynamic
Dynamic widget
A Backend-Driven UI toolkit, build your dynamic UI with json, and the json format is very similar with flutter widget code.
Stars: ✭ 851 (+1250.79%)
Mutual labels:  dynamic
Carbon
🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.
Stars: ✭ 1,034 (+1541.27%)
Mutual labels:  datasource
Thinningcoordinator
The UITableView/UICollectionView dataSource/delegate thinning coordinator, help thinning your UIViewController!
Stars: ✭ 25 (-60.32%)
Mutual labels:  datasource
Dyci Main
Dynamic Code Injection Tool for Objective-C
Stars: ✭ 1,103 (+1650.79%)
Mutual labels:  dynamic
Kendogridbinderex
This is a ModelBinder designed to consume an http request and build a json serializable object for the Kendo UI Grid datasource. AutoMapper is used to support mapping from ViewModel <> Entity.
Stars: ✭ 52 (-17.46%)
Mutual labels:  dynamic
Rapidview
RapidView is an android ui and lightapp development framework
Stars: ✭ 951 (+1409.52%)
Mutual labels:  dynamic
Hr4r
Example project - "Hot Reloading 4 RequireJS" front-end web applications & some extra code demonstrating hot-reloading for Node.js Express servers
Stars: ✭ 28 (-55.56%)
Mutual labels:  dynamic
System.linq.dynamic.core
The .NET Standard / .NET Core version from the System Linq Dynamic functionality.
Stars: ✭ 864 (+1271.43%)
Mutual labels:  dynamic
Django Access
Django-Access - the application introducing dynamic evaluation-based instance-level (row-level) access rights control for Django
Stars: ✭ 47 (-25.4%)
Mutual labels:  dynamic
Xudanrw
一个灵活的读写分离组件
Stars: ✭ 9 (-85.71%)
Mutual labels:  datasource
Terraform Aws Multi Az Subnets
Terraform module for multi-AZ public and private subnets provisioning
Stars: ✭ 58 (-7.94%)
Mutual labels:  dynamic
Modelassistant
Elegant library to manage the interactions between view and model in Swift
Stars: ✭ 26 (-58.73%)
Mutual labels:  datasource
Redux Dynamic Modules
Modularize Redux by dynamically loading reducers and middlewares.
Stars: ✭ 874 (+1287.3%)
Mutual labels:  dynamic
Dynamic Data Source Demo
基于事务的读写分离
Stars: ✭ 43 (-31.75%)
Mutual labels:  dynamic
Horope
Generates dynamic rope setup from spline. c4d python plugin.
Stars: ✭ 61 (-3.17%)
Mutual labels:  dynamic
Lypaymentfield
多种风格的支付框控件,可定制加密图片,也可使用带动画的弹窗。A variety of styles of payment box controls can be customized to encrypt pictures, can also be used with animated alert.
Stars: ✭ 58 (-7.94%)
Mutual labels:  dynamic

dynamic-datasource-starter

springboot 动态切换数据的基本思想与实现方法

(1)基本原理: 通过切面根据不同的条件在执行数据库操作前切换数据源

在理想情况下(一个项目应该只有一个业务库),一个项目有一个可写数据源和多个只读数据源,为了减少数据库的压力,使用轮询的方式选择只读数据库; 考虑到在一个Service中同时会有读和写的操作,所以使用AOP切面通过dao层的方法名切换只读数据源

在使用中,应该避免在同一个Service方法中写入后立即查询,如果一定需要,应当在Service方法上添加@Transactional注解以保证主数据源

(2)在极端现实场景中,使用的DB宕机之后需要修改IP,因此实现了一个动态调整数据源的方式

  • 主要思路是实现在HikariDataSource上包装一个DataSource,在发生修改的时候,从Spring的上下文中获取原有的DataSource进行替换

  • 需要注意的是,如果老的连接没有做任何的处理,可能造成连接泄漏。
    因此在我们完全切换成新的数据库连接前,我们需要获取到老的连接池,并且校验是否有活动链接,直到没有任何活动链接时,我们需要关闭老的连接

DataSourceConfigurer
数据源配置类,在该类中生成多个数据源实例并且将其注入到ApplicationContext中
  - 将数据源的 key 放到数据源上下文的 key 集合中,用于切换时判断数据源是否有效
  - 将 Slave数据源的key放在集合中,用于轮循
DynamicDataSourceAspect
 DynamicDataSourceAspect,动态数据源切换的切面,切dao层,通过dao的名称来判断使用哪个数据源
 关于切面的Order可以不设,因为 @Transactional是最低的,取决于其他切面的设置,并且在 org.springframework.core.annotation.AnnotationAwareOrderComparator 会重新排序
DataSourceManager
 重新新建一个dataSource进行替换
DynamicDataSource
  自定义类,实现DataSource的获取connection的方法,通过 {@link DynamicRoutingDataSource 找到对应的 DynamicDataSource}
DynamicRoutingDataSource
该类继承自AbstractRoutingDataSource类,在访问数据库的时会调用该类的 determineCurrentLookupKey() 方法获取数据库的实例的key

注意

(1) 有@Trasactional注解的方法无法切换数据源   

(2) Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
    该异常在错误信息中已经说的很清楚了,是因为有多个 DataSource 的实例,所以无法确定该引用那个实例
    
    为数据源的某个Bean添加@Primary注解,该Bean应当是通过DataSourceBuilder.create().build() 得到的 Bean,
    而不是通过 new AbstractRoutingDataSource 的子类实现的 Bean,
    在本项目中可以是 master() 或 slave() 得到的 DataSource,不能是 dynamicDataSource() 得到的 DataSource
    
(3) @ConfigurationProperties的功能,可以将同类的配置信息自动封装成实体类
    
    @Bean
    @ConfigurationProperties(prefix = "connection")
    public ConnectionSettings connectionSettings(){
       return new ConnectionSettings();
    }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].