提交 15c2365f 编写于 作者: 小傅哥's avatar 小傅哥

小傅哥,数据库路由组件设计

上级 0996951c
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
...@@ -34,5 +34,4 @@ public class DBContextHolder { ...@@ -34,5 +34,4 @@ public class DBContextHolder {
tbKey.remove(); tbKey.remove();
} }
} }
...@@ -37,7 +37,6 @@ public class DBRouterJoinPoint { ...@@ -37,7 +37,6 @@ public class DBRouterJoinPoint {
@Around("aopPoint() && @annotation(dbRouter)") @Around("aopPoint() && @annotation(dbRouter)")
public Object doRouter(ProceedingJoinPoint jp, DBRouter dbRouter) throws Throwable { public Object doRouter(ProceedingJoinPoint jp, DBRouter dbRouter) throws Throwable {
Method method = getMethod(jp);
String dbKey = dbRouter.key(); String dbKey = dbRouter.key();
if (StringUtils.isBlank(dbKey)) throw new RuntimeException("annotation DBRouter key is null!"); if (StringUtils.isBlank(dbKey)) throw new RuntimeException("annotation DBRouter key is null!");
// 计算路由 // 计算路由
...@@ -48,10 +47,11 @@ public class DBRouterJoinPoint { ...@@ -48,10 +47,11 @@ public class DBRouterJoinPoint {
// 库表索引 // 库表索引
int dbIdx = idx / dbRouterConfig.getTbCount() + 1; int dbIdx = idx / dbRouterConfig.getTbCount() + 1;
int tbIdx = idx - dbRouterConfig.getTbCount() * (dbIdx - 1); int tbIdx = idx - dbRouterConfig.getTbCount() * (dbIdx - 1);
// 设置到ThreadLocal // 设置到 ThreadLocal
DBContextHolder.setDBKey(String.format("%02d", dbIdx)); DBContextHolder.setDBKey(String.format("%02d", dbIdx));
DBContextHolder.setTBKey(String.format("%02d", tbIdx)); DBContextHolder.setTBKey(String.format("%02d", tbIdx));
logger.info("数据库路由 method:{} dbIdx:{} tbIdx:{}", method.getName(), dbIdx, tbIdx); logger.info("数据库路由 method:{} dbIdx:{} tbIdx:{}", getMethod(jp).getName(), dbIdx, tbIdx);
// 返回结果
try { try {
return jp.proceed(); return jp.proceed();
} finally { } finally {
...@@ -63,22 +63,15 @@ public class DBRouterJoinPoint { ...@@ -63,22 +63,15 @@ public class DBRouterJoinPoint {
private Method getMethod(JoinPoint jp) throws NoSuchMethodException { private Method getMethod(JoinPoint jp) throws NoSuchMethodException {
Signature sig = jp.getSignature(); Signature sig = jp.getSignature();
MethodSignature methodSignature = (MethodSignature) sig; MethodSignature methodSignature = (MethodSignature) sig;
return getClass(jp).getMethod(methodSignature.getName(), methodSignature.getParameterTypes()); return jp.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
}
private Class<? extends Object> getClass(JoinPoint jp) throws NoSuchMethodException {
return jp.getTarget().getClass();
} }
public String getAttrValue(String attr, Object[] args) { public String getAttrValue(String attr, Object[] args) {
String filedValue = null; String filedValue = null;
for (Object arg : args) { for (Object arg : args) {
try { try {
if (StringUtils.isBlank(filedValue)) { if (StringUtils.isNotBlank(filedValue)) break;
filedValue = BeanUtils.getProperty(arg, attr); filedValue = BeanUtils.getProperty(arg, attr);
} else {
break;
}
} catch (Exception e) { } catch (Exception e) {
logger.error("获取路由属性值失败 attr:{}", attr, e); logger.error("获取路由属性值失败 attr:{}", attr, e);
} }
......
...@@ -14,7 +14,6 @@ import org.springframework.core.env.PropertyResolver; ...@@ -14,7 +14,6 @@ import org.springframework.core.env.PropertyResolver;
*/ */
public class PropertyUtil { public class PropertyUtil {
private static int springBootVersion = 1; private static int springBootVersion = 1;
static { static {
......
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
文件模式从 100644 更改为 100755
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册