跳到主要内容

Snowflake

metadata fields

Example: configs/config_snowflake.json

FieldRequiredDescription
mysqlHostYmysql server address, such as localhost:3306
userNameYmysql username
passwordYmysql password
databaseNameYmysql database name
tableNameNmysql table name
timeBitsNtime id size, default value is 28
workerBitsNworker id size, default value is 22
seqBitsNsequence id size, default value is 13
startTimeNtime base, default value is “2022-01-01”
reqTimeoutNid request timeout. Default is 500 milliseconds
keyTimeoutNtimeout of key namespace. The default is 24 hours

Overall design

The overall design of the id generated by the snowflake algorithm is as follows:

img.jpg

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"