irpas技术客

springcloud + nacos整合seata1.4.2_wanwandeyue

irpas 6675

部署seata1.4.2

1、下载??Releases · seata/seata · GitHub

?

文件解压后

1) D:\Program Files\seata-1.4.2\script\config-center\config.txt

放入到

D:\Program Files\seata-server-1.4.2?目录下

2) D:\Program Files\seata-1.4.2\script\config-center\nacos目录下文件

放入到

D:\Program Files\seata-server-1.4.2\bin目录下

3)数据库中建库seata,执行语句

CREATE TABLE `global_table` ( `xid` varchar(128) NOT NULL, `transaction_id` bigint(20) DEFAULT NULL, `status` tinyint(4) NOT NULL, `application_id` varchar(32) DEFAULT NULL, `transaction_service_group` varchar(32) DEFAULT NULL, `transaction_name` varchar(128) DEFAULT NULL, `timeout` int(11) DEFAULT NULL, `begin_time` bigint(20) DEFAULT NULL, `application_data` varchar(2000) DEFAULT NULL, `gmt_create` datetime DEFAULT NULL, `gmt_modified` datetime DEFAULT NULL, PRIMARY KEY (`xid`), KEY `idx_gmt_modified_status` (`gmt_modified`,`status`), KEY `idx_transaction_id` (`transaction_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `branch_table` ( `branch_id` bigint(20) NOT NULL, `xid` varchar(128) NOT NULL, `transaction_id` bigint(20) DEFAULT NULL, `resource_group_id` varchar(32) DEFAULT NULL, `resource_id` varchar(256) DEFAULT NULL, `branch_type` varchar(8) DEFAULT NULL, `status` tinyint(4) DEFAULT NULL, `client_id` varchar(64) DEFAULT NULL, `application_data` varchar(2000) DEFAULT NULL, `gmt_create` datetime(6) DEFAULT NULL, `gmt_modified` datetime(6) DEFAULT NULL, PRIMARY KEY (`branch_id`), KEY `idx_xid` (`xid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `lock_table` ( `row_key` varchar(128) NOT NULL, `xid` varchar(128) DEFAULT NULL, `transaction_id` bigint(20) DEFAULT NULL, `branch_id` bigint(20) NOT NULL, `resource_id` varchar(256) DEFAULT NULL, `table_name` varchar(32) DEFAULT NULL, `pk` varchar(36) DEFAULT NULL, `gmt_create` datetime DEFAULT NULL, `gmt_modified` datetime DEFAULT NULL, PRIMARY KEY (`row_key`), KEY `idx_branch_id` (`branch_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4) 修改D:\Program Files\seata-server-1.4.2\conf目录下文件file.conf,registry.conf

## transaction log store, only used in seata-server store { ## store mode: file、db、redis mode = "db" ## rsa decryption public key publicKey = "" ## database store property db { ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc. datasource = "druid" ## mysql/oracle/postgresql/h2/oceanbase etc. dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true" user = "mysql" password = "mysql" minConn = 5 maxConn = 100 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 } }

改成自己的数据库信息

registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" nacos { application = "seata-server" serverAddr = "127.0.0.1:8848" group = "SEATA_GROUP" namespace = "5a3c7d6c-f497-4d68-a71a-2e5e3340b3ca" cluster = "default" username = "nacos" password = "nacos" } } config { # file、nacos 、apollo、zk、consul、etcd3 type = "file" file { name = "file.conf" } }

修改成自己的nacos服务信息

5)打个包放入到Linux中,执行语句。默认端口8091

/opt/seata/seata-server-1.4.2/bin/seata-server.sh > /opt/seata/seata-server-1.4.2/logs/seata.log 2>&1 &

项目部署

1、父级pom.xml引入

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR9</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency>

2、子级pom.xml引入seata包

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>

3、代码、配置

seata: application-id: seata-server tx-service-group: my_test_tx_group # 事务组 config.txt配置 service: vgroup-mapping: my_test_tx_group: default @RequestMapping @GlobalTransactional public void test(Integer type) throws Exception{ System.out.println("welcome to order"); // try{ System.out.println(RootContext.getXID()); chargerService.test(type); userService.test(type); try{ Thread.sleep(5000l); }catch (Exception e){ log.error(e.getMessage()); e.printStackTrace(); } if (1==type){ int i = 1/0; } System.out.println("--------------->结束"); }

4、启动服务后,通过命令测试

curl http://localhost:9001/test?type=1

5、在睡眠期间可通过查询数据库seata知道数据变化

?global_table信息

?branch_table信息

?lock_table信息

?seata日志(一个回滚、一个提交)

?

?


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #springcloud #nacos整合seata142 #seataseata #GitHub文件解压后1