Sfoglia il codice sorgente

init: init project step3: rebuild 8

shc 1 anno fa
parent
commit
ec021d8696

+ 164 - 163
hnqz-common/hnqz-common-sequence/src/main/java/com/qunzhixinxi/hnqz/common/sequence/range/impl/db/BaseDbHelper.java

@@ -12,168 +12,169 @@ import java.sql.*;
  */
 abstract class BaseDbHelper {
 
-	private static final long DELTA = 100000000L;
-
-	private final static String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS #tableName("
-			+ "id bigint(20) NOT NULL AUTO_INCREMENT," + "value bigint(20) NOT NULL," + "name varchar(32) NOT NULL,"
-			+ "gmt_create DATETIME NOT NULL," + "gmt_modified DATETIME NOT NULL,"
-			+ "PRIMARY KEY (`id`),UNIQUE uk_name (`name`)" + ")";
-
-	private final static String SQL_INSERT_RANGE = "INSERT IGNORE INTO #tableName(name,value,gmt_create,gmt_modified)"
-			+ " VALUE(?,?,?,?)";
-
-	private final static String SQL_UPDATE_RANGE = "UPDATE #tableName SET value=?,gmt_modified=? WHERE name=? AND "
-			+ "value=?";
-
-	private final static String SQL_SELECT_RANGE = "SELECT value FROM #tableName WHERE name=?";
-
-	/**
-	 * 创建表
-	 * @param dataSource DB来源
-	 * @param tableName 表名
-	 */
-	static void createTable(DataSource dataSource, String tableName) {
-
-		Connection conn = null;
-		Statement stmt = null;
-
-		try {
-			conn = dataSource.getConnection();
-			stmt = conn.createStatement();
-			stmt.executeUpdate(SQL_CREATE_TABLE.replace("#tableName", tableName));
-		}
-		catch (SQLException e) {
-			throw new SeqException(e);
-		}
-		finally {
-			closeQuietly(stmt);
-			closeQuietly(conn);
-		}
-	}
-
-	/**
-	 * 插入数据区间
-	 * @param dataSource DB来源
-	 * @param tableName 表名
-	 * @param name 区间名称
-	 * @param stepStart 初始位置
-	 */
-	private static void insertRange(DataSource dataSource, String tableName, String name, long stepStart) {
-
-		Connection conn = null;
-		PreparedStatement stmt = null;
-
-		try {
-			conn = dataSource.getConnection();
-			stmt = conn.prepareStatement(SQL_INSERT_RANGE.replace("#tableName", tableName));
-			stmt.setString(1, name);
-			stmt.setLong(2, stepStart);
-			stmt.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
-			stmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
-			stmt.executeUpdate();
-		}
-		catch (SQLException e) {
-			throw new SeqException(e);
-		}
-		finally {
-			closeQuietly(stmt);
-			closeQuietly(conn);
-		}
-	}
-
-	/**
-	 * 更新区间,乐观策略
-	 * @param dataSource DB来源
-	 * @param tableName 表名
-	 * @param newValue 更新新数据
-	 * @param oldValue 更新旧数据
-	 * @param name 区间名称
-	 * @return 成功/失败
-	 */
-	static boolean updateRange(DataSource dataSource, String tableName, Long newValue, Long oldValue, String name) {
-
-		Connection conn = null;
-		PreparedStatement stmt = null;
-
-		try {
-			conn = dataSource.getConnection();
-			stmt = conn.prepareStatement(SQL_UPDATE_RANGE.replace("#tableName", tableName));
-			stmt.setLong(1, newValue);
-			stmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
-			stmt.setString(3, name);
-			stmt.setLong(4, oldValue);
-			int affectedRows = stmt.executeUpdate();
-			return affectedRows > 0;
-		}
-		catch (SQLException e) {
-			throw new SeqException(e);
-		}
-		finally {
-			closeQuietly(stmt);
-			closeQuietly(conn);
-		}
-	}
-
-	/**
-	 * 查询区间,如果区间不存在,会新增一个区间,并返回null,由上层重新执行
-	 * @param dataSource DB来源
-	 * @param tableName 来源
-	 * @param name 区间名称
-	 * @param stepStart 初始位置
-	 * @return 区间值
-	 */
-	static Long selectRange(DataSource dataSource, String tableName, String name, long stepStart) {
-		Connection conn = null;
-		PreparedStatement stmt = null;
-		ResultSet rs = null;
-		long oldValue;
-
-		try {
-			conn = dataSource.getConnection();
-			stmt = conn.prepareStatement(SQL_SELECT_RANGE.replace("#tableName", tableName));
-			stmt.setString(1, name);
-
-			rs = stmt.executeQuery();
-			if (!rs.next()) {
-				// 没有此类型数据,需要初始化
-				insertRange(dataSource, tableName, name, stepStart);
-				return null;
-			}
-			oldValue = rs.getLong(1);
-
-			if (oldValue < 0) {
-				String msg = "Sequence value cannot be less than zero, value = " + oldValue
-						+ ", please check table sequence" + tableName;
-				throw new SeqException(msg);
-			}
-
-			if (oldValue > Long.MAX_VALUE - DELTA) {
-				String msg = "Sequence value overflow, value = " + oldValue + ", please check table sequence"
-						+ tableName;
-				throw new SeqException(msg);
-			}
-
-			return oldValue;
-		}
-		catch (SQLException e) {
-			throw new SeqException(e);
-		}
-		finally {
-			closeQuietly(rs);
-			closeQuietly(stmt);
-			closeQuietly(conn);
-		}
-	}
-
-	private static void closeQuietly(AutoCloseable closeable) {
-		if (null != closeable) {
-			try {
-				closeable.close();
-			}
-			catch (Throwable e) {
-				// Ignore
-			}
-		}
-	}
+    private static final long DELTA = 100000000L;
+
+    private final static String SQL_CREATE_TABLE =
+            "CREATE TABLE IF NOT EXISTS #tableName("
+                    + "id bigint(20) NOT NULL AUTO_INCREMENT,"
+                    + "value bigint(20) NOT NULL,"
+                    + "name varchar(32) NOT NULL,"
+                    + "gmt_create DATETIME NOT NULL,"
+                    + "gmt_modified DATETIME NOT NULL,"
+                    + "PRIMARY KEY (`id`),"
+					+ "UNIQUE KEY (`name`)"
+					+ ")";
+
+    private final static String SQL_INSERT_RANGE = "INSERT IGNORE INTO #tableName(name,value,gmt_create,gmt_modified)"
+            + " VALUE(?,?,?,?)";
+
+    private final static String SQL_UPDATE_RANGE = "UPDATE #tableName SET value=?,gmt_modified=? WHERE name=? AND "
+            + "value=?";
+
+    private final static String SQL_SELECT_RANGE = "SELECT value FROM #tableName WHERE name=?";
+
+    /**
+     * 创建表
+     *
+     * @param dataSource DB来源
+     * @param tableName  表名
+     */
+    static void createTable(DataSource dataSource, String tableName) {
+
+        Connection conn = null;
+        Statement stmt = null;
+
+        try {
+            conn = dataSource.getConnection();
+            stmt = conn.createStatement();
+            stmt.executeUpdate(SQL_CREATE_TABLE.replace("#tableName", tableName));
+        } catch (SQLException e) {
+            throw new SeqException(e);
+        } finally {
+            closeQuietly(stmt);
+            closeQuietly(conn);
+        }
+    }
+
+    /**
+     * 插入数据区间
+     *
+     * @param dataSource DB来源
+     * @param tableName  表名
+     * @param name       区间名称
+     * @param stepStart  初始位置
+     */
+    private static void insertRange(DataSource dataSource, String tableName, String name, long stepStart) {
+
+        Connection conn = null;
+        PreparedStatement stmt = null;
+
+        try {
+            conn = dataSource.getConnection();
+            stmt = conn.prepareStatement(SQL_INSERT_RANGE.replace("#tableName", tableName));
+            stmt.setString(1, name);
+            stmt.setLong(2, stepStart);
+            stmt.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
+            stmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
+            stmt.executeUpdate();
+        } catch (SQLException e) {
+            throw new SeqException(e);
+        } finally {
+            closeQuietly(stmt);
+            closeQuietly(conn);
+        }
+    }
+
+    /**
+     * 更新区间,乐观策略
+     *
+     * @param dataSource DB来源
+     * @param tableName  表名
+     * @param newValue   更新新数据
+     * @param oldValue   更新旧数据
+     * @param name       区间名称
+     * @return 成功/失败
+     */
+    static boolean updateRange(DataSource dataSource, String tableName, Long newValue, Long oldValue, String name) {
+
+        Connection conn = null;
+        PreparedStatement stmt = null;
+
+        try {
+            conn = dataSource.getConnection();
+            stmt = conn.prepareStatement(SQL_UPDATE_RANGE.replace("#tableName", tableName));
+            stmt.setLong(1, newValue);
+            stmt.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
+            stmt.setString(3, name);
+            stmt.setLong(4, oldValue);
+            int affectedRows = stmt.executeUpdate();
+            return affectedRows > 0;
+        } catch (SQLException e) {
+            throw new SeqException(e);
+        } finally {
+            closeQuietly(stmt);
+            closeQuietly(conn);
+        }
+    }
+
+    /**
+     * 查询区间,如果区间不存在,会新增一个区间,并返回null,由上层重新执行
+     *
+     * @param dataSource DB来源
+     * @param tableName  来源
+     * @param name       区间名称
+     * @param stepStart  初始位置
+     * @return 区间值
+     */
+    static Long selectRange(DataSource dataSource, String tableName, String name, long stepStart) {
+        Connection conn = null;
+        PreparedStatement stmt = null;
+        ResultSet rs = null;
+        long oldValue;
+
+        try {
+            conn = dataSource.getConnection();
+            stmt = conn.prepareStatement(SQL_SELECT_RANGE.replace("#tableName", tableName));
+            stmt.setString(1, name);
+
+            rs = stmt.executeQuery();
+            if (!rs.next()) {
+                // 没有此类型数据,需要初始化
+                insertRange(dataSource, tableName, name, stepStart);
+                return null;
+            }
+            oldValue = rs.getLong(1);
+
+            if (oldValue < 0) {
+                String msg = "Sequence value cannot be less than zero, value = " + oldValue
+                        + ", please check table sequence" + tableName;
+                throw new SeqException(msg);
+            }
+
+            if (oldValue > Long.MAX_VALUE - DELTA) {
+                String msg = "Sequence value overflow, value = " + oldValue + ", please check table sequence"
+                        + tableName;
+                throw new SeqException(msg);
+            }
+
+            return oldValue;
+        } catch (SQLException e) {
+            throw new SeqException(e);
+        } finally {
+            closeQuietly(rs);
+            closeQuietly(stmt);
+            closeQuietly(conn);
+        }
+    }
+
+    private static void closeQuietly(AutoCloseable closeable) {
+        if (null != closeable) {
+            try {
+                closeable.close();
+            } catch (Throwable e) {
+                // Ignore
+            }
+        }
+    }
 
 }

