注意:命令空间的一致
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
}
cd conf
sh nacos-config.sh
1.4.0版本有下列配置参数,当前使用0.9.0版本
sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t cbf85741-7e38-4644-b333-140733a87e51 -u nacos -w nacos
-h -p 指定nacos的端口地址;-g 指定配置的分组,注意,是配置的分组;-t 指定命名空间id; -u -w指定nacos的用户名和密码,同样,这里开启了nacos注册和配置认证的才需要指定。
cd bin
seata-server.bat -p 9000 -h 127.0.0.1 -m file
-h 指定运行的ip,本地运行也要指定,否则连接不上
nacos出现新服务
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=1 DEFAULT CHARSET=utf8;
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
<!--nacos客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.3</version>
</dependency>
<!--nacos conf-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Seata是通过代理数据源实现事务分支的,且是@Primary默认的数据源,否则事务不会回滚
@Configuration
public class DataSourceProxyConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource druidDataSource() {
return new DruidDataSource();
}
@Primary
@Bean
public DataSourceProxy dataSource(DruidDataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}
}
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
}
tx-service-group 要与nacos-conf.txt的一致
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
namespace: seata
group: SEATA_GROUP
alibaba:
seata:
tx-service-group: product-service
//全局事务控制
@GlobalTransactional
public Order createOrder(Long pid){
//直接根据服务id获取服务
Product product = productFeignClient.findByPid(pid);
log.info("查询{}号商品:{}", pid, JSON.toJSONString(product));
pre.cg.entity.Order order = new pre.cg.entity.Order().setNumber(1).setPid(pid).setUid(1L);
log.info("订单信息{}", order.toString());
orderDao.save(order);
//扣库存
productFeignClient.reduceInventory(pid,1);
//mq发送
rocketMQTemplate.convertAndSend("order-topic",order);
return order;
}