制作seata docker镜像

seata是阿里巴巴的一款开源的分布式事务框架,官方已经支持docker了,但是因为业务的需要,需要自己定制。
制作docker镜像

官方的Dockerfile下载seata-server-1.1.0

导入seata需要的sql脚本到mysql,创建seata数据库

-- 每个业务库中都要创建undo_log表
CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) NOT NULL,
  `context` varchar(128) NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int(11) NOT NULL,
  `log_created` datetime NOT NULL,
  `log_modified` datetime NOT NULL,
  `ext` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAR(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAR(32),
    `transaction_service_group` VARCHAR(64),
    `transaction_name`          VARCHAR(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAR(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(96),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

因为运行的脚本seata-server.sh中一些虚拟机参数是写死的,而且给的很大,需要修改脚本,通过环境变量来获得参数值:

准备工作:

# 解压缩 seata-server-1.1.0.zip
[root@VM_0_16_centos seata]# tree seata
seata
|-- bin
|   |-- seata-server.bat
|   `-- seata-server.sh
|-- conf
|   |-- file.conf
|   |-- file.conf.example
|   |-- logback.xml
|   |-- META-INF
|   |   `-- services
|   |       |-- io.seata.core.lock.Locker
|   |       |-- io.seata.core.store.db.DataSourceGenerator
|   |       |-- io.seata.server.coordinator.AbstractCore
|   |       |-- io.seata.server.session.SessionManager
|   |       `-- io.seata.server.store.TransactionStoreManager
|   |-- README.md
|   |-- README-zh.md
|   `-- registry.conf
|-- lib
|   |-- animal-sniffer-annotations-1.17.jar
。。。

vim seata-server.sh

#====================   修改前
exec "$JAVACMD" $JAVA_OPTS -server -Xmx2048m -Xms2048m -Xmn1024m -Xss512k -XX:SurvivorRatio=10 -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:MaxDirectMemorySize=1024m -XX:-OmitStackTraceInFastThrow -XX:-UseAdaptiveSizePolicy -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="$BASEDIR"/logs/java_heapdump.hprof -XX:+DisableExplicitGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 -Xloggc:"$BASEDIR"/logs/seata_gc.log -verbose:gc -Dio.netty.leakDetectionLevel=advanced \
  -classpath "$CLASSPATH" \
  -Dapp.name="seata-server" \
  -Dapp.pid="$$" \
  -Dapp.repo="$REPO" \
  -Dapp.home="$BASEDIR" \
  -Dbasedir="$BASEDIR" \
  io.seata.server.Server \
  "$@"
#====================   修改后
# -Xmx${JVM_XMX:-2048m} 表示当JVM_XMX为null或者空字符串的时候赋值2048m
exec "$JAVACMD" $JAVA_OPTS -server -Xmx${JVM_XMX:-2048m} -Xms${JVM_XMS:-2048m} -Xmn${JVM_XMN:-1024m} -Xss${JVM_XSS:-512k} -XX:SurvivorRatio=10 -XX:MetaspaceSize=${JVM_MS:-128m} -XX:MaxMetaspaceSize=${JVM_MMS:-256m} -XX:MaxDirectMemorySize=${JVM_MDMS:-1024m} -XX:-OmitStackTraceInFastThrow -XX:-UseAdaptiveSizePolicy -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="$BASEDIR"/logs/java_heapdump.hprof -XX:+DisableExplicitGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 -Xloggc:"$BASEDIR"/logs/seata_gc.log -verbose:gc -Dio.netty.leakDetectionLevel=advanced \
  -classpath "$CLASSPATH" \
  -Dapp.name="seata-server" \
  -Dapp.pid="$$" \
  -Dapp.repo="$REPO" \
  -Dapp.home="$BASEDIR" \
  -Dbasedir="$BASEDIR" \
  io.seata.server.Server \
  "$@"

vim Dockerfile

FROM centos:7
MAINTAINER pader "bartggg@163.com"

RUN set -x \
    && ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo '$TIME_ZONE' > /etc/timezone 
# 允许挂载该目录 
VOLUME /opt/seata/conf
VOLUME /opt/seata/logs

# 添加jdk
ADD jdk-8u144-linux-x64.tar.gz /opt
ADD seata /opt/seata

# set environment
ENV SEATA_USER="seata" \
	BASE_DIR=/opt/seata \
    JAVA_HOME="/opt/jdk1.8.0_144" \
    JAVA="/opt/jdk1.8.0_144/bin/java" \
    TIME_ZONE="Asia/Shanghai" \
    SERVER_PORT="8091" \
    CLUSTER_NUM=1
# 设置额外参数
ENV JVM_XMX=1024m \
    JVM_XMS=1024m \
    JVM_XMN=512m \
    JVM_XSS=256K \
    JVM_MS=64m \
    JVM_MMS=128m \
    JVM_MDMS=512m

WORKDIR $BASE_DIR
EXPOSE 9411
ENTRYPOINT ["sh", "-c", "/opt/seata/bin/seata-server.sh"]

构建镜像:

docker build -t bart/seata:1.1.0 .

运行:

# 如果需要使用自己的配置文件的话
mkdir -p /home/seata/conf
mkdir -p /home/seata/logs

# 将默认配置文件放到conf中
# file.conf 、 registry.conf 参考官网配置
# https://seata.io/zh-cn/blog/seata-quick-start.html#

# 默认启动
docker run -d \
--name seata01 \
--restart always \
-p 19411:9411 \
bart/seata:1.1.0 

posted @ 2020-05-28 17:09  bartggg  阅读(1284)  评论(0编辑  收藏  举报