Jelajahi Sumber

简化配置

AE86 1 tahun lalu
induk
melakukan
c194fd1a16

+ 6 - 15
dbsyncer-biz/src/main/java/org/dbsyncer/biz/checker/impl/system/SystemConfigChecker.java

@@ -1,20 +1,18 @@
 package org.dbsyncer.biz.checker.impl.system;
 
 import org.dbsyncer.biz.checker.AbstractChecker;
-import org.dbsyncer.common.util.NumberUtil;
-import org.dbsyncer.common.util.StringUtil;
+import org.dbsyncer.common.util.BeanUtil;
 import org.dbsyncer.manager.Manager;
 import org.dbsyncer.parser.logger.LogService;
 import org.dbsyncer.parser.logger.LogType;
-import org.dbsyncer.parser.model.SystemConfig;
 import org.dbsyncer.parser.model.ConfigModel;
-import org.dbsyncer.storage.constant.ConfigConstant;
+import org.dbsyncer.parser.model.SystemConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.util.Assert;
 
+import javax.annotation.Resource;
 import java.util.Map;
 
 /**
@@ -27,10 +25,10 @@ public class SystemConfigChecker extends AbstractChecker {
 
     private final Logger logger = LoggerFactory.getLogger(getClass());
 
-    @Autowired
+    @Resource
     private Manager manager;
 
-    @Autowired
+    @Resource
     private LogService logService;
 
     @Override
@@ -52,14 +50,7 @@ public class SystemConfigChecker extends AbstractChecker {
 
         SystemConfig systemConfig = manager.getSystemConfig();
         Assert.notNull(systemConfig, "配置文件为空.");
-
-        // 同步数据过期时间(天)
-        systemConfig.setExpireDataDays(NumberUtil.toInt(params.get("expireDataDays"), systemConfig.getExpireDataDays()));
-        // 系统日志过期时间(天)
-        systemConfig.setExpireLogDays(NumberUtil.toInt(params.get("expireLogDays"), systemConfig.getExpireLogDays()));
-        // 刷新监控间隔(秒)
-        systemConfig.setRefreshIntervalSeconds(NumberUtil.toInt(params.get("refreshIntervalSeconds"), systemConfig.getRefreshIntervalSeconds()));
-
+        BeanUtil.mapToBean(params, systemConfig);
         logService.log(LogType.SystemLog.INFO, "修改系统配置");
 
         // 修改基本配置

+ 10 - 10
dbsyncer-common/src/main/java/org/dbsyncer/common/config/StorageConfig.java

@@ -30,12 +30,12 @@ public class StorageConfig extends BufferActuatorConfig {
     /**
      * 是否记录同步成功数据
      */
-    private boolean writerSuccess;
+    private boolean writeSuccess;
 
     /**
      * 是否记录同步失败数据
      */
