这里就不赘述服务端的配置了,只写客户端
客户端
pom文件
dependencies>
<!--nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--seata-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.5.2</version>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
在nacos上面创建事务组
在 nacos 增加配置 Data ID:service.vgroupMapping.default 配置格式:properties 内容default
service.vgroupMapping 前面固定的格式,后面加**.default**。后面这个可以是client端的spring.application.name的value值,但是它们拼接起来要和nacos的添加的配置文件要保持一致
对应客户端的spring.application.name
客户端application.yaml
server:
port: 2001
spring:
application:
name: seata-order-service
cloud:
alibaba:
seata:
# 这个名称需要和Seata-server中配置的名称相同
tx-service-group: default
nacos:
discovery:
server-addr: ip:端口
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://ip:3306/seata_order?useUnicode=true&characterEncoding=utf-8&createDatabaseIfNotExist=true&useSSL=false
username: root
password: ***
feign:
hystrix:
enabled: false
logging:
level:
io:
seata: info
mybatis:
mapperLocations: classpath:mapper/*.xml
seata: #设置seata的通讯地址 去nacos 找 seata服务端 TC的地址
tx-service-group: default
# 服务配置项
service:
# 虚拟组和分组的映射
vgroup-mapping:
seata-user-group: default
# 关闭自动代理
enable-auto-data-source-proxy: true
#Seata 应用编号,默认为 ${spring.application.name}
application-id: ${spring.application.name}
registry:
type: nacos
nacos:
server-addr: ip:端口
namespace: ""
group: SEATA_GROUP
username: nacos
password: ***
cluster: default
application: seata-server #服务name要和nacos中保持一致 默认就是seata-server
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr:ip:端口
namespace: 42445642-cff9-40b9-ba32-d9063670e58d
group: SEATA_GROUP
username: nacos
password: ***
主启动
@EnableDiscoveryClient
@EnableFeignClients
@EnableTransactionManagement
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class SeataOrderApplication {
public static void main(String[] args) {
SpringApplication.run(SeataOrderApplication.class,args);
}
}
值得注意的是如果配置了druid数据源的要删掉比如这样
//@Configuration
public class DataSourceProxyConfig {
@Value("${mybatis.mapperLocations}")
private String mapperLocations;
// @Bean
// @ConfigurationProperties(prefix = "spring.datasource")
// public DataSource druidDataSource() {
// return new DruidDataSource();
// }
// @Bean
// public DataSourceProxy dataSourceProxy(DataSource dataSource) {
// return new DataSourceProxy(dataSource);
// }
//
// @Bean
// public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
// SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// sqlSessionFactoryBean.setDataSource(dataSourceProxy);
// sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
// sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
// return sqlSessionFactoryBean.getObject();
// }
}
seata有自己的数据源,所以使用druid会冲突报错