python中使用snowflake分布式id生成器
- 安装pysnowflake
- 启动pysnowflake
- 获取id
安装pysnowflake
pip install pysnowflake
启动pysnowflake
snowflake_start_server --address=localhost --port=8910 --dc=1 --worker=1 --log_file_prefix=E:\Work\gdos\importdata\logs\logspysnowflask.log
获取id
import snowflake.client
# 链接服务端并初始化一个pysnowflake客户端
host = 'localhost'
port = 8910
snowflake.client.setup(host, port)
# 生成一个全局唯一的ID(在MySQL中可以用BIGINT UNSIGNED对应)
def getdistruteid():
return snowflake.client.get_guid()