前提:invoke调用order和store,因为是一个demo没有做太细的处理,有一些重复maven依赖其实可以创建一个父类统一管理。 invoke的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.9</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<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-starter-alibaba-seata</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
store和order的pom
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>${mybatis-generator.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<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-starter-alibaba-seata</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
invoke.serviceImpl
@Service
public class InvokeServiceImpl implements InvokeService {
@Autowired
private OrderClientApi orderClientApi;
@Autowired
private StoreClientApi storeClientApi;
@GlobalTransactional
@Transactional
@Override
public int invokeHandler() {
System.out.println("invokeHandler" + RootContext.getXID());
String s = orderClientApi.addOrder();
int i = 1 / 0;
String s1 = storeClientApi.addStore();
if ("success".equals(s) && "success".equals(s1)){
return 1;
}
return 0;
}
}
invoke的application.yml
server:
port: 8001
spring:
application:
name: zongdapao-invoke
cloud:
nacos:
discovery:
group: SEATA_GROUP
seata:
data-source-proxy-mode: XA
registry:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
username: nacoa
password: nacos
group: SEATA_GROUP
application: seata-server
tx-service-group: seata-zzx
service:
vgroup-mapping:
seata-zzx: default
enabled: true
config:
type: nacos
nacos:
data-id: seataServer.properties
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: nacos
password: nacos
order和store的application.yml
server:
port: 8801
spring:
application:
name: zongdapao-order
datasource:
url: jdbc:mysql://localhost:3306/zzxmysql01?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
cloud:
nacos:
discovery:
group: SEATA_GROUP
mybatis:
mapper-locations:
- classpath:dao/*.xml
- classpath*:com/**/dao/*.xml
seata:
data-source-proxy-mode: XA
registry:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
username: nacoa
password: nacos
group: SEATA_GROUP
application: seata-server
tx-service-group: seata-zzx
service:
vgroup-mapping:
seata-zzx: default
enabled: true
config:
type: nacos
nacos:
data-id: seataServer.properties
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: nacos
password: nacos
接下来配置一下seata,我用的版本是1.4.2,需要向nacos上传两个配置文件 注意一下,第一张图更改为seata-zzx之后,要创建一个Data ID为service.vgroupMapping.seata-zzx的配置文件,配置内容是后面的default,也就是第二个配置文件。 下面说一下我遇到的问题:
java.lang.NoSuchMethodError: org.springframework.beans.factory.ListableBeanFactory.getBeanProvider(Ljava/lang/Class;Z)Lorg/springframework/…
这个错是jar包冲突;
java.lang.NoSuchMethodException: PropertySet.getBooleanReadableProperty(java.lang.Stri)
这个是mysql的版本问题,要选择8.0.11,我选择了以上的也不行。
XID为null问题
加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
打印一下XID是否不为null或者不相等
System.out.println("store" + RootContext.getXID());
~有写错的或者有更优雅的写法记得@ 我~
|