前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >seata(4):dubbo

seata(4):dubbo

作者头像
golangLeetcode
发布2023-09-21 15:30:48
2370
发布2023-09-21 15:30:48
举报

接下来学习下dubbo的例子,首先还是启动seata和mysql

代码语言:javascript
复制
 % docker compose -f seata/exp2/docker-compose.yaml up

然后创建数据库,添加表

代码语言:javascript
复制
mysql> create database seata;
Query OK, 1 row affected (0.00 sec)

mysql> use seata
Database changed
代码语言:javascript
复制
DROP TABLE IF EXISTS `stock_tbl`;
CREATE TABLE `stock_tbl`
(
    `id`             int(11) NOT NULL AUTO_INCREMENT,
    `commodity_code` varchar(255) DEFAULT NULL,
    `count`          int(11) DEFAULT 0,
    PRIMARY KEY (`id`),
    UNIQUE KEY (`commodity_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS `order_tbl`;
CREATE TABLE `order_tbl`
(
    `id`             int(11) NOT NULL AUTO_INCREMENT,
    `user_id`        varchar(255) DEFAULT NULL,
    `commodity_code` varchar(255) DEFAULT NULL,
    `count`          int(11) DEFAULT 0,
    `money`          int(11) DEFAULT 0,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS `account_tbl`;
CREATE TABLE `account_tbl`
(
    `id`      int(11) NOT NULL AUTO_INCREMENT,
    `user_id` varchar(255) DEFAULT NULL,
    `money`   int(11) DEFAULT 0,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
代码语言:javascript
复制
-- 注意此处0.3.0+ 增加唯一索引 ux_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,
    PRIMARY KEY (`id`),
    UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

然后依次启动我们的三个微服务和一个测试程序

代码语言:javascript
复制
 [DUBBO] Export dubbo service io.seata.samples.dubbo.service.AccountService to local registry url : injvm://127.0.0.1/io.seata.samples.dubbo.service.AccountService?anyhost=true&application=dubbo-demo-account-service&bind.ip=192.168.0.100&bind.port=20881&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=io.seata.samples.dubbo.service.AccountService&methods=debit&pid=8825&qos.enable=false&release=2.7.15&service.name=ServiceBean:/io.seata.samples.dubbo.service.AccountService&side=provider&timeout=10000&timestamp=1695049549710, dubbo version: 2.7.15, current host: 192.168.0.100
[INFO ] 2023-09-18 23:05:50,039 method:org.apache.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:501)
 [DUBBO] Register dubbo service io.seata.samples.dubbo.service.AccountService url dubbo://192.168.0.100:20881/io.seata.samples.dubbo.service.AccountService?anyhost=true&application=dubbo-demo-account-service&bind.ip=192.168.0.100&bind.port=20881&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=io.seata.samples.dubbo.service.AccountService&methods=debit&pid=8825&qos.enable=false&release=2.7.15&service.name=ServiceBean:/io.seata.samples.dubbo.service.AccountService&side=provider&timeout=10000&timestamp=1695049549710 to registry registry://224.5.6.7:1234/org.apache.dubbo.registry.RegistryService?application=dubbo-demo-account-service&dubbo=2.0.2&id=org.apache.dubbo.config.RegistryConfig&pid=8825&qos.enable=false&registry=multicast&release=2.7.15&timestamp=1695049549694&unicast=false, dubbo version: 2.7.15, current host: 192.168.0.100
[INFO ] 2023-09-18 23:05:50,093 method:org.apache.dubbo.qos.protocol.QosProtocolWrapper.startQosServer(QosProtocolWrapper.java:97)
 [DUBBO] qos won't be started because it is disabled. Please check dubbo.application.qos.enable is configured either in system property, dubbo.properties or XML/spring-boot configuration., dubbo version: 2.7.15, current host: 192.168.0.100
[INFO ] 2023-09-18 23:05:50,205 method:org.apache.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:69)
 [DUBBO] Start NettyServer bind /0.0.0.0:20881, export /192.168.0.100:20881, dubbo version: 2.7.15, current host: 192.168.0.100
代码语言:javascript
复制
[DUBBO] Receive multicast message: register dubbo://192.168.0.100:20882/io.seata.samples.dubbo.service.StockService?anyhost=true&application=dubbo-demo-stock-service&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=io.seata.samples.dubbo.service.StockService&methods=deduct,batchDeduct&pid=9347&release=2.7.15&service.name=ServiceBean:/io.seata.samples.dubbo.service.StockService&side=provider&timeout=10000&timestamp=1695049605526 from /192.168.0.100:1234, dubbo version: 2.7.15, current host: 192.168.0.100
[INFO ] 2023-09-18 23:09:20,105 method:org.apache.dubbo.remoting.transport.netty4.NettyServerHandler.channelActive(NettyServerHandler.java:76)
 [DUBBO] The connection of /192.168.0.100:61009 -> /192.168.0.100:20882 is established., dubbo version: 2.7.15, current host: 192.168.0.100
代码语言:javascript
复制
 [DUBBO] Receive multicast message: unsubscribe consumer://192.168.0.100/io.seata.samples.dubbo.service.OrderService?application=dubbo-demo-app&category=providers,configurators,routers&check=false&dubbo=2.0.2&init=false&interface=io.seata.samples.dubbo.service.OrderService&methods=create&pid=10198&qos.accept.foreign.ip=false&qos.enable=false&qos.port=33333&release=2.7.15&side=consumer&sticky=false&timestamp=1695049759102 from /192.168.0.100:1234, dubbo version: 2.7.15, current host: 192.168.0.100
[WARN ] 2023-09-18 23:09:22,455 method:org.apache.dubbo.remoting.transport.AbstractServer.disconnected(AbstractServer.java:174)
 [DUBBO] All clients has disconnected from /192.168.0.100:20883. You can graceful shutdown now., dubbo version: 2.7.15, current host: 192.168.0.100
[INFO ] 2023-09-18 23:09:22,456 method:org.apache.dubbo.remoting.transport.netty4.NettyServerHandler.channelInactive(NettyServerHandler.java:91)
 [DUBBO] The connection of /192.168.0.100:61008 -> /192.168.0.100:20883 is disconnected., dubbo version: 2.7.15, current host: 192.168.0.100

最后启动测试程序

代码语言:javascript
复制
[INFO ] 2023-09-18 23:09:18,851 method:org.apache.dubbo.config.bootstrap.DubboBootstrap.isUsedRegistryAsCenter(DubboBootstrap.java:762)
 [DUBBO] The registry[<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" protocol="multicast" port="1234" />] will be not used as the config center, dubbo version: 2.7.15, current host: 192.168.0.100
[INFO ] 2023-09-18 23:09:19,016 method:org.apache.dubbo.config.utils.ConfigValidationUtils.validateMonitorConfig(ConfigValidationUtils.java:471)
 [DUBBO] No valid monitor config found, specify monitor info to enable collection of Dubbo statistics, dubbo version: 2.7.15, current host: 192.168.0.100

执行完成后我们发现数据库已经更改

代码语言:javascript
复制
mysql> select * from account_tbl;
+----+---------+-------+
| id | user_id | money |
+----+---------+-------+
|  2 | U100001 |   599 |
+----+---------+-------+
1 row in set (0.00 sec)

mysql> select * from order_tbl;
+----+---------+----------------+-------+-------+
| id | user_id | commodity_code | count | money |
+----+---------+----------------+-------+-------+
|  1 | U100001 | C00321         |     2 |   400 |
+----+---------+----------------+-------+-------+
1 row in set (0.02 sec)

当然也可以改大购买数量,这个时候事务回滚,数据库不变

代码语言:javascript
复制
  business.purchase("U100001", "C00321", 200);
代码语言:javascript
复制
[INFO ] 2023-09-18 23:09:22,476 method:io.seata.core.rpc.netty.AbstractNettyRemotingClient$ClientHandler.close(AbstractNettyRemotingClient.java:461)
ChannelHandlerContext(AbstractNettyRemotingClient$ClientHandler#0, [id: 0x4085cc4c, L:/127.0.0.1:61010 ! R:/127.0.0.1:8091]) will closed
[INFO ] 2023-09-18 23:09:22,477 method:io.seata.core.rpc.netty.AbstractNettyRemotingClient$ClientHandler.close(AbstractNettyRemotingClient.java:461)
ChannelHandlerContext(AbstractNettyRemotingClient$ClientHandler#0, [id: 0x4085cc4c, L:/127.0.0.1:61010 ! R:/127.0.0.1:8091]) will closed
[INFO ] 2023-09-18 23:09:22,481 method:com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.destroy(AbstractAnnotationBeanPostProcessor.java:354)
class org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor was destroying!

可以看到,seata可以非常容易和dubbo结合,执行分布式事务。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-09-21 00:00,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档