-    private boolean writerFail;
+    private boolean writeFail;
 
     /**
      * 最大记录异常信息长度
@@ -63,20 +63,20 @@ public class StorageConfig extends BufferActuatorConfig {
         this.threadQueueCapacity = threadQueueCapacity;
     }
 
-    public boolean isWriterSuccess() {
-        return writerSuccess;
+    public boolean isWriteSuccess() {
+        return writeSuccess;
     }
 
-    public void setWriterSuccess(boolean writerSuccess) {
-        this.writerSuccess = writerSuccess;
+    public void setWriteSuccess(boolean writeSuccess) {
+        this.writeSuccess = writeSuccess;
     }
 
-    public boolean isWriterFail() {
-        return writerFail;
+    public boolean isWriteFail() {
+        return writeFail;
     }
 
-    public void setWriterFail(boolean writerFail) {
-        this.writerFail = writerFail;
+    public void setWriteFail(boolean writeFail) {
+        this.writeFail = writeFail;
     }
 
     public int getMaxErrorLength() {

+ 83 - 0
dbsyncer-common/src/main/java/org/dbsyncer/common/util/BeanUtil.java

@@ -0,0 +1,83 @@
+package org.dbsyncer.common.util;
+
+import org.dbsyncer.common.CommonException;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+
+public abstract class BeanUtil {
+
+    public static Object mapToBean(Map<String, String> map, Object instance) {
+        try {
+            Class<?> clazz = instance.getClass();
+            for (Map.Entry<String, String> eachMap : map.entrySet()) {
+                String property = eachMap.getKey();
+                String value = eachMap.getValue();
+
+                String setMethod = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
+                Field field = getField(property, clazz);
+                Class<?> fType = field.getType();
+                Object newValue = convert(value, fType);
+                clazz.getMethod(setMethod, fType).invoke(instance, newValue);
+            }
+        } catch (Exception e) {
+            throw new CommonException(e);
+        }
+        return instance;
+    }
+
+    public static Map beanToMap(Object object) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
+        Field[] field = object.getClass().getDeclaredFields();
+        Map map = new HashMap();
+        for (Field fi : field) {
+            String property = fi.getName();
+            String getMe = "get" + property.substring(0, 1).toUpperCase() + property.substring(1);
+            Object obj = object.getClass().getMethod(getMe).invoke(object);
+            map.put(property, obj);
+        }
+        return map;
+    }
+
+    private static Object convert(String value, Class<?> fType) {
+        if (Long.class.getName().equals(fType.getName()) || long.class.getName().equals(fType.getName())) {
+            return Long.parseLong(value);
+        }
+
+        if (Float.class.getName().equals(fType.getName()) || float.class.getName().equals(fType.getName())) {
+            return Float.parseFloat(value);
+        }
+
+        if (Double.class.getName().equals(fType.getName()) || double.class.getName().equals(fType.getName())) {
+            return Double.parseDouble(value);
+        }
+
+        if (Integer.class.getName().equals(fType.getName()) || int.class.getName().equals(fType.getName())) {
+            return Integer.parseInt(value);
+        }
+
+        if (Boolean.class.getName().equals(fType.getName()) || boolean.class.getName().equals(fType.getName())) {
+            return Boolean.valueOf(value);
+        }
+        return value;
+    }
+
+    private static Field getField(String property, Class<?> obj) {
+        if (Object.class.getName().equals(obj.getName())) {
+            return null;
+        }
+        Field[] field = obj.getDeclaredFields();
+        for (Field f : field) {
+            if (f.getName().equals(property)) {
+                return f;
+            }
+        }
+        Class<?> parent = obj.getSuperclass();
+        if (parent != null) {
+            return getField(property, parent);
+        }
+        return null;
+    }
+
+}

+ 2 - 2
dbsyncer-connector/src/main/test/ConnectionTest.java

@@ -114,11 +114,11 @@ public class ConnectionTest {
         long begin = Instant.now().toEpochMilli();
         final int threadSize = 10;
         final ExecutorService pool = Executors.newFixedThreadPool(threadSize);
-        final String sql = "INSERT INTO `vote_records` (`id`, `user_id`, `vote_num`, `group_id`, `status`, `create_time`) VALUES (?, ?, ?, ?, ?, ?)";
+        final String sql = "INSERT INTO `vote_records_copy` (`id`, `user_id`, `vote_num`, `group_id`, `status`, `create_time`) VALUES (?, ?, ?, ?, ?, ?)";
 
         // 模拟1000w条数据
         List<Object[]> dataList = new ArrayList<>();
-        for (int i = 2000002; i <= 5000000; i++) {
+        for (int i = 1; i <= 200001; i++) {
             // 442001, 'dA8LeJLtX9MgQgDe7H1O', 9620, 1, 2, '2022-11-17 16:35:21'
             Object[] args = new Object[6];
             args[0] = i;

+ 1 - 2
dbsyncer-parser/src/main/java/org/dbsyncer/parser/ParserFactory.java

@@ -190,8 +190,7 @@ public class ParserFactory implements Parser {
 
     @Override
     public <T> T parseObject(String json, Class<T> clazz) {
-        T t = JsonUtil.jsonToObj(json, clazz);
-        return t;
+        return JsonUtil.jsonToObj(json, clazz);
     }
 
     @Override

+ 2 - 2
dbsyncer-parser/src/main/java/org/dbsyncer/parser/flush/AbstractFlushStrategy.java

@@ -41,13 +41,13 @@ public abstract class AbstractFlushStrategy implements FlushStrategy {
     protected void flush(String metaId, Result result, String event) {
         refreshTotal(metaId, result);
 
-        if (storageConfig.isWriterFail() && !CollectionUtils.isEmpty(result.getFailData())) {
+        if (storageConfig.isWriteFail() && !CollectionUtils.isEmpty(result.getFailData())) {
             final String error = StringUtil.substring(result.getError().toString(), 0, storageConfig.getMaxErrorLength());
             flushService.asyncWrite(metaId, result.getTableGroupId(), result.getTargetTableGroupName(), event, false, result.getFailData(), error);
         }
 
         // 是否写增量数据
-        if (storageConfig.isWriterSuccess() && !CollectionUtils.isEmpty(result.getSuccessData())) {
+        if (storageConfig.isWriteSuccess() && !CollectionUtils.isEmpty(result.getSuccessData())) {
             flushService.asyncWrite(metaId, result.getTableGroupId(), result.getTargetTableGroupName(), event, true, result.getSuccessData(), "");
         }
     }

+ 1 - 1
dbsyncer-parser/src/main/java/org/dbsyncer/parser/strategy/impl/EnableFlushStrategy.java

@@ -12,7 +12,7 @@ import org.springframework.stereotype.Component;
  * @date 2021/11/18 22:21
  */
 @Component
-@ConditionalOnProperty(value = "dbsyncer.storage.full.enabled", havingValue = "true")
+@ConditionalOnProperty(value = "dbsyncer.storage.write.full.enabled", havingValue = "true")
 public final class EnableFlushStrategy extends AbstractFlushStrategy {
 
 }

+ 3 - 3
dbsyncer-web/src/main/resources/application.properties

@@ -63,11 +63,11 @@ dbsyncer.storage.buffer-queue-capacity=50000
 # [StorageBufferActuator]定时消费缓存队列间隔(毫秒)
 dbsyncer.storage.buffer-period-millisecond=300
 # 是否记录全量数据(false-关闭; true-开启)
-dbsyncer.storage.full.enabled=false
+dbsyncer.storage.write.full.enabled=false
 # 是否记录同步成功数据(false-关闭; true-开启)
-dbsyncer.storage.writer-success=true
+dbsyncer.storage.write-success=true
 # 是否记录同步失败数据(false-关闭; true-开启)
-dbsyncer.storage.writer-fail=true
+dbsyncer.storage.write-fail=true
 # 记录同步失败日志最大长度
 dbsyncer.storage.max-error-length=2048