Snowflake
metadata fields
Example: configs/config_snowflake.json
Field | Required | Description |
---|---|---|
mysqlHost | Y | mysql server address, such as localhost:3306 |
userName | Y | mysql username |
password | Y | mysql password |
databaseName | Y | mysql database name |
tableName | N | mysql table name |
timeBits | N | time id size, default value is 28 |
workerBits | N | worker id size, default value is 22 |
seqBits | N | sequence id size, default value is 13 |
startTime | N | time base, default value is “2022-01-01” |
reqTimeout | N | id request timeout. Default is 500 milliseconds |
keyTimeout | N | timeout of key namespace. The default is 24 hours |
Overall design
The overall design of the id generated by the snowflake algorithm is as follows:
How to start mysql
If you want to run the mysql demo, you need to start a mysql server with Docker first.
If port 3306 is occupied by other services, you need to exit other services first
docker pull mysql:latest
docker run --name snowflake -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
docker exec -it snowflake bash
mysql -uroot -p123456
You need to create a new database:
CREATE DATABASE layotto_sequencer;
Run layotto
cd ${project_path}/cmd/layotto
go build
If build reports an error, it can be executed in the root directory of the project
go mod vendor
Execute after the compilation is successful:
./layotto start -c ../../configs/config_redis.json
Run Demo
cd ${project_path}/demo/sequencer/redis/
go build -o client
./client -s "sequencer_demo"