doc: update docs

This commit is contained in:
开源海哥 2023-09-24 09:24:50 +08:00
parent 893a1f3455
commit f839dfdd24
2 changed files with 65 additions and 10 deletions

View File

@ -60,7 +60,56 @@ public interface TenantFactory {
除了显示租户自己的数据以外,还包含下级租户的数据,这种场景则要求 `getTenantIds` 返回多个值。
- **场景3**:忽略租户条件,由代码自定义条件查询,此项要求 `getTenantIds` 返回 null 或者 空数组。
**注意!注意!注意!**
> 在整个应用中,应该只有一个 `TenantFactory` 实例,然后再通过其 `getTenantIds()` 方法里去获取当前的租户 ID在 Spring 常见中,我们可以通过在
> RequestContextHolder 中去获取当前的租户 ID。在其他框架中我们可以通过自定义 ThreadLocal 去获取 TenantId。
## 示例代码
```java
public class MyTenantFactory implements TenantFactory {
public Object[] getTenantIds(){
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
Long tenantId = attributes.getAttribute("tenantId", RequestAttributes.SCOPE_REQUEST);
return new Object[]{tenantId};
}
}
```
当然,`MyTenantFactory` 需要正常工作,我们需要在 Spring 拦截器里,需要通过 request 去获取当前的租户 ID并设置到 request 的 attribute如下代码所示
```java
public class TenantInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request
, HttpServletResponse response, Object handler) throws Exception {
//通过 request 去获取租户 ID
Long tenantId = getTenantIdByReuqest(request);
//设置租户ID到 request 的 attribute
request.setAttribute("tenantId", tenantId);
return true;
}
}
```
同时,在 `WebMvcConfigurer` 中,通过重写 `addInterceptors` 方法添加一下我们自定义的多租户拦截器:`TenantInterceptor`
```java
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TenantInterceptor());
}
}
```
## SpringBoot 支持
@ -69,10 +118,9 @@ public interface TenantFactory {
```java
@Configuration
public class MyConfiguration {
@Bean
public TenantFactory tenantFactory(){
TenantFactory tenantFactory = new ....;
TenantFactory tenantFactory = new MyTenantFactory();
return tenantFactory;
}

View File

@ -1,17 +1,20 @@
# 读写分离
MyBatis-Flex 的读写分离功能是基于 【多数据源】 功能来实现的。
MyBatis-Flex 的读写分离功能是基于 【[多数据源](./multi-datasource.md)】 功能来实现的。
读写分离的功能,要求当前环境必须是多个数据库(也可理解为多个数据源),其原理是:
让主数据库master处理事务性操作比如增、删、改INSERT、DELETE、UPDATE而从数据库slave处理 SELECT 查询操作。
读写分离的功能,要求当前环境必须是多个数据库(也可理解为多个数据源),其原理是:
让主数据库master处理事务性操作比如增、删、改INSERT、DELETE、UPDATE而从数据库slave处理查询SELECT操作。
在 MyBatis 框架中,我们知道: 所有关于数据库的的操作都是通过 Mapper 来实现的Mapper 里的一个方法,往往是和一个 SQL 一一对应。
## 实现原理
在 MyBatis 框架中,我们知道: 所有关于数据库的的操作都是通过 Mapper 来进行的Mapper 里的一个方法,往往是和一个执行 SQL 一一对应。
因此,在 MyBatis-Flex 中,提供了一种基于 Mapper 方法的读写分离策略。
## 分片策略
## 数据源分片策略
自定义 `DataSourceShardingStrategy` 例如:
在 MyBatis-Flex 框架中,我们需要通过实现 `DataSourceShardingStrategy` 接口来自定义自己的数据源读写分离策略(分片策略)例如:
```java
public class MyStrategy implements DataSourceShardingStrategy {
@ -27,7 +30,7 @@ public class MyStrategy implements DataSourceShardingStrategy {
doSharding 的参数分别为:
- currentDataSourceKey当前由用户端已配置的 key
- currentDataSourceKey当前使用的数据源 key
- mapper当前的 mapper 对象
- mapperMethod: 当前的 mapper 方法
- methodArgs当前的 mapper 方法的参数内容
@ -68,7 +71,7 @@ mybatis-flex:
password: 123456
```
以上配置中,一共有 4 个数据源,分别为 `master``slave1``slave2``other`
假设我们的需求是:在 增删改 时,走 master而在查询时自动使用 `slave1``slave2` 进行负载均衡。
我们的需求是:在 增删改 时,走 master 数据源,而在查询时,随机自动使用 `slave1``slave2` 数据源进行负载均衡。
那么,我们的分片策略代码如下:
@ -95,3 +98,7 @@ public class MyStrategy implements DataSourceShardingStrategy {
}
}
```
## 注意事项
> MyBatis-Flex 的读写分离组件,只进行数据查询和数据操作时的读写分离,并不涉及主从数据库之间的数据同步,主从数据库同步需要用户自己在数据库服务器,通过第三方组件去实现。