+ 8 - 24
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/config/SequenceConfig.java

@@ -1,15 +1,11 @@
 package com.qunzhixinxi.hnqz.admin.config;
 
-import cn.hutool.core.date.DateUtil;
-import com.qunzhixinxi.hnqz.common.data.tenant.TenantContextHolder;
-import com.qunzhixinxi.hnqz.common.sequence.builder.DbSeqBuilder;
-import com.qunzhixinxi.hnqz.common.sequence.properties.SequenceDbProperties;
+import com.qunzhixinxi.hnqz.common.sequence.builder.SnowflakeSeqBuilder;
+import com.qunzhixinxi.hnqz.common.sequence.properties.SequenceSnowflakeProperties;
 import com.qunzhixinxi.hnqz.common.sequence.sequence.Sequence;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
-import javax.sql.DataSource;
-
 /**
  * @author hnqz
  * @date 2019-05-26
@@ -22,45 +18,33 @@ public class SequenceConfig {
 	/**
 	 * 订单流水号发号器
 	 *
-	 * @param dataSource 数据源
 	 * @param properties 配置
 	 * @return 发号器
 	 */
 	@Bean
-	public Sequence paySequence(DataSource dataSource, SequenceDbProperties properties) {
-		return DbSeqBuilder.create()
-				.bizName(() -> String.format("payoff_%s_%s", TenantContextHolder.getTenantId(), DateUtil.today()))
-				.dataSource(dataSource).step(properties.getStep()).retryTimes(properties.getRetryTimes())
-				.tableName(properties.getTableName()).build();
+	public Sequence paySequence(SequenceSnowflakeProperties properties) {
+		return SnowflakeSeqBuilder.create().datacenterId(properties.getDatacenterId()).workerId(properties.getWorkerId()).build();
 	}
 
 	/**
 	 * 积分包发号器
 	 *
-	 * @param dataSource 数据源
 	 * @param properties 配置
 	 * @return 发号器
 	 */
 	@Bean(value = "pkgSequence")
-	public Sequence pkgSequence(DataSource dataSource, SequenceDbProperties properties) {
-		return DbSeqBuilder.create()
-				.bizName(() -> String.format("pkg_%s_%s", TenantContextHolder.getTenantId(), DateUtil.today()))
-				.dataSource(dataSource).step(properties.getStep()).retryTimes(properties.getRetryTimes())
-				.tableName(properties.getTableName()).build();
+	public Sequence pkgSequence(SequenceSnowflakeProperties properties) {
+		return SnowflakeSeqBuilder.create().datacenterId(properties.getDatacenterId()).workerId(properties.getWorkerId()).build();
 	}
 
 	/**
 	 * 任务发号器
 	 *
-	 * @param dataSource 数据源
 	 * @param properties 参数
 	 * @return 发号器
 	 */
 	@Bean(name = "taskSequence")
-	public Sequence taskSequence(DataSource dataSource, SequenceDbProperties properties) {
-		return DbSeqBuilder.create()
-				.bizName(() -> String.format("task_%s_%s", TenantContextHolder.getTenantId(), DateUtil.today()))
-				.dataSource(dataSource).step(properties.getStep()).retryTimes(properties.getRetryTimes())
-				.tableName(properties.getTableName()).build();
+	public Sequence taskSequence(SequenceSnowflakeProperties properties) {
+		return SnowflakeSeqBuilder.create().datacenterId(properties.getDatacenterId()).workerId(properties.getWorkerId()).build();
 	}
 }

+ 14 - 10
pom.xml

@@ -243,10 +243,11 @@
 				<profiles.active>dev</profiles.active>
 				<nacos.username>nacos</nacos.username>
 				<nacos.password>TGCzv6Rg2qimAJn</nacos.password>
+				<nacos.server-addr>localhost:8848</nacos.server-addr>
 				<profiles.namespace>a5e42a72-1f8f-44fc-bd2d-2dbc73cb327c</profiles.namespace>
 				<profiles.auth.port>3000</profiles.auth.port>
 				<profiles.gateway.port>9999</profiles.gateway.port>
-				<prefiles.upms.biz.port>4000</prefiles.upms.biz.port>
+				<profiles.upms.biz.port>4000</profiles.upms.biz.port>
 				<profiles.daemon.quartz.port>5007</profiles.daemon.quartz.port>
 			</properties>
 			<activation>
@@ -260,11 +261,12 @@
 				<profiles.active>demo</profiles.active>
 				<nacos.username>nacos</nacos.username>
 				<nacos.password>TGCzv6Rg2qimAJn</nacos.password>
+				<nacos.server-addr>localhost:8848</nacos.server-addr>
 				<profiles.namespace>a5e42a72-1f8f-44fc-bd2d-2dbc73cb327c</profiles.namespace>
-				<profiles.auth.port>3000</profiles.auth.port>
-				<profiles.gateway.port>9999</profiles.gateway.port>
-				<prefiles.upms.biz.port>4000</prefiles.upms.biz.port>
-				<profiles.daemon.quartz.port>5007</profiles.daemon.quartz.port>
+				<profiles.auth.port>23000</profiles.auth.port>
+				<profiles.gateway.port>29999</profiles.gateway.port>
+				<profiles.upms.biz.port>24000</profiles.upms.biz.port>
+				<profiles.daemon.quartz.port>25007</profiles.daemon.quartz.port>
 			</properties>
 		</profile>
 		<profile>
@@ -273,11 +275,12 @@
 				<profiles.active>pre</profiles.active>
 				<nacos.username>nacos</nacos.username>
 				<nacos.password>TGCzv6Rg2qimAJn</nacos.password>
+				<nacos.server-addr>localhost:8848</nacos.server-addr>
 				<profiles.namespace>a5e42a72-1f8f-44fc-bd2d-2dbc73cb327c</profiles.namespace>
-				<profiles.auth.port>7001</profiles.auth.port>
-				<profiles.gateway.port>7002</profiles.gateway.port>
-				<prefiles.upms.biz.port>7003</prefiles.upms.biz.port>
-				<profiles.daemon.quartz.port>7004</profiles.daemon.quartz.port>
+				<profiles.auth.port>13000</profiles.auth.port>
+				<profiles.gateway.port>19999</profiles.gateway.port>
+				<profiles.upms.biz.port>14000</profiles.upms.biz.port>
+				<profiles.daemon.quartz.port>15007</profiles.daemon.quartz.port>
 			</properties>
 		</profile>
 		<profile>
@@ -286,10 +289,11 @@
 				<profiles.active>prod</profiles.active>
 				<nacos.username>nacos</nacos.username>
 				<nacos.password>TGCzv6Rg2qimAJn</nacos.password>
+				<nacos.server-addr>localhost:8848</nacos.server-addr>
 				<profiles.namespace>a5e42a72-1f8f-44fc-bd2d-2dbc73cb327c</profiles.namespace>
 				<profiles.auth.port>3000</profiles.auth.port>
 				<profiles.gateway.port>9999</profiles.gateway.port>
-				<prefiles.upms.biz.port>4000</prefiles.upms.biz.port>
+				<profiles.upms.biz.port>4000</profiles.upms.biz.port>
 				<profiles.daemon.quartz.port>5007</profiles.daemon.quartz.port>
 			</properties>
 		</profile>