根据官方在gitee的文档实际操作总结而来。

传统方式

准备工作

  1. 部署 etcd

    首先要安装 etcd 作为存储。这里使用 Docker 进行部署。
    将以下内容保存为 docker-compose.yaml 并在同目录新建 data 文件夹。然后 docker compose up -d 即可启动 etcd。

    services:
      etcd:
        image: quay.nju.edu.cn/coreos/etcd:v3.5.13
        container_name: etcd
        ports:
          - "2379:2379"
          - "2380:2380"
        volumes:
          - type: bind
            source: ./data
            target: /etcd-data
        command:
          - /usr/local/bin/etcd
          - --name=s1
          - --data-dir=/etcd-data
          - --listen-client-urls=http://0.0.0.0:2379
          - --advertise-client-urls=http://0.0.0.0:2379
          - --listen-peer-urls=http://0.0.0.0:2380
          - --initial-advertise-peer-urls=http://0.0.0.0:2380
          - --initial-cluster=s1=http://0.0.0.0:2380
          - --initial-cluster-token=tkn
          - --initial-cluster-state=new
          - --log-level=info
          - --logger=zap
          - --log-outputs=stderr
    
  2. 打包 worker 和 dashboard

    先把代码拉下来 git clone https://gitee.com/jd-platform-opensource/hotkey.git

    使用命令行进入目录中,输入 mvn clean package,即可获得 worker 和 dashboard 的 jar 包。

    注意:构建前,请配置好 maven 以及 jdk8 。

  3. Debian 下安装 jdk8

    apt update; apt install -y wget apt-transport-https
    mkdir -p /etc/apt/keyrings
    wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | tee /etc/apt/keyrings/adoptium.asc
    echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list
    apt update; apt install temurin-8-jdk
    
  4. 导入数据库

    dashboard 运行依赖于 MySQL 数据库。新建数据库 hotkey_db 并执行以下 sql:

    --  !!! 注意设置sql model 否则可能sql报错 !!!
    --  查询你的sql_model参数:select @@global.sql_mode;  发现ONLY_FULL_GROUP_BY 则会导致报错
    --  解决方式:set @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
    --  详情查阅:https://www.cnblogs.com/hjhsblogs/p/11079356.html
    
    DROP TABLE IF EXISTS `hk_change_log`;
    CREATE TABLE `hk_change_log`  (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `biz_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '业务key',
      `biz_type` int(11) NOT NULL COMMENT '业务类型:1规则变更;2worker变更',
      `from_str` varchar(1024) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '原始值',
      `to_str` varchar(1024) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '目标值',
      `app_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '数据所属APP',
      `update_user` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '修改人',
      `create_time` datetime(0) NOT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
      `uuid` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '防重ID',
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `uniq_uuid`(`uuid`) USING BTREE COMMENT '防重索引'
    ) ENGINE = InnoDB AUTO_INCREMENT = 29 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;
    
    
    
    DROP TABLE IF EXISTS `hk_user`;
    CREATE TABLE `hk_user`  (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
      `nick_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '昵称',
      `user_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '用户名',
      `pwd` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '密码',
      `phone` varchar(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '手机号',
      `role` varchar(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '角色:ADMIN-超管,APPADMIN-app管理员,APPUSER-app用户',
      `app_name` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '所属appName',
      `create_time` datetime(0) NOT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
      `state` int(11) NOT NULL DEFAULT 1 COMMENT '状态:1可用;0冻结',
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `uniq_userName`(`user_name`) USING BTREE COMMENT '账号唯一索引'
    ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;
    
    
    -- pwd: 123456
    INSERT INTO `hk_user` VALUES (2, 'admin', 'admin', 'e10adc3949ba59abbe56e057f20f883e', '1888888', 'ADMIN', '', '2020-07-28 14:01:03', 1);
    
    
    DROP TABLE IF EXISTS `hk_key_record`;
    CREATE TABLE `hk_key_record`  (
      `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `key_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT 'key',
      `app_name` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '所属appName',
      `val` varchar(2048) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT 'value',
      `duration` int(11) NOT NULL DEFAULT 60 COMMENT '缓存时间',
      `source` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '来源',
      `type` int(11) NOT NULL DEFAULT 1 COMMENT '记录类型:1put;2del; -1unkonw',
      `create_time` datetime(0) NOT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
      `uuid` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '防重ID',
      `rule` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '''' COMMENT '规则',
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `uniq_key`(`uuid`) USING BTREE COMMENT '唯一索引'
    ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;
    
    
    DROP TABLE IF EXISTS `hk_key_timely`;
    CREATE TABLE `hk_key_timely`  (
      `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `key_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT 'key',
      `val` varchar(2048) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT 'value',
      `uuid` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '防重ID',
      `app_name` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '' COMMENT '所属appName',
      `duration` int(11) NOT NULL DEFAULT 0 COMMENT '缓存时间',
      `create_time` datetime(0) NOT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '创建时间',
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `uniq_key`(`uuid`) USING BTREE COMMENT '唯一索引'
    ) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;
    
    
    CREATE TABLE `hk_statistics`  (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `key_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT 'keyName',
      `count` int(11) NOT NULL COMMENT '计数',
      `app` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT 'app',
      `days` int(11) NOT NULL COMMENT '天数',
      `hours` bigint(11) NOT NULL COMMENT '小时数',
      `minutes` bigint(11) NOT NULL DEFAULT 0 COMMENT '分钟数',
      `biz_type` int(2) NOT NULL COMMENT '业务类型',
      `rule` varchar(180) NOT NULL COMMENT '所属规则',
      `uuid` varchar(180) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '防重ID',
      `create_time` datetime(0) NOT NULL COMMENT '创建时间',
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `uniq_uuid`(`uuid`) USING BTREE COMMENT '防重唯一索引'
    ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
    
    
    CREATE TABLE `hk_rules`  (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `rules` varchar(5000) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '规则JSON',
      `app` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '所属APP',
      `update_user` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '修改人',
      `update_time` datetime(0) NOT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '修改时间',
      `version` int(11) NOT NULL DEFAULT 0 COMMENT '版本号',
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `uniq_app`(`app`) USING BTREE COMMENT '防重索引'
    ) ENGINE = InnoDB AUTO_INCREMENT = 15 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact;
    
    
    
    CREATE TABLE `hk_summary`  (
      `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
      `index_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '指标名称',
      `rule` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '规则',
      `app` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'app',
      `index_val1` int(11) NOT NULL DEFAULT 0 COMMENT '指标值1',
      `index_val2` int(11) NOT NULL DEFAULT 0 COMMENT '指标值2',
      `index_val3` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '指标值3',
      `days` int(11) NOT NULL DEFAULT 0 COMMENT '天数',
      `hours` int(11) NOT NULL DEFAULT 0 COMMENT '小时数',
      `minutes` bigint(11) NOT NULL DEFAULT 0 COMMENT '分钟数',
      `seconds` bigint(11) NOT NULL DEFAULT 0 COMMENT '秒数',
      `biz_type` tinyint(2) NOT NULL DEFAULT 0 COMMENT '类型',
      `uuid` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '防重ID',
      `create_time` datetime(0) NOT NULL COMMENT '创建时间',
      `update_time` datetime(0) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '修改时间',
      PRIMARY KEY (`id`) USING BTREE,
      UNIQUE INDEX `uniq_uuid`(`uuid`) USING BTREE COMMENT '防重索引',
      INDEX `idx_apprule`(`app`, `rule`) USING BTREE COMMENT '查询索引',
      INDEX `ix_ct`(`create_time`) USING BTREE COMMENT '时间索引'
    ) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '汇总表' ROW_FORMAT = Compact;
    
    
    /* 请确认以下SQL符合您的变更需求,务必确认无误后再提交执行 */
    
    CREATE TABLE `biz_access_token` (
       `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
       `token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'token',
       `flag` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT 'flag',
        `CREATED_BY` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT '创建人',
        `CREATED_TIME` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
        `UPDATED_BY` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT '修改人',
        `UPDATED_TIME` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
         PRIMARY KEY (`id`)
    ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '咚咚token表' ROW_FORMAT = Compact;
    

运行

接下来就可以将 jd-hotkey 跑起来了。

  1. 启动 etcd

    进入 etcd 目录 运行 docker compose up -d

  2. 启动 dashboard

    java -jar dashboard-0.0.2-SNAPSHOT.jar --etcd.server="http://127.0.0.1:2379" --MYSQL_USER=root --MYSQL_PASS=root --MYSQL_HOST=xxx
    
  3. 启动 worker

    java -jar $JAVA_OPTS worker-0.0.4-SNAPSHOT.jar --etcd.server=http://127.0.0.1:2379 --etcd.workerPath=default --localAddress=localhost
    

至此已启动完成。

纯 Docker 方式

打包 dashboard 和 worker 以及导入数据库的步骤和传统方式一致。

将 dashboard 和 worker 的 jar 包重命名为 dashboard.jar worker.jar 并和以下 Dockerfile docker-compose.yaml 放在同一个文件夹中。运行 docker compose up -d 即可启动服务。

# 使用 debian:12-slim 作为基础镜像
FROM debian:12-slim

# 更换为清华大学的Debian源
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
    && sed -i 's/security.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debian.sources \
    && apt-get update \
    && apt-get install -y wget apt-transport-https gnupg2

# 创建存储GPG密钥的目录
RUN mkdir -p /etc/apt/keyrings

# 添加 Adoptium 的公钥
RUN wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | tee /etc/apt/keyrings/adoptium.asc

# 添加 Adoptium 的软件源
RUN echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://mirror.nju.edu.cn/adoptium/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list

# 更新软件源并安装 Temurin-8-JDK
RUN apt-get update \
    && apt-get install -y temurin-8-jdk

# 清理缓存,减少镜像大小
RUN apt-get clean \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /opt
services:
  etcd:
    image: quay.nju.edu.cn/coreos/etcd:v3.5.13
    container_name: etcd
    ports:
      - "2379:2379"
      - "2380:2380"
    volumes:
      - ./etcd/data:/etcd-data
    command:
      - /usr/local/bin/etcd
      - --name=s1
      - --data-dir=/etcd-data
      - --listen-client-urls=http://0.0.0.0:2379
      - --advertise-client-urls=http://0.0.0.0:2379
      - --listen-peer-urls=http://0.0.0.0:2380
      - --initial-advertise-peer-urls=http://0.0.0.0:2380
      - --initial-cluster=s1=http://0.0.0.0:2380
      - --initial-cluster-token=tkn
      - --initial-cluster-state=new
      - --log-level=info
      - --logger=zap
      - --log-outputs=stderr
    
  dashboard:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: dashboard
    ports:
      - "8081:8081"
      - "11112:11112"
    volumes:
      - ./dashboard.jar:/opt/dashboard.jar
    depends_on:
      - etcd
    command:
      - java
      - -jar
      - /opt/dashboard.jar 
      - --etcd.server=http://etcd:2379
      - --MYSQL_USER=root
      - --MYSQL_PASS=xxx
      - --MYSQL_HOST=xxx

  worker:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: worker
    
    ports:
      - "8080:8080"
      - "11111:11111"
    volumes:
      - ./worker.jar:/opt/worker.jar
    depends_on:
     - dashboard
    command:
      - java
      - -jar
      - /opt/worker.jar
      - --etcd.server=http://etcd:2379
      - --etcd.workerPath=app1
      - --nettyPort=11111
      # 要使用真实 IP 不能用ip段
      - --localAddress=192.168.8.2