7.2 spring cloud示例

7.2.1 关于本节

  • 本节内容为您介绍如何编译部署spring cloud业务端代码
  • 该示例包含4个服务
    • global
    • transfer
    • user
    • merchant

7.2.2 环境准备

  • JDK 1.8
  • Maven 3.x
  • txle server

7.2.3 运行示例

  1. 参照1.2 docker image部署的说明,安装部署txle server
  2. 运行如下命令创建业务代码所需要的数据库

     $ docker exec -it backend-mysql bash
     # mysql -uroot -p123456
     mysql> create database db1;
     Query OK, 1 row affected (0.00 sec)
    
     mysql> create database db2;
     Query OK, 1 row affected (0.00 sec)
    
     mysql> create database db3;
     Query OK, 1 row affected (0.00 sec)
    
  3. 运行如下命令编译打包业务端代码
     $ git clone git@github.com:actiontech/txle.git
     $ cd txle/examples/sample-txle-springcloud && \
     mvn clean package
    
  4. 运行业务端代码
    1. 运行eureka服务。打包好的jar在txle/examples/sample-txle-springcloud/sample-txle-springcloud-eureka/target/
      $ java -jar sample-txle-springcloud-eureka-0.0.1-SNAPSHOT.jar
      
    2. 运行global服务。打包好的jar在txle/examples/sample-txle-springcloud/sample-txle-springcloud-global/target/
       $ java -Dalpha.cluster.address=${txle_server_address}:8080 -jar sample-txle-springcloud-global-0.0.1-SNAPSHOT.jar
      
    3. 运行transfer服务。打包好的jar在txle/examples/sample-txle-springcloud/sample-txle-springcloud-transfer/target/
       $ java -Dalpha.cluster.address=${txle_server_address}:8080 -jar sample-txle-springcloud-transfer-0.0.1-SNAPSHOT.jar
      
    4. 运行user服务。打包好的jar在txle/examples/sample-txle-springcloud/sample-txle-springcloud-user/target/
       $ java -Dalpha.cluster.address=${txle_server_address}:8080 -jar sample-txle-springcloud-user-0.0.1-SNAPSHOT.jar
      
    5. 运行merchant服务。打包好的jar在txle/examples/sample-txle-springcloud/sample-txle-springcloud-merchant/target/
       $ java -Dalpha.cluster.address=${txle_server_address}:8080 -jar sample-txle-springcloud-merchant-0.0.1-SNAPSHOT.jar
      

7.1.4 使用工具发起全局事务

  1. 发起转账1元,转账将会成功
    $ curl http://${host_address}:8000/testGlobalTransaction/1/1/1
    $ ok
    
  2. 访问数据库查看账户余额
    txle_sample_transfer记录了一条交易成功的记录,txle_sample_usertxle_sample_merchant数据正常记录

    $ 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)
    
  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. 访问数据库查看账户余额
    txle_sample_transfer记录了一条交易失败的记录,txle_sample_usertxle_sample_merchant数据回滚

    $ 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)
    

results matching ""

    No results matching ""