mirror of
https://gitee.com/wangkang/udf.git
synced 2026-10-31 23:33:40 +00:00
更新spring boot版本
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
<module>udf-starter-mail</module>
|
||||
<module>udf-starter-scheduler</module>
|
||||
<module>udf-starter-scheduler-client</module>
|
||||
<module>udf-starter-id</module>
|
||||
</modules>
|
||||
<!-- 依赖管理 -->
|
||||
<dependencyManagement>
|
||||
@@ -101,6 +102,12 @@
|
||||
<artifactId>udf-starter-scheduler-client</artifactId>
|
||||
<version>${udf-starter.version}</version>
|
||||
</dependency>
|
||||
<!-- udf-starter-id -->
|
||||
<dependency>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter-id</artifactId>
|
||||
<version>${udf-starter.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<!-- 构建 -->
|
||||
|
||||
@@ -83,15 +83,4 @@ public class BaseApplication {
|
||||
requestFactory.setBufferRequestBody(applicationConfig.getBufferRequestBody());
|
||||
return requestFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 分布式ID生成器
|
||||
*
|
||||
* @return 分布式ID生成器
|
||||
*/
|
||||
@Bean
|
||||
public IdWorker idWorker() {
|
||||
return new IdWorker();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+20
-21
@@ -1,24 +1,23 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<!-- maven坐标 -->
|
||||
<artifactId>udf-starter-cors</artifactId>
|
||||
<name>udf-starter-cors</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://www.egridcloud.com</url>
|
||||
<!-- 父依赖 -->
|
||||
<parent>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- udf-core-starter -->
|
||||
<dependency>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<!-- maven坐标 -->
|
||||
<artifactId>udf-starter-cors</artifactId>
|
||||
<name>udf-starter-cors</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://www.egridcloud.com</url>
|
||||
<!-- 父依赖 -->
|
||||
<parent>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="true">
|
||||
<fileset name="all" enabled="true" check-config-name="itkk.org" local="false">
|
||||
<file-match-pattern match-pattern="." include-pattern="true"/>
|
||||
</fileset>
|
||||
</fileset-config>
|
||||
@@ -0,0 +1,4 @@
|
||||
/target/
|
||||
/bin/
|
||||
/.classpath
|
||||
/.project
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<!-- maven坐标 -->
|
||||
<artifactId>udf-starter-id</artifactId>
|
||||
<name>udf-starter-id</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://www.egridcloud.com</url>
|
||||
<!-- 父依赖 -->
|
||||
<parent>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+283
-249
@@ -1,249 +1,283 @@
|
||||
/**
|
||||
* IdWorker.java
|
||||
* Created at 2016-11-01
|
||||
* Created by wangkang
|
||||
* Copyright (C) 2016 itkk.org, All rights reserved.
|
||||
*/
|
||||
package org.itkk.udf.core;
|
||||
|
||||
import org.itkk.udf.core.exception.SystemRuntimeException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 描述 : 分布式id生成器,基于twiter的snowflake
|
||||
*
|
||||
* @author wangkang
|
||||
*/
|
||||
public class IdWorker {
|
||||
|
||||
/**
|
||||
* 描述 : 日志
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(IdWorker.class);
|
||||
|
||||
/**
|
||||
* 描述 : 机器ID
|
||||
*/
|
||||
private long workerId;
|
||||
|
||||
/**
|
||||
* 描述 : 数据中心ID
|
||||
*/
|
||||
private long datacenterId;
|
||||
|
||||
/**
|
||||
* 描述 : 并发控制
|
||||
*/
|
||||
private long sequence = 0L;
|
||||
|
||||
/**
|
||||
* 描述 : 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
|
||||
*/
|
||||
private long twepoch = 1288834974657L;
|
||||
|
||||
/**
|
||||
* 描述 : 机器标识位数
|
||||
*/
|
||||
private long workerIdBits = 5L;
|
||||
|
||||
/**
|
||||
* 描述 : 数据中心标识位数
|
||||
*/
|
||||
private long datacenterIdBits = 5L;
|
||||
|
||||
/**
|
||||
* 描述 : 机器ID最大值
|
||||
*/
|
||||
private long maxWorkerId = -1L ^ (-1L << workerIdBits);
|
||||
|
||||
/**
|
||||
* 描述 : 数据中心ID最大值
|
||||
*/
|
||||
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
|
||||
|
||||
/**
|
||||
* 描述 : 毫秒内自增位
|
||||
*/
|
||||
private long sequenceBits = 12L;
|
||||
|
||||
/**
|
||||
* 描述 : 机器ID偏左移12位
|
||||
*/
|
||||
private long workerIdShift = sequenceBits;
|
||||
|
||||
/**
|
||||
* 描述 : 数据中心ID左移17位
|
||||
*/
|
||||
private long datacenterIdShift = sequenceBits + workerIdBits;
|
||||
|
||||
/**
|
||||
* 描述 : 时间毫秒左移22位
|
||||
*/
|
||||
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
|
||||
|
||||
/**
|
||||
* 描述 : sequenceMask
|
||||
*/
|
||||
private long sequenceMask = -1L ^ (-1L << sequenceBits);
|
||||
|
||||
/**
|
||||
* 描述 : 上次生产id时间戳
|
||||
*/
|
||||
private long lastTimestamp = -1L;
|
||||
|
||||
/**
|
||||
* 描述 : 构造函数
|
||||
*/
|
||||
public IdWorker() {
|
||||
this.datacenterId = getDatacenterId(maxDatacenterId);
|
||||
this.workerId = getWorkerId(maxWorkerId);
|
||||
LOGGER.info("datacenterId:{},workerId:{}", this.datacenterId, this.workerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 构造函数
|
||||
*
|
||||
* @param workerId workerId
|
||||
* @param datacenterId datacenterId
|
||||
*/
|
||||
public IdWorker(long workerId, long datacenterId) {
|
||||
// sanity check for workerId
|
||||
if (workerId > maxWorkerId || workerId < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
|
||||
}
|
||||
if (datacenterId > maxDatacenterId || datacenterId < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
|
||||
}
|
||||
this.workerId = workerId;
|
||||
this.datacenterId = datacenterId;
|
||||
LOGGER.info("datacenterId:{},workerId:{}", maxDatacenterId, maxWorkerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 下一个ID
|
||||
*
|
||||
* @return ID
|
||||
*/
|
||||
public synchronized long nextId() {
|
||||
long timestamp = timeGen();
|
||||
if (timestamp < lastTimestamp) {
|
||||
throw new SystemRuntimeException(
|
||||
String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
|
||||
lastTimestamp - timestamp));
|
||||
}
|
||||
if (lastTimestamp == timestamp) {
|
||||
sequence = (sequence + 1) & sequenceMask;
|
||||
if (sequence == 0) {
|
||||
timestamp = tilNextMillis(lastTimestamp);
|
||||
}
|
||||
} else {
|
||||
sequence = 0L;
|
||||
}
|
||||
lastTimestamp = timestamp;
|
||||
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift)
|
||||
| (workerId << workerIdShift) | sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获得下一个毫秒数
|
||||
*
|
||||
* @param lastTimestampParam lastTimestampParam
|
||||
* @return 下一个毫秒数
|
||||
*/
|
||||
protected long tilNextMillis(long lastTimestampParam) {
|
||||
long timestamp = timeGen();
|
||||
while (timestamp <= lastTimestampParam) {
|
||||
timestamp = timeGen();
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获得当前时间毫秒数
|
||||
*
|
||||
* @return 当前时间毫秒数
|
||||
*/
|
||||
protected long timeGen() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取机器ID
|
||||
*
|
||||
* @param maxWorkerIdparam maxWorkerIdparam
|
||||
* @return 机器ID
|
||||
*/
|
||||
protected long getWorkerId(long maxWorkerIdparam) {
|
||||
return Math.abs(UUID.randomUUID().toString().hashCode() % maxWorkerIdparam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取数据中心ID
|
||||
*
|
||||
* @param maxDatacenterIdparam maxDatacenterIdparam
|
||||
* @return 数据中心ID
|
||||
*/
|
||||
protected long getDatacenterId(long maxDatacenterIdparam) {
|
||||
return Math.abs(UUID.randomUUID().toString().hashCode() % maxDatacenterIdparam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取机器ID(备用,docker容器中有问题)
|
||||
*
|
||||
* @param datacenterIdparam datacenterIdparam
|
||||
* @param maxWorkerIdparam maxWorkerIdparam
|
||||
* @return 机器ID
|
||||
*/
|
||||
protected long getWorkerIdBack(long datacenterIdparam, long maxWorkerIdparam) {
|
||||
final int numbera = 0xffff;
|
||||
StringBuilder mpid = new StringBuilder();
|
||||
mpid.append(datacenterIdparam);
|
||||
String name = ManagementFactory.getRuntimeMXBean().getName();
|
||||
if (!name.isEmpty()) {
|
||||
//GET jvmPid
|
||||
mpid.append(name.split("@")[0]);
|
||||
}
|
||||
//MAC + PID 的 hashcode 获取16个低位
|
||||
return (mpid.toString().hashCode() & numbera) % (maxWorkerIdparam + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取数据中心ID(备用,docker容器中有问题)
|
||||
*
|
||||
* @param maxDatacenterIdparam maxDatacenterIdparam
|
||||
* @return 数据中心ID
|
||||
*/
|
||||
protected long getDatacenterIdBack(long maxDatacenterIdparam) {
|
||||
final int numbera = 0x000000FF;
|
||||
final int numberb = 0x0000FF00;
|
||||
final int number2 = 2;
|
||||
final int number6 = 6;
|
||||
final int number8 = 8;
|
||||
long id;
|
||||
try {
|
||||
InetAddress ip = InetAddress.getLocalHost();
|
||||
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
|
||||
if (network == null) {
|
||||
id = 1L;
|
||||
} else {
|
||||
byte[] mac = network.getHardwareAddress();
|
||||
id = ((numbera & (long) mac[mac.length - 1])
|
||||
| (numberb & (((long) mac[mac.length - number2]) << number8))) >> number6;
|
||||
id = id % (maxDatacenterIdparam + 1);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new SystemRuntimeException(e);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* IdWorker.java
|
||||
* Created at 2016-11-01
|
||||
* Created by wangkang
|
||||
* Copyright (C) 2016 itkk.org, All rights reserved.
|
||||
*/
|
||||
package org.itkk.udf.id;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.itkk.udf.core.exception.SystemRuntimeException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 描述 : 分布式id生成器,基于twiter的snowflake
|
||||
*
|
||||
* @author wangkang
|
||||
*/
|
||||
public class IdWorker {
|
||||
|
||||
/**
|
||||
* uuid类型
|
||||
*/
|
||||
public static final String UUID_TYPE = "uuid";
|
||||
|
||||
/**
|
||||
* uuid类型
|
||||
*/
|
||||
public static final String MAC_PID_TYPE = "mac_pid";
|
||||
|
||||
/**
|
||||
* 描述 : 日志
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(IdWorker.class);
|
||||
|
||||
/**
|
||||
* 描述 : 机器ID
|
||||
*/
|
||||
private long workerId;
|
||||
|
||||
/**
|
||||
* 描述 : 数据中心ID
|
||||
*/
|
||||
private long datacenterId;
|
||||
|
||||
/**
|
||||
* 描述 : 并发控制
|
||||
*/
|
||||
private long sequence = 0L;
|
||||
|
||||
/**
|
||||
* 描述 : 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
|
||||
*/
|
||||
private long twepoch = 1288834974657L;
|
||||
|
||||
/**
|
||||
* 描述 : 机器标识位数
|
||||
*/
|
||||
private long workerIdBits = 5L;
|
||||
|
||||
/**
|
||||
* 描述 : 数据中心标识位数
|
||||
*/
|
||||
private long datacenterIdBits = 5L;
|
||||
|
||||
/**
|
||||
* 描述 : 机器ID最大值
|
||||
*/
|
||||
private long maxWorkerId = -1L ^ (-1L << workerIdBits);
|
||||
|
||||
/**
|
||||
* 描述 : 数据中心ID最大值
|
||||
*/
|
||||
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
|
||||
|
||||
/**
|
||||
* 描述 : 毫秒内自增位
|
||||
*/
|
||||
private long sequenceBits = 12L;
|
||||
|
||||
/**
|
||||
* 描述 : 机器ID偏左移12位
|
||||
*/
|
||||
private long workerIdShift = sequenceBits;
|
||||
|
||||
/**
|
||||
* 描述 : 数据中心ID左移17位
|
||||
*/
|
||||
private long datacenterIdShift = sequenceBits + workerIdBits;
|
||||
|
||||
/**
|
||||
* 描述 : 时间毫秒左移22位
|
||||
*/
|
||||
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
|
||||
|
||||
/**
|
||||
* 描述 : sequenceMask
|
||||
*/
|
||||
private long sequenceMask = -1L ^ (-1L << sequenceBits);
|
||||
|
||||
/**
|
||||
* 描述 : 上次生产id时间戳
|
||||
*/
|
||||
private long lastTimestamp = -1L;
|
||||
|
||||
/**
|
||||
* 描述 : 构造函数
|
||||
*/
|
||||
public IdWorker() {
|
||||
this(IdWorker.UUID_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param type ID构造类型
|
||||
*/
|
||||
public IdWorker(String type) {
|
||||
if (StringUtils.isNotBlank(type)) {
|
||||
switch (type) {
|
||||
case IdWorker.UUID_TYPE:
|
||||
this.datacenterId = getDatacenterId(maxDatacenterId);
|
||||
this.workerId = getWorkerId(maxWorkerId);
|
||||
break;
|
||||
case IdWorker.MAC_PID_TYPE: //DOCKER容器下有问题
|
||||
this.datacenterId = getDatacenterIdBack(maxDatacenterId);
|
||||
this.workerId = getWorkerIdBack(this.datacenterId, maxWorkerId);
|
||||
break;
|
||||
default:
|
||||
throw new SystemRuntimeException("unknow type");
|
||||
}
|
||||
} else {
|
||||
throw new SystemRuntimeException("type must not be null");
|
||||
}
|
||||
LOGGER.info("type:{},datacenterId:{},workerId:{}", type, this.datacenterId, this.workerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 构造函数
|
||||
*
|
||||
* @param workerId workerId
|
||||
* @param datacenterId datacenterId
|
||||
*/
|
||||
public IdWorker(long workerId, long datacenterId) {
|
||||
// sanity check for workerId
|
||||
if (workerId > maxWorkerId || workerId < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
|
||||
}
|
||||
if (datacenterId > maxDatacenterId || datacenterId < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
|
||||
}
|
||||
this.workerId = workerId;
|
||||
this.datacenterId = datacenterId;
|
||||
LOGGER.info("datacenterId:{},workerId:{}", maxDatacenterId, maxWorkerId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 下一个ID
|
||||
*
|
||||
* @return ID
|
||||
*/
|
||||
public synchronized long nextId() {
|
||||
long timestamp = timeGen();
|
||||
if (timestamp < lastTimestamp) {
|
||||
throw new SystemRuntimeException(
|
||||
String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",
|
||||
lastTimestamp - timestamp));
|
||||
}
|
||||
if (lastTimestamp == timestamp) {
|
||||
sequence = (sequence + 1) & sequenceMask;
|
||||
if (sequence == 0) {
|
||||
timestamp = tilNextMillis(lastTimestamp);
|
||||
}
|
||||
} else {
|
||||
sequence = 0L;
|
||||
}
|
||||
lastTimestamp = timestamp;
|
||||
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift)
|
||||
| (workerId << workerIdShift) | sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获得下一个毫秒数
|
||||
*
|
||||
* @param lastTimestampParam lastTimestampParam
|
||||
* @return 下一个毫秒数
|
||||
*/
|
||||
protected long tilNextMillis(long lastTimestampParam) {
|
||||
long timestamp = timeGen();
|
||||
while (timestamp <= lastTimestampParam) {
|
||||
timestamp = timeGen();
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获得当前时间毫秒数
|
||||
*
|
||||
* @return 当前时间毫秒数
|
||||
*/
|
||||
protected long timeGen() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取机器ID
|
||||
*
|
||||
* @param maxWorkerIdparam maxWorkerIdparam
|
||||
* @return 机器ID
|
||||
*/
|
||||
protected long getWorkerId(long maxWorkerIdparam) {
|
||||
return Math.abs(UUID.randomUUID().toString().hashCode() % maxWorkerIdparam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取数据中心ID
|
||||
*
|
||||
* @param maxDatacenterIdparam maxDatacenterIdparam
|
||||
* @return 数据中心ID
|
||||
*/
|
||||
protected long getDatacenterId(long maxDatacenterIdparam) {
|
||||
return Math.abs(UUID.randomUUID().toString().hashCode() % maxDatacenterIdparam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取机器ID(备用,docker容器中有问题)
|
||||
*
|
||||
* @param datacenterIdparam datacenterIdparam
|
||||
* @param maxWorkerIdparam maxWorkerIdparam
|
||||
* @return 机器ID
|
||||
*/
|
||||
protected long getWorkerIdBack(long datacenterIdparam, long maxWorkerIdparam) {
|
||||
final int numbera = 0xffff;
|
||||
StringBuilder mpid = new StringBuilder();
|
||||
mpid.append(datacenterIdparam);
|
||||
String name = ManagementFactory.getRuntimeMXBean().getName();
|
||||
if (!name.isEmpty()) {
|
||||
//GET jvmPid
|
||||
mpid.append(name.split("@")[0]);
|
||||
}
|
||||
//MAC + PID 的 hashcode 获取16个低位
|
||||
return (mpid.toString().hashCode() & numbera) % (maxWorkerIdparam + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述 : 获取数据中心ID(备用,docker容器中有问题)
|
||||
*
|
||||
* @param maxDatacenterIdparam maxDatacenterIdparam
|
||||
* @return 数据中心ID
|
||||
*/
|
||||
protected long getDatacenterIdBack(long maxDatacenterIdparam) {
|
||||
final int numbera = 0x000000FF;
|
||||
final int numberb = 0x0000FF00;
|
||||
final int number2 = 2;
|
||||
final int number6 = 6;
|
||||
final int number8 = 8;
|
||||
long id;
|
||||
try {
|
||||
InetAddress ip = InetAddress.getLocalHost();
|
||||
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
|
||||
if (network == null) {
|
||||
id = 1L;
|
||||
} else {
|
||||
byte[] mac = network.getHardwareAddress();
|
||||
id = ((numbera & (long) mac[mac.length - 1])
|
||||
| (numberb & (((long) mac[mac.length - number2]) << number8))) >> number6;
|
||||
id = id % (maxDatacenterIdparam + 1);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new SystemRuntimeException(e);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.itkk.udf.id;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* IdWorkerConfig
|
||||
*/
|
||||
@Configuration
|
||||
public class IdWorkerConfig {
|
||||
|
||||
/**
|
||||
* idWorkerProperties
|
||||
*/
|
||||
@Autowired
|
||||
private IdWorkerProperties idWorkerProperties;
|
||||
|
||||
/**
|
||||
* 描述 : 分布式ID生成器
|
||||
*
|
||||
* @return 分布式ID生成器
|
||||
*/
|
||||
@Bean
|
||||
public IdWorker idWorker() {
|
||||
return new IdWorker(idWorkerProperties.getType());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.itkk.udf.id;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* IdWorkerProperties
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "org.itkk.id.properties")
|
||||
@Validated
|
||||
public class IdWorkerProperties {
|
||||
|
||||
/**
|
||||
* ID构造类型
|
||||
*/
|
||||
@NotNull
|
||||
private String type = IdWorker.MAC_PID_TYPE;
|
||||
|
||||
/**
|
||||
* D构造类型
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* D构造类型
|
||||
*
|
||||
* @param type type
|
||||
*/
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* IIdWorkerController.java
|
||||
* Created at 2017-05-26
|
||||
* Created by Administrator
|
||||
* Copyright (C) 2016 itkk.org, All rights reserved.
|
||||
*/
|
||||
package org.itkk.udf.id.web;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.itkk.udf.core.RestResponse;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 描述 : IIdWorkerController
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
@Api(value = "分布式ID服务", consumes = "application/json", produces = "application/json", protocols = "http")
|
||||
@RequestMapping(value = "/service/id")
|
||||
public interface IIdWorkerController {
|
||||
|
||||
/**
|
||||
* 获得分布式ID
|
||||
*
|
||||
* @return ID
|
||||
*/
|
||||
@ApiOperation(value = "ID_1", notes = "获得分布式ID(注意:ID为长整型,在javascript下会丢失精度,请注意数据类型转换)")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsApplicationName", value = "rms应用名称", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsSign", value = "rms认证秘钥", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsServiceCode", value = "rms接口编号", required = true, dataType = "string")
|
||||
})
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
RestResponse<String> get();
|
||||
|
||||
/**
|
||||
* 批量获得分布式ID
|
||||
*
|
||||
* @param count 数量
|
||||
* @return ID
|
||||
*/
|
||||
@ApiOperation(value = "ID_2", notes = "批量获得分布式ID(注意:ID为长整型,在javascript下会丢失精度,请注意数据类型转换)")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsApplicationName", value = "rms应用名称", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsSign", value = "rms认证秘钥", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsServiceCode", value = "rms接口编号", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(paramType = "path", name = "count", value = "数量", required = true, dataType = "string")
|
||||
})
|
||||
@RequestMapping(value = "{count}", method = RequestMethod.GET)
|
||||
RestResponse<List<String>> get(@PathVariable Integer count);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* MailController.java
|
||||
* Created at 2017-05-26
|
||||
* Created by Administrator
|
||||
* Copyright (C) 2016 itkk.org, All rights reserved.
|
||||
*/
|
||||
package org.itkk.udf.id.web;
|
||||
|
||||
import org.itkk.udf.core.RestResponse;
|
||||
import org.itkk.udf.id.IdWorker;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 描述 : IdWorkerController
|
||||
*
|
||||
* @author Administrator
|
||||
*/
|
||||
@RestController
|
||||
public class IdWorkerController implements IIdWorkerController {
|
||||
|
||||
/**
|
||||
* idWorker
|
||||
*/
|
||||
@Autowired
|
||||
private IdWorker idWorker;
|
||||
|
||||
@Override
|
||||
public RestResponse<String> get() {
|
||||
return new RestResponse<>(Long.toString(idWorker.nextId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestResponse<List<String>> get(@PathVariable Integer count) {
|
||||
List<String> ids = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
ids.add(Long.toString(idWorker.nextId()));
|
||||
}
|
||||
return new RestResponse<>(ids);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user