7.3 dubbo示例

7.3.1 关于本节

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

7.3.2 环境准备

  • JDK 1.8
  • Maven 3.x
  • txle server

7.3.3 运行示例

  1. 参照1.2 docker image部署的说明,安装部署txle server
  2. dubbo架构需要zookeeper支持,可以使用如下命令创建
    $ docker run --name=dubbo_zookeeper -p 2181:2181 --network=txle_net --ip=172.20.0.61 zookeeper
    
  3. 运行如下命令创建业务代码所需要的数据库

    $ 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)
    
  4. 运行如下命令编译打包业务端代码
    $ git clone git@github.com:actiontech/txle.git
    $ cd txle/examples/sample-txle-dubbo && \
    mvn clean package
    
  5. 运行业务端代码
    1. 运行transfer服务。打包好的jar在txle/examples/sample-txle-springboot/sample-txle-dubbo-provider-transfer/target/
       $ java -Dalpha.cluster.address=${txle_server_address}:8080 -jar sample-txle-dubbo-provider-transfer-0.0.1-SNAPSHOT.jar
      
    2. 运行user服务。打包好的jar在txle/examples/sample-txle-springboot/sample-txle-dubbo-provider-user/target/
       $ java -Dalpha.cluster.address=${txle_server_address}:8080 -jar sample-txle-dubbo-provider-user-0.0.1-SNAPSHOT.jar
      
    3. 运行merchant服务。打包好的jar在txle/examples/sample-txle-springboot/sample-txle-dubbo-provider-merchant/target/
       $ java -Dalpha.cluster.address=${txle_server_address}:8080 -jar sample-txle-dubbo-provider-merchant-0.0.1-SNAPSHOT.jar
      
    4. 运行consumer服务。打包好的jar在txle/examples/sample-txle-springboot/sample-txle-dubbo-consumer/target/
      请务必等前面三个子服务启动完毕后,再启动consumer服务
       $ java -Dalpha.cluster.address=${txle_server_address}:8080 -jar sample-txle-dubbo-consumer-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
    $ The 'Merchant' Service threw a runtime exception in case of balance was more than 200.
    
  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 ""