Browse Source

data-upgrade 配置项调整

deason 3 tháng trước cách đây
mục cha
commit
eeb8675ea4

+ 1 - 3
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/config/DataUpgradeConstants.java

@@ -2,12 +2,10 @@ package com.qmth.boot.data.upgrade.config;
 
 public interface DataUpgradeConstants {
 
-    String ENABLE = "com.qmth.data.upgrade.enable";
+    String RUN_MODE = "com.qmth.data.upgrade.run-mode";
 
     String BASE_PACKAGE = "com.qmth.data.upgrade.base-package";
 
-    String FIRST_INSTALL = "com.qmth.data.upgrade.first-install";
-
     String BASELINE = "com.qmth.data.upgrade.baseline";
 
     String BASELINE_APP_CODE = "com.qmth.data.upgrade.baseline-app-code";

+ 53 - 28
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/config/DataUpgradeListener.java

@@ -2,6 +2,7 @@ package com.qmth.boot.data.upgrade.config;
 
 import com.qmth.boot.data.upgrade.annotation.DataUpgradeVersion;
 import com.qmth.boot.data.upgrade.model.BootAppInfo;
+import com.qmth.boot.data.upgrade.model.RunMode;
 import com.qmth.boot.data.upgrade.service.DataInitService;
 import com.qmth.boot.data.upgrade.service.DataUpgradeService;
 import com.qmth.boot.data.upgrade.utils.ClassHelper;
@@ -37,25 +38,30 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
     public void onApplicationEvent(ApplicationPreparedEvent event) {
         try {
             properties = new DataUpgradeProperties(event.getApplicationContext().getEnvironment());
-            if (!properties.getEnable()) {
-                // 未启用时,不再进行后续处理
-                return;
+
+            if (RunMode.INSTALL == properties.getRunMode()) {
+                // 首次安装模式,仅执行数据初始化
+                this.handlerInstall();
+                // 执行完后默认退出程序
+                System.exit(1);
             }
 
-            if (properties.getFirstInstall()) {
-                // 首次安装时,仅执行数据初始化
-                this.handlerDataInit();
-            } else {
-                // 否则执行数据升级
-                this.handlerDataUpgrade();
+            if (RunMode.UPGRADE == properties.getRunMode()) {
+                // 升级模式,仅执行数据升级
+                this.handlerUpgrade();
+                // 执行完后默认退出程序
+                System.exit(1);
             }
+
+            // 启动模式
+            this.handlerStart();
         } catch (Exception e) {
-            log.error("数据操作异常终止!原因:{}", e.getMessage(), e);
+            log.error("程序异常终止!原因:{}", e.getMessage(), e);
             System.exit(1);
         }
     }
 
-    public void handlerDataInit() {
+    public void handlerInstall() {
         try (HikariDataSource dataSource = this.initDataSource()) {
             JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
 
@@ -74,22 +80,20 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
                 throw new RuntimeException("不允许存在多个数据初始化的具体实现!");
             }
 
-            log.warn("*************** data init start ***************");
             try {
+                log.warn("*************** data init start ***************");
                 services.get(0).process(jdbcTemplate);
             } catch (Exception e) {
-                throw new RuntimeException("SQL语句执行异常!", e);
+                throw new RuntimeException("数据初始化执行异常!" + e.getMessage(), e);
             }
 
             // 数据初始化完成后,初始应用信息表
             this.initBootAppInfo(jdbcTemplate, properties.getAppCode(), properties.getAppVersion());
             log.warn("*************** data init finish ***************");
-        } catch (Exception e) {
-            throw new RuntimeException(e.getMessage(), e);
         }
     }
 
-    public void handlerDataUpgrade() {
+    public void handlerUpgrade() {
         try (HikariDataSource dataSource = this.initDataSource()) {
             JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
 
@@ -105,7 +109,7 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
 
             BootAppInfo appInfo = this.queryBootAppInfo(jdbcTemplate, properties.getAppCode());
             if (appInfo == null) {
-                throw new RuntimeException("【应用信息表】中未找到匹配的数据!appCode:" + properties.getAppCode());
+                throw new RuntimeException("【应用信息表】版本数据未找到!appCode:" + properties.getAppCode());
             }
 
             log.warn("待升级应用码:{} 待升级应用版本号:{} 当前应用码:{} 当前应用版本号:{} locked:{}", properties.getAppCode(),
@@ -115,14 +119,14 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
             if (compareValue > 0) {
                 throw new RuntimeException("待升级的版本号不能低于当前版本号!");
             }
+
             if (compareValue == 0) {
                 log.warn("跳过数据升级,版本号一致!");
                 return;
             }
 
+            // 数据升级实现 按版本号由小到大排序
             Map<String, DataUpgradeService> serviceMaps = this.matchServices(appInfo.getAppVersion(), properties.getAppVersion());
-
-            // 按版本号由小到大排序
             List<String> versionKeys = new ArrayList<>(serviceMaps.keySet());
             versionKeys.sort(new VersionComparator());
 
@@ -134,11 +138,11 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
 
             try {
                 for (String version : versionKeys) {
-                    log.warn("*************** data upgrade version:{} start ***************", version);
+                    log.warn("*************** version:{} data upgrade start ***************", version);
                     serviceMaps.get(version).process(jdbcTemplate);
                 }
             } catch (Exception e) {
-                throw new RuntimeException("SQL语句执行异常!", e);
+                throw new RuntimeException("数据升级执行异常!" + e.getMessage(), e);
             } finally {
                 // 升级解锁
                 unLock(jdbcTemplate, appInfo.getAppCode());
@@ -148,8 +152,29 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
             // 数据升级完成后,更新应用信息表
             this.updateBootAppInfo(jdbcTemplate, properties.getAppCode(), properties.getAppVersion());
             log.warn("*************** data upgrade finish ***************");
-        } catch (Exception e) {
-            throw new RuntimeException(e.getMessage(), e);
+        }
+    }
+
+    public void handlerStart() {
+        try (HikariDataSource dataSource = this.initDataSource()) {
+            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
+
+            if (!this.existBootAppInfo(jdbcTemplate)) {
+                throw new RuntimeException("【应用信息表】不存在!");
+            }
+
+            BootAppInfo appInfo = this.queryBootAppInfo(jdbcTemplate, properties.getAppCode());
+            if (appInfo == null) {
+                throw new RuntimeException("【应用信息表】版本数据未找到!appCode:" + properties.getAppCode());
+            }
+
+            log.warn("配置应用码:{} 配置应用版本号:{} 当前应用码:{} 当前应用版本号:{}", properties.getAppCode(),
+                    properties.getAppVersion(), appInfo.getAppCode(), appInfo.getAppVersion());
+
+            int compareValue = new VersionComparator().compare(appInfo.getAppVersion(), properties.getAppVersion());
+            if (compareValue != 0) {
+                throw new RuntimeException("配置中的版本号与数据库中的应用版本号不一致!");
+            }
         }
     }
 
@@ -182,7 +207,7 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
             if (vc.compare(implVersion, currentVersion) > 0 && targetVersionCompare <= 0) {
                 log.debug("implClass:{} implVersion:{}", implClass.getSimpleName(), implVersion);
                 if (serviceMaps.containsKey(implVersion)) {
-                    throw new RuntimeException("同个版本数据升级的实现出现重复!@DataUpgradeVersion:" + implVersion);
+                    throw new RuntimeException("同个版本数据升级的实现出现重复!版本:" + implVersion);
                 }
 
                 try {
@@ -191,14 +216,14 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
                         serviceMaps.put(implVersion, (DataUpgradeService) constructor.newInstance());
                     }
                 } catch (Exception e) {
-                    throw new RuntimeException(e.getMessage(), e);
+                    log.warn("{} class newInstance fail. {}", implClass.getSimpleName(), e.getMessage());
                 }
             }
         }
 
         if (!existTargetVersionImpl) {
-            log.warn("未找到版本“{}”数据升级的具体实现!{}:{}", targetVersion, BASE_PACKAGE, properties.getBasePackage());
-            throw new RuntimeException("未找到数据升级的具体实现!");
+            log.warn("未找到数据升级的具体实现!版本:{} {}:{}", targetVersion, BASE_PACKAGE, properties.getBasePackage());
+            throw new RuntimeException("未找到数据升级的具体实现!版本:" + targetVersion);
         }
 
         return serviceMaps;
@@ -256,7 +281,7 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
 
     private boolean existBootAppInfo(JdbcTemplate jdbcTemplate) {
         try {
-            // 是否存在应用信息表:boot_app_info
+            // 是否存在应用信息表
             List<String> exists = jdbcTemplate.queryForList("SHOW TABLES LIKE 'boot_app_info'", String.class);
             return !exists.isEmpty();
         } catch (Exception e) {

+ 13 - 20
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/config/DataUpgradeProperties.java

@@ -1,5 +1,6 @@
 package com.qmth.boot.data.upgrade.config;
 
+import com.qmth.boot.data.upgrade.model.RunMode;
 import com.qmth.boot.data.upgrade.utils.VersionComparator;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.boot.jdbc.DatabaseDriver;
@@ -8,20 +9,15 @@ import org.springframework.core.env.ConfigurableEnvironment;
 public class DataUpgradeProperties implements DataUpgradeConstants {
 
     /**
-     * 是否启用数据升级处理,默认值为true
+     * 运行模式,值范围:start(默认)、install、upgrade。
      */
-    private Boolean enable;
+    private RunMode runMode;
 
     /**
      * 数据升级服务实现类所在位置的包名,默认扫描包名为:com.qmth
      */
     private String basePackage;
 
-    /**
-     * 是否首次安装初始化,默认值为false
-     */
-    private Boolean firstInstall;
-
     /**
      * “应用信息表”不存在时,是否指定基线数据,默认值为false
      */
@@ -68,15 +64,16 @@ public class DataUpgradeProperties implements DataUpgradeConstants {
     private String appVersion;
 
     public DataUpgradeProperties(ConfigurableEnvironment environment) {
-        this.enable = environment.getProperty(ENABLE, Boolean.class, true);
-        if (!enable) {
-            // 未启用时,不再进行后续配置处理
-            return;
+        String runModeStr = environment.getProperty(RUN_MODE, String.class, RunMode.START.name());
+        this.runMode = RunMode.getRunMode(runModeStr);
+        if (this.runMode == null) {
+            throw new RuntimeException(RUN_MODE + " 配置值有误!值范围:start、install、upgrade");
         }
 
-        this.basePackage = environment.getProperty(BASE_PACKAGE, String.class, "com.qmth");
-
-        this.firstInstall = environment.getProperty(FIRST_INSTALL, Boolean.class, false);
+        this.basePackage = environment.getProperty(BASE_PACKAGE, String.class);
+        if (StringUtils.isBlank(basePackage)) {
+            this.basePackage = "com.qmth";
+        }
 
         this.baseline = environment.getProperty(BASELINE, Boolean.class, false);
         this.baselineAppCode = environment.getProperty(BASELINE_APP_CODE, String.class);
@@ -136,18 +133,14 @@ public class DataUpgradeProperties implements DataUpgradeConstants {
         this.driverClassName = databaseDriver.getDriverClassName();
     }
 
-    public Boolean getEnable() {
-        return enable;
+    public RunMode getRunMode() {
+        return runMode;
     }
 
     public String getBasePackage() {
         return basePackage;
     }
 
-    public Boolean getFirstInstall() {
-        return firstInstall;
-    }
-
     public Boolean getBaseline() {
         return baseline;
     }

+ 37 - 0
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/model/RunMode.java

@@ -0,0 +1,37 @@
+package com.qmth.boot.data.upgrade.model;
+
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * 运行模式
+ */
+public enum RunMode {
+
+    /**
+     * 启动应用
+     */
+    START,
+
+    /**
+     * 首次安装应用
+     */
+    INSTALL,
+
+    /**
+     * 升级应用
+     */
+    UPGRADE;
+
+    public static RunMode getRunMode(String str) {
+        if (StringUtils.isNotBlank(str)) {
+            for (RunMode runMode : RunMode.values()) {
+                if (runMode.name().equalsIgnoreCase(str)) {
+                    return runMode;
+                }
+            }
+        }
+
+        return null;
+    }
+
+}