技术分享| 如何部署安装分布式序列号生成器系统

Tinyid介绍

Tinyid是滴滴开发的一款分布式ID系统,Tinyid是在美团(Leaf)leaf-segment算法基础上升级而来,不仅支持了数据库多主节点模式,还提供了tinyid-client客户端的接入方式,使用起来更加方便。但和美团(Leaf)不同的是,Tinyid只支持号段一种模式不支持雪花模式。

Tinyid的特性

  • 全局唯一的long型ID
  • 趋势递增的id
  • 提供 http 和 java-client 方式接入
  • 支持批量获取ID
  • 支持生成1,3,5,7,9…序列的ID
  • 支持多个db的配置

Tinyid部署

JDK部署

[root@localhost packages]# tar zxvf jdk-8u121-linux-x64.tar.gz
[root@localhost packages]# mv jdk1.8.0_121/ /usr/local/jdk
[root@localhost packages]# vim /etc/profile
##末尾添加
export JAVA_HOME=/usr/local/jdk 
export CLASSPATH=$:CLASSPATH:$JAVA_HOME/lib/ 
export PATH=$PATH:$JAVA_HOME/bin
[root@localhost packages]# source /etc/profile
[root@localhost packages]# java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Maven部署

[root@localhost packages]# tar zxvf apache-maven-3.5.0-bin.tar.gz
[root@localhost packages]# mv apache-maven-3.5.0 /usr/local/maven
[root@localhost packages]# vim /etc/profile
##末尾添加
export MAVEN_HOME=/usr/local/maven 
export PATH=$MAVEN_HOME/bin:$PATH
[root@localhost packages]# source /etc/profile
[root@localhost packages]# mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /usr/local/maven

MySQL部署

[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql -uroot 
mysql> set password = password('123456');
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;    //设置mysql外网连接密码
mysql> flush privileges;

Tinyid部署

## 拉取代码
[root@localhost local]# git clone https://github.com/didi/tinyid.git
[root@localhost local]# cd tinyid/tinyid-server/

## 导入SQL文件
[root@localhost tinyid-server]# mysql -uroot -p123456
mysql> create database tinyid;
mysql> use tinyid;
mysql> source /usr/local/tinyid/tinyid-server/db.sql;
mysql> exit

修改配置文件指定MySQL

[root@localhost tinyid-server]# vim src/main/resources/offline/application.properties

datasource.tinyid.primary.driver-class-name=com.mysql.jdbc.Driver
datasource.tinyid.primary.url=jdbc:mysql://localhost:3306/tinyid?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
datasource.tinyid.primary.username=root
datasource.tinyid.primary.password=123456

## 注意修mysql地址和库名称
jdbc:mysql://localhost:3306/tinyid?

启动tinyid

[root@localhost local]# cd tinyid/tinyid-server/ 
[root@localhost tinyid-server]# sh build.sh offline
[root@localhost tinyid-server]# java -jar output/tinyid-server-xxx.jar
[root@localhost tinyid-server]#  netstat -untpl | grep java
tcp        0      0 0.0.0.0:9999            0.0.0.0:*               LISTEN      12836/java 

自启动脚本

[root@localhost ~]# cd /usr/local/tinyid/tinyid-server

[root@localhost tinyid-server]# vim tinyid.sh

#!/bin/sh
##
if [ -f /etc/init.d/functions ]; then
        . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
        . /etc/rc.d/init.d/functions
else
        echo -e "\aJnice: unable to locate functions lib. Cannot continue."
        exit 0
fi

ulimit -n 65000
echo 1024 65000 > /proc/sys/net/ipv4/ip_local_port_range

## jar文件名称
export JAVA_HOME=/usr/local/java/jdk1.8.0_181
export PATH=$JAVA_HOME/bin:$PATH:$PATH:/usr/ffmpeg/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
JAR_HOME=/usr/local/tinyid/tinyid-server/
JAR_NAME=tinyid-server-0.1.0-SNAPSHOT.jar

## 过滤pid进程号
tpid=`ps -ef|grep $JAR_NAME | grep -v grep | grep -v kill | awk '{print $2}'`

#---------------------------------------------------------------------------
# START
#---------------------------------------------------------------------------
Start()
{
        if [ $tpid ]; then
                echo -n "tinyid process [${prog}] already running"
                echo_failure
                echo
                return 1
        fi

        if [ ! -f ${JAR_HOME}/output/${JAR_NAME} ]; then
                echo -n "tinyid binary [${prog}] not found."
                echo_failure
                echo
                return 1
        fi

        echo -n "starting service (tinyid): "
        cd ${JAR_HOME}
        nohup java -jar ${JAR_HOME}/output/${JAR_NAME}  > /dev/null 2>&1 &
        retval=$?
        if [ ${retval} == 0 ]; then
                echo_success
                echo
        else
                echo_failure
                echo
                break
        fi
        sleep 1

        return 0
}

#---------------------------------------------------------------------------
# STOP
#---------------------------------------------------------------------------
Stop()
{
        echo -n "stopping service (tinyid): "
        kill -9 $tpid
        echo
        return 0
}

#---------------------------------------------------------------------------
# MAIN
#---------------------------------------------------------------------------
case "$1" in
        start)
                Start
                ;;
        stop)
                Stop
                ;;
        restart)
                Stop
                sleep 3
                Start
                ;;
        *)
                echo "please use : $0 {start|stop|restart}"
esac

exit

设置任务计划

[root@localhost tinyid-server]# crontab -l

*/1 * * * * sh /usr/local/tinyid/tinyid-server/tinyid.sh  start  >/dev/null 2>&1

在这里插入图片描述

posted @ 2022-06-02 11:48  anyRTC  阅读(150)  评论(0编辑  收藏  举报