1.2 快速开始(docker)

1.2.1 关于本节

  • 本节内容为您介绍如何通过dockerhub上的txle镜像快速启动一个txle的demon

1.2.2 安装准备

  • 安装docker

1.2.3 安装过程

按照顺序依次执行以下docker命令:

  1. 创建网络
     $ docker network create --subnet=172.20.0.0/24 --gateway=172.20.0.254 txle_net
    
  2. 运行consul
     $ docker volume create consul_data
     $ docker run -d --name=consul -v consul_data:/consul/data -p 8500:8500 --network=txle_net --ip=172.20.0.1 consul agent -server -ui -node=1 -client=0.0.0.0 -bootstrap
    
  3. 运行MySQL
     $ docker run -d --name=backend-mysql -p 3306:3306 --network=txle_net --ip=172.20.0.11 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=txle mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    
  4. 运行txle server
     $ docker run -itd --name=txle-server-1 -p 8090:8090 --network=txle_net --ip=172.20.0.21 actiontech/txle-server
    
  5. 运行spring boot demo
     $ docker run -itd --name=global -p 8000:8000 --network=txle_net --ip=172.20.0.31 actiontech/txle-global
     $ docker run -itd --name=transfer --network=txle_net --ip=172.20.0.32 actiontech/txle-transfer
     $ docker run -itd --name=user --network=txle_net --ip=172.20.0.33 actiontech/txle-user
     $ docker run -itd --name=merchant --network=txle_net --ip=172.20.0.34 actiontech/txle-merchant
    

1.2.4 使用工具发起全局事务

  1. 发起转账1元,转账将会成功
     $ curl http://${host_address}:8000/testGlobalTransaction/1/1/1
     $ ok
    
  2. 访问数据库查看账户余额

     $ docker exec -it backend-mysql bash
     # mysql -uroot -p123456
     mysql> select * from db1.txle_sample_transfer;
     +----+--------+------------+---------+--------+--------+---------+---------------------+
     | id | userid | merchantid | amount  | payway | status | version | createtime          |
     +----+--------+------------+---------+--------+--------+---------+---------------------+
     |  1 |      1 | 1          | 1.00000 |      1 |      1 |       0 | 2019-10-30 09:47:54 |
     +----+--------+------------+---------+--------+--------+---------+---------------------+
     1 row in set (0.00 sec)
    
     mysql> select * from db2.txle_sample_user;
     +----+------+-----------+---------+---------------------+
     | id | name | balance   | version | createtime          |
     +----+------+-----------+---------+---------------------+
     |  1 | user | 999.00000 |       1 | 2019-10-30 09:47:30 |
     +----+------+-----------+---------+---------------------+
     1 row in set (0.00 sec)
    
     mysql> select * from db3.txle_sample_merchant;
     +----+----------+---------+---------+---------------------+
     | id | name     | balance | version | createtime          |
     +----+----------+---------+---------+---------------------+
     |  1 | merchant | 1.00000 |       1 | 2019-10-30 09:47:31 |
     +----+----------+---------+---------+---------------------+
     1 row in set (0.00 sec)
    

    txle_sample_transfer记录了一条交易成功的记录,txle_sample_usertxle_sample_merchant数据正常记录

  3. 根据示例代码里的设定发起转账金额大于200元,第二个子服务merchant会报错导致转账将会失败
     $ curl http://${host_address}:8000/testGlobalTransaction/1/300/1
     $ {"timestamp":1572429018848,"status":500,"error":"Internal Server Error","exception":"org.springframework.web.client.HttpServerErrorException","message":"500 null","path":"/testGlobalTransaction/1/300/1"}
    
  4. 访问数据库查看账户余额

     $ docker exec -it backend-mysql bash
     # mysql -uroot -p123456
     mysql> select * from db1.txle_sample_transfer;
     +----+--------+------------+-----------+--------+--------+---------+---------------------+
     | id | userid | merchantid | amount    | payway | status | version | createtime          |
     +----+--------+------------+-----------+--------+--------+---------+---------------------+
     |  1 |      1 | 1          |   1.00000 |      1 |      1 |       0 | 2019-10-30 09:47:54 |
     |  2 |      1 | 1          | 300.00000 |      1 |      2 |       0 | 2019-10-30 09:50:18 |
     +----+--------+------------+-----------+--------+--------+---------+---------------------+
     2 rows in set (0.00 sec)
    
     mysql> select * from db2.txle_sample_user;
     +----+------+-----------+---------+---------------------+
     | id | name | balance   | version | createtime          |
     +----+------+-----------+---------+---------------------+
     |  1 | user | 999.00000 |       1 | 2019-10-30 09:47:30 |
     +----+------+-----------+---------+---------------------+
     1 row in set (0.00 sec)
    
     mysql> select * from db3.txle_sample_merchant;
     +----+----------+---------+---------+---------------------+
     | id | name     | balance | version | createtime          |
     +----+----------+---------+---------+---------------------+
     |  1 | merchant | 1.00000 |       1 | 2019-10-30 09:47:31 |
     +----+----------+---------+---------+---------------------+
     1 row in set (0.01 sec)
    

    txle_sample_transfer记录了一条交易失败的记录,txle_sample_usertxle_sample_merchant数据回滚

results matching ""

    No results matching ""