@@ -3,6 +3,7 @@ using System.Collections; | |||
using System.Collections.Generic; | |||
using System.Runtime.InteropServices; | |||
using System.Threading; | |||
using System.Threading.Tasks; | |||
using Yitter.IdGenerator; | |||
namespace Yitter.OrgSystem.TestA | |||
@@ -54,20 +55,23 @@ namespace Yitter.OrgSystem.TestA | |||
while (true) | |||
{ | |||
RunSingle(); | |||
// Go(options); | |||
//CallDll(); | |||
//RunSingle(); | |||
//Go(options); | |||
CallDll(); | |||
Thread.Sleep(1000); // 每隔1秒执行一次Go | |||
} | |||
} | |||
[DllImport("yitidgenc.dll", CallingConvention = CallingConvention.Cdecl)] | |||
//[DllImport("yitidgenc.dll", CallingConvention = CallingConvention.StdCall)] | |||
//public static extern long NextId(); | |||
[DllImport("yitidgenc.dll", CallingConvention = CallingConvention.StdCall)] | |||
public static extern long NextId(); | |||
[DllImport("yitidgenc.dll", CallingConvention = CallingConvention.Cdecl)] | |||
[DllImport("yitidgenc.dll", CallingConvention = CallingConvention.StdCall)] | |||
public static extern void SetWorkerId(uint workerId); | |||
[DllImport("yitidgenc.dll", CallingConvention = CallingConvention.Cdecl)] | |||
[DllImport("yitidgenc.dll", CallingConvention = CallingConvention.StdCall)] | |||
public static extern int TestId(); | |||
private static void CallDll() | |||
@@ -78,15 +82,27 @@ namespace Yitter.OrgSystem.TestA | |||
int i = 0; | |||
long id = 0; | |||
DateTime start = DateTime.Now; | |||
bool useMultiThread = false; | |||
var ids = TestId(); | |||
//var ids = TestId(); | |||
//SetWorkerId(1); | |||
while (i < 50000) | |||
{ | |||
id = NextId(); | |||
i++; | |||
if (useMultiThread) | |||
{ | |||
Task.Run(() => | |||
{ | |||
id = NextId(); | |||
Console.WriteLine("id:" + id); | |||
}); | |||
} | |||
else | |||
{ | |||
id = NextId(); | |||
} | |||
} | |||
DateTime end = DateTime.Now; | |||
Console.WriteLine("id:" + id); | |||
@@ -0,0 +1,8 @@ | |||
{ | |||
"profiles": { | |||
"Yitter.IdGenTest": { | |||
"commandName": "Project", | |||
"nativeDebugging": true | |||
} | |||
} | |||
} |
@@ -12,6 +12,10 @@ | |||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | |||
</PropertyGroup> | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | |||
<PlatformTarget>x64</PlatformTarget> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\Yitter.IdGenerator\Yitter.IdGenerator.csproj" /> | |||
</ItemGroup> | |||
@@ -9,13 +9,13 @@ | |||
TAP_DLLEXPORT | |||
extern void SetIdGenerator(IdGeneratorOptions options); | |||
extern void TAP_STDCALL SetIdGenerator(IdGeneratorOptions options); | |||
TAP_DLLEXPORT | |||
extern void SetWorkerId(uint32_t workerId); | |||
extern void TAP_STDCALL SetWorkerId(uint32_t workerId); | |||
TAP_DLLEXPORT | |||
extern uint64_t NextId(); | |||
extern uint64_t TAP_STDCALL NextId(); | |||
TAP_DLLEXPORT | |||
extern uint64_t TestId() ; | |||
extern uint64_t TAP_STDCALL TestId(); |
@@ -5,9 +5,11 @@ | |||
#ifdef _WIN32 | |||
#define TAP_CDECL __cdecl | |||
#define TAP_STDCALL __stdcall | |||
#define TAP_DLLEXPORT __declspec(dllexport) | |||
#else | |||
#define TAP_CDECL | |||
#define TAP_STDCALL | |||
#define TAP_DLLEXPORT | |||
#endif | |||
@@ -24,7 +24,7 @@ void RunMultiThread() { | |||
//int64_t start = GetCurrentMicroTime(); | |||
for (int i = 0; i < GenIdCount / threadCount; i++) { | |||
int64_t id = NextId(); | |||
printf("生成ID: %D\n", id); | |||
printf("ID: %D\n", id); | |||
} | |||
int64_t end = GetCurrentMicroTime(); | |||
@@ -35,11 +35,11 @@ void RunSingle() { | |||
int64_t start = GetCurrentMicroTime(); | |||
for (int i = 0; i < GenIdCount; i++) { | |||
int64_t id = NextId(); | |||
// printf("生成ID: %ld\n", id); | |||
// printf("ID: %ld\n", id); | |||
} | |||
int64_t end = GetCurrentMicroTime(); | |||
printf("%s,total:%d μs\n", method == 1 ? "1" : "2", (end - start)); | |||
printf("%s, total: %d us\n", method == 1 ? "1" : "2", (end - start)); | |||
} | |||
int main() { | |||
@@ -1,5 +1,11 @@ | |||
# idgenerator | |||
## | |||
Go集成专项工程入口:https://gitee.com/yitter/idgenerator-go | |||
后文内容以 Go 专项工程为准。 | |||
## Go环境 | |||
1.SDK,go1.16 | |||
@@ -1,12 +1,19 @@ | |||
package main | |||
import ( | |||
"C" | |||
"fmt" | |||
"time" | |||
"yitidgen/contract" | |||
"yitidgen/gen" | |||
) | |||
//export NextId | |||
func NextId() uint64{ | |||
return gen.GetIns().NextId() | |||
} | |||
func main() { | |||
// 方法一:直接采用默认方法生成一个Id | |||
var yid = gen.YitIdHelper{} | |||
@@ -5,7 +5,12 @@ JDK 1.8+ | |||
## 引用 maven 包 | |||
``` | |||
<dependency> | |||
<groupId>com.github.yitter</groupId> | |||
<artifactId>yitter-idgenerator</artifactId> | |||
<version>1.0.1</version> | |||
<type>pom</type> | |||
</dependency> | |||
``` | |||
## 调用示例 | |||
@@ -4,23 +4,40 @@ | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||
<modelVersion>4.0.0</modelVersion> | |||
<groupId>com.yitter</groupId> | |||
<artifactId>yitter.idgenerator</artifactId> | |||
<groupId>com.github.yitter</groupId> | |||
<artifactId>yitter-idgenerator</artifactId> | |||
<packaging>jar</packaging> | |||
<version>1.0.0</version> | |||
<version>1.0.1</version> | |||
<name>yitter.idgenerator</name> | |||
<description>Shorter ID and faster generation with a new snowflake drift algorithm. The core is to shorten the ID length, but also can have a very high instantaneous concurrent processing capacity (50W/0.1s), and powerful configuration capacity.</description> | |||
<name>yitter-idgenerator</name> | |||
<description>Shorter ID and faster generation with a new snowflake drift algorithm. The core is to shorten the ID | |||
length, but also can have a very high instantaneous concurrent processing capacity (50W/0.1s), and powerful | |||
configuration capacity. | |||
</description> | |||
<url>https://github.com/yitter/idgenerator</url> | |||
<developers> | |||
<developer> | |||
<id>yitter</id> | |||
<name>yitter</name> | |||
<email>yitter@126.com</email> | |||
<url>https://gitee.com/yitter/idgenerator</url> | |||
<url>https://github.com/yitter/idgenerator</url> | |||
</developer> | |||
</developers> | |||
<scm> | |||
<connection>scm:git:https://github.com/yitter/idgenerator.git</connection> | |||
<developerConnection>scm:git:https://github.com/yitter/idgenerator.git</developerConnection> | |||
<url>https://github.com/yitter/idgenerator.git</url> | |||
</scm> | |||
<licenses> | |||
<license> | |||
<name>MIT</name> | |||
<url>https://github.com/yitter/IdGenerator/blob/main/LICENSE</url> | |||
</license> | |||
</licenses> | |||
<properties> | |||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||
@@ -30,64 +47,85 @@ | |||
</properties> | |||
<build> | |||
<pluginManagement> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-compiler-plugin</artifactId> | |||
<version>3.8.0</version> | |||
<configuration> | |||
<source>${java.version}</source> | |||
<target>${java.version}</target> | |||
<encoding>UTF-8</encoding> | |||
</configuration> | |||
</plugin> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-jar-plugin</artifactId> | |||
<version>3.1.0</version> | |||
</plugin> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-surefire-plugin</artifactId> | |||
<configuration> | |||
<skip>true</skip> | |||
</configuration> | |||
</plugin> | |||
</plugins> | |||
</pluginManagement> | |||
<resources> | |||
<resource> | |||
<directory>${project.basedir}</directory> | |||
<targetPath>META-INF</targetPath> | |||
<includes> | |||
<include>LICENSE</include> | |||
<include>NOTICE</include> | |||
</includes> | |||
</resource> | |||
<resource> | |||
<directory>src/main/resources</directory> | |||
<filtering>true</filtering> | |||
<includes> | |||
<include>**</include> | |||
</includes> | |||
</resource> | |||
</resources> | |||
<testResources> | |||
<testResource> | |||
<directory>src/test/resources</directory> | |||
<includes> | |||
<include>**</include> | |||
</includes> | |||
</testResource> | |||
</testResources> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-source-plugin</artifactId> | |||
<version>2.2.1</version> | |||
<executions> | |||
<execution> | |||
<phase>package</phase> | |||
<goals> | |||
<goal>jar-no-fork</goal> | |||
</goals> | |||
</execution> | |||
</executions> | |||
</plugin> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-javadoc-plugin</artifactId> | |||
<version>3.0.1</version> | |||
<executions> | |||
<execution> | |||
<phase>package</phase> | |||
<goals> | |||
<goal>jar</goal> | |||
</goals> | |||
</execution> | |||
</executions> | |||
</plugin> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-gpg-plugin</artifactId> | |||
<version>1.5</version> | |||
<executions> | |||
<execution> | |||
<phase>verify</phase> | |||
<goals> | |||
<goal>sign</goal> | |||
</goals> | |||
</execution> | |||
</executions> | |||
</plugin> | |||
<plugin> | |||
<groupId>org.sonatype.plugins</groupId> | |||
<artifactId>nexus-staging-maven-plugin</artifactId> | |||
<version>1.6.8</version> | |||
<extensions>true</extensions> | |||
<configuration> | |||
<serverId>ossrh</serverId> | |||
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl> | |||
<autoReleaseAfterClose>false</autoReleaseAfterClose> | |||
</configuration> | |||
</plugin> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-compiler-plugin</artifactId> | |||
<version>3.8.1</version> | |||
<configuration> | |||
<fork>true</fork> | |||
<source>${java.version}</source> | |||
<target>${java.version}</target> | |||
<encoding>UTF-8</encoding> | |||
</configuration> | |||
</plugin> | |||
<plugin> | |||
<groupId>org.apache.maven.plugins</groupId> | |||
<artifactId>maven-jar-plugin</artifactId> | |||
<version>3.1.0</version> | |||
</plugin> | |||
</plugins> | |||
</build> | |||
<licenses> | |||
<license> | |||
<name>The Apache Software License, Version 2.0</name> | |||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> | |||
</license> | |||
</licenses> | |||
<distributionManagement> | |||
<snapshotRepository> | |||
<id>ossrh</id> | |||
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url> | |||
</snapshotRepository> | |||
<repository> | |||
<id>ossrh</id> | |||
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url> | |||
</repository> | |||
</distributionManagement> | |||
</project> |
@@ -2,7 +2,7 @@ | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.contract; | |||
package com.github.yitter.contract; | |||
public interface IIdGenerator { | |||
long newLong() throws IdGeneratorException; |
@@ -2,7 +2,7 @@ | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.contract; | |||
package com.github.yitter.contract; | |||
public interface ISnowWorker { | |||
long nextId() throws IdGeneratorException; |
@@ -2,7 +2,7 @@ | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.contract; | |||
package com.github.yitter.contract; | |||
public class IdGeneratorException extends RuntimeException { | |||
@@ -2,7 +2,7 @@ | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.contract; | |||
package com.github.yitter.contract; | |||
/** | |||
* 雪花算法使用的参数 |
@@ -2,7 +2,7 @@ | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.contract; | |||
package com.github.yitter.contract; | |||
/** | |||
* Id生成时回调参数 |
@@ -2,12 +2,12 @@ | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.core; | |||
package com.github.yitter.core; | |||
import com.yitter.contract.ISnowWorker; | |||
import com.yitter.contract.IdGeneratorException; | |||
import com.yitter.contract.IdGeneratorOptions; | |||
import com.yitter.contract.OverCostActionArg; | |||
import com.github.yitter.contract.ISnowWorker; | |||
import com.github.yitter.contract.IdGeneratorOptions; | |||
import com.github.yitter.contract.OverCostActionArg; | |||
import com.github.yitter.contract.IdGeneratorException; | |||
public class SnowWorkerM1 implements ISnowWorker { | |||
@@ -2,10 +2,10 @@ | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.core; | |||
package com.github.yitter.core; | |||
import com.yitter.contract.IdGeneratorException; | |||
import com.yitter.contract.IdGeneratorOptions; | |||
import com.github.yitter.contract.IdGeneratorOptions; | |||
import com.github.yitter.contract.IdGeneratorException; | |||
public class SnowWorkerM2 extends SnowWorkerM1 { | |||
@@ -2,14 +2,14 @@ | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.idgen; | |||
package com.github.yitter.idgen; | |||
import com.yitter.contract.ISnowWorker; | |||
import com.yitter.contract.IdGeneratorException; | |||
import com.yitter.contract.IdGeneratorOptions; | |||
import com.yitter.contract.IIdGenerator; | |||
import com.yitter.core.SnowWorkerM1; | |||
import com.yitter.core.SnowWorkerM2; | |||
import com.github.yitter.contract.IIdGenerator; | |||
import com.github.yitter.contract.ISnowWorker; | |||
import com.github.yitter.contract.IdGeneratorException; | |||
import com.github.yitter.contract.IdGeneratorOptions; | |||
import com.github.yitter.core.SnowWorkerM1; | |||
import com.github.yitter.core.SnowWorkerM2; | |||
public class DefaultIdGenerator implements IIdGenerator { |
@@ -1,45 +1,43 @@ | |||
/* | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.yitter.idgen; | |||
import com.yitter.contract.IdGeneratorException; | |||
import com.yitter.contract.IdGeneratorOptions; | |||
import com.yitter.contract.IIdGenerator; | |||
/** | |||
* 这是一个调用的例子,默认情况下,单机集成者可以直接使用 nextId()。 | |||
*/ | |||
public class YitIdHelper { | |||
private static IIdGenerator idGenInstance = null; | |||
public static IIdGenerator getIdGenInstance() { | |||
return idGenInstance; | |||
} | |||
/** | |||
* 设置参数,建议程序初始化时执行一次 | |||
* | |||
* @param options | |||
*/ | |||
public static void setIdGenerator(IdGeneratorOptions options) throws IdGeneratorException { | |||
idGenInstance = new DefaultIdGenerator(options); | |||
} | |||
/** | |||
* 生成新的Id | |||
* 调用本方法前,请确保调用了 SetIdGenerator 方法做初始化。 | |||
* | |||
* @return | |||
*/ | |||
public static long nextId() throws IdGeneratorException { | |||
if (idGenInstance == null) { | |||
idGenInstance = new DefaultIdGenerator(new IdGeneratorOptions((short) 1)); | |||
} | |||
return idGenInstance.newLong(); | |||
} | |||
} | |||
/* | |||
* 版权属于:yitter(yitter@126.com) | |||
* 开源地址:https://gitee.com/yitter/idgenerator | |||
*/ | |||
package com.github.yitter.idgen; | |||
import com.github.yitter.contract.IIdGenerator; | |||
import com.github.yitter.contract.IdGeneratorException; | |||
import com.github.yitter.contract.IdGeneratorOptions; | |||
/** | |||
* 这是一个调用的例子,默认情况下,单机集成者可以直接使用 nextId()。 | |||
*/ | |||
public class YitIdHelper { | |||
private static IIdGenerator idGenInstance = null; | |||
public static IIdGenerator getIdGenInstance() { | |||
return idGenInstance; | |||
} | |||
/** | |||
* 设置参数,建议程序初始化时执行一次 | |||
*/ | |||
public static void setIdGenerator(IdGeneratorOptions options) throws IdGeneratorException { | |||
idGenInstance = new DefaultIdGenerator(options); | |||
} | |||
/** | |||
* 生成新的Id | |||
* 调用本方法前,请确保调用了 SetIdGenerator 方法做初始化。 | |||
* | |||
* @return | |||
*/ | |||
public static long nextId() throws IdGeneratorException { | |||
if (idGenInstance == null) { | |||
idGenInstance = new DefaultIdGenerator(new IdGeneratorOptions((short) 1)); | |||
} | |||
return idGenInstance.newLong(); | |||
} | |||
} |
@@ -1,6 +1,6 @@ | |||
package com.yitter.test; | |||
package com.github.yitter.test; | |||
import com.yitter.contract.IIdGenerator; | |||
import com.github.yitter.contract.IIdGenerator; | |||
import java.util.HashSet; | |||
import java.util.Set; |
@@ -1,9 +1,9 @@ | |||
package com.yitter.test; | |||
package com.github.yitter.test; | |||
import com.yitter.contract.IdGeneratorOptions; | |||
import com.yitter.contract.IIdGenerator; | |||
import com.yitter.idgen.DefaultIdGenerator; | |||
import com.yitter.idgen.YitIdHelper; | |||
import com.github.yitter.contract.IIdGenerator; | |||
import com.github.yitter.contract.IdGeneratorOptions; | |||
import com.github.yitter.idgen.DefaultIdGenerator; | |||
import com.github.yitter.idgen.YitIdHelper; | |||
public class StartUp { | |||
@@ -63,6 +63,8 @@ QQ群:646049993 | |||
6.不依赖任何外部缓存和数据库。(当然 WorkerId 须由外部指定) | |||
7.基础功能,开箱即用,无需配置文件、数据库连接等。 | |||
## 性能数据 | |||
(参数:10位自增序列,1000次漂移最大值) | |||
@@ -0,0 +1,3 @@ | |||
@echo off | |||
cargo build --release | |||
@pause |