SpringcloudAlibaba2.2.5 +seata1.4+naco2.0.3环境搭建

前置工作:

??Centos7下Seata的安装和配置??

??Centos7下Nacos安装和部署??

??Spring Cloud Alibaba、Seata、Nacos之间对应的版本??

注意1:

修改seata Server端存储模式(store.mode)配置file.conf,不要使用默认的方式,最好使用db存储方式。

安装目录–>seata-server–>resources–>file.conf,修改store.mode=“db”

2:

SpringcloudAlibaba2.2.5之前的版本需要在resource目录下创建 “file.conf” 和 “registry.conf” 文件。高于这个版本的 则不需要这样创建了。

可以在application.yml中直接配置:

3:

??阿里中间件SEATA源码剖析四:AT模式中UNDOLOG实现?? 每个涉及到分布式事务的微服务的数据库都要有一张undo_log日志表,sql如下:

SET FOREIGN_KEY_CHECKS=0; — —————————— Table structure for undo_log– —————————-DROP TABLE IF EXISTS `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, `ext` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;在项目中的使用:1.添加依赖:<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <exclusions> <!–版本较低,1.3.0,因此排除–> <exclusion> <artifactId>seata-spring-boot-starter</artifactId> <groupId>io.seata</groupId> </exclusion> </exclusions> </dependency> <!–seata starter 采用1.4.2版本–> <dependency> <groupId>io.seata</groupId> <artifactId>seata-spring-boot-starter</artifactId> <version>1.4.2</version> </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> <!– springCloud –> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR8</version> <type>pom</type> <scope>import</scope> </dependency>2.application.yml:server: port: 8082spring: application: name: order-service datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///seata_demo?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false username: root password: root cloud: nacos: server-addr: localhost:8848mybatis-plus: global-config: db-config: insert-strategy: not_null update-strategy: not_null id-type: autologging: level: org.springframework.cloud.alibaba.seata.web: debug cn.itcast: debug pattern: dateformat: MM-dd HH:mm:ss:SSSseata: registry: # TC服务注册中心的配置,微服务根据这些信息去注册中心获取tc服务地址 # 参考tc服务自己的registry.conf中的配置 type: nacos nacos: # tc server-addr: 127.0.0.1:8848 namespace: “” group: DEFAULT_GROUP application: seata-tc-server # tc服务在nacos中的服务名称 username: nacos password: nacos tx-service-group: seata-demo # 事务组,根据这个获取tc服务的cluster名称 service: vgroup-mapping: # 事务组与TC服务cluster的映射关系 seata-demo: WH data-source-proxy-mode: AT #XA3:在你需要进行微服务间调到的方法上添加注解@GlobalTransactional/** * 使用了seata的分布式事务管理, * 因为create()方法会去调用其他远程服务的接口,所有需要再这使用 * @GlobalTransactional,开启全局事务,要么都成功,要么都失败 * * @param order * @return */ @Override @GlobalTransactional public Long create(Order order) { // 创建订单 orderMapper.insert(order); try { // 扣用户余额 accountClient.deduct(order.getUserId(), order.getMoney()); // 扣库存 storageClient.deduct(order.getCommodityCode(), order.getCount()); } catch (FeignException e) { log.error(“下单失败,原因:{}”, e.contentUTF8(), e); throw new RuntimeException(e.contentUTF8(), e); } return order.getId(); }常见报错:1.nacos配置读取不到[imeoutChecker_1] i.s.c.r.netty.NettyClientChannelManager : no available service ‘null’ found, please make sure registry config correct

出现上述异常,可能是nacos配置没有读取到,需要确认下nacos的版本,看下nacos客服端版本是否和服务端不一致,或者版本较低。本demo中的nacos版本1.1.4版本,更nacos服务端版本一致

2.seata客服端和服务端版本不一致[imeoutChecker_1] i.s.c.r.netty.NettyClientChannelManager : no available service ‘default’ found, please make sure registry config correct

nacos配置可以读取,但是seata客服端版本太低,本demo服务端版本是1.4.0,所以客服端版本需要升级。版本不一致也会造成上述异常

3.seata客服端版本太低org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.seata.spring.annotation.GlobalTransactionScanner]: Factory method ‘globalTransactionScanner’ threw exception; nested exception is io.seata.common.exception.ShouldNeverHappenException: Can’t find any object of class org.springframework.context.ApplicationContext

这个原因是引用的spring-cloud-starter-alibaba-seata包中的seata-all版本太低,和服务端版本对不上,需要剔除掉包中低版本的seata-all

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.1.0.RELEASE</version> <exclusions> <exclusion> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> </exclusion> </exclusions> </dependency>

【文章原创作者:建湖网站设计 jianhu.html 欢迎留下您的宝贵建议】相信成功的信念比成功本身更重要,

SpringcloudAlibaba2.2.5 +seata1.4+naco2.0.3环境搭建

相关文章:

你感兴趣的文章:

标签云: