技術分享| 如何部署安裝分佈式序列號生成器系統

語言: CN / TW / HK

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

在這裏插入圖片描述