提交 6dc89fc9 编写于 作者: lakernote's avatar lakernote

增加shardingjdbc

上级 c780bffb
......@@ -10,6 +10,7 @@ import com.laker.admin.module.ext.service.IExtLogService;
import com.laker.admin.utils.IP2CityUtil;
import com.laker.admin.utils.http.HttpServletRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.hint.HintManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
......@@ -88,7 +89,15 @@ public class MetricsAspect {
Boolean success = JSONUtil.parseObj(response).getBool("success", true);
logBean.setStatus(success);
}
extLogService.save(logBean);
// hint
HintManager hintManager = HintManager.getInstance();
hintManager.addTableShardingValue("ext_log", 0L);
try {
extLogService.save(logBean);
} finally {
hintManager.close();
}
return returnValue;
}
}
package com.laker.admin.framework.ext.sharding;
import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;
import java.util.ArrayList;
import java.util.Collection;
public final class ModuloHintShardingAlgorithm implements HintShardingAlgorithm<Long> {
@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final HintShardingValue<Long> shardingValue) {
Collection<String> result = new ArrayList<>();
System.out.println("----------------------------------------------------");
System.out.println(availableTargetNames);
for (String each : availableTargetNames) {
for (Long value : shardingValue.getValues()) {
if (each.endsWith(String.valueOf(value % 2))) {
result.add(each);
}
}
}
return result;
}
}
\ No newline at end of file
package com.laker.admin.framework.ext.sharding;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import java.util.Collection;
public final class PreciseModuloShardingTableAlgorithm implements PreciseShardingAlgorithm<Long> {
@Override
public String doSharding(final Collection<String> tableNames, final PreciseShardingValue<Long> shardingValue) {
for (String each : tableNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new UnsupportedOperationException();
}
}
\ No newline at end of file
......@@ -28,18 +28,30 @@ spring:
jdbcUrl: jdbc:mysql://localhost:3306/laker?serverTimezone=GMT%2B8&characterEncoding=utf8&useSSL=false
username: root
password: 123456
# 是否在日志中打印 SQ
mode:
type: Standalone
sharding:
tables:
# 配置ext_log的分表的规则
ext_log:
# 拥有几个分片表0-1
actual-data-nodes: ds.ext_log_$->{0..1}
# 表达式方式指定
# table-strategy:
# inline:
# sharding-column: user_id
# algorithmExpression: ext_log_${user_id % 2}
# 代码逻辑指定
table-strategy:
inline:
sharding-column: user_id
algorithmExpression: ext_log_${user_id % 2}
hint:
algorithmClassName: com.laker.admin.framework.ext.sharding.ModuloHintShardingAlgorithm
# table-strategy:
# standard:
# shardingColumn: user_id
# preciseAlgorithmClassName: com.laker.admin.framework.ext.sharding.PreciseModuloShardingTableAlgorithm
keyGenerator:
type: SNOWFLAKE
column: log_id
props:
worker.id: 123
# 是否在日志中打印逻辑SQL和实际sql
props:
sql.show: true
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册