nacos作为Seata的配置中心

7/1/2022 微服务

# 前述

上一节,我们在配置 seataregistry.conf 文件时,将 registry 部分的 type="file"换成了 type="nacos"。除了registry 部分,还有一处就是 config部分,用于存在seata的配置,默认 type="file",同样的,我们也可以使用Nacos作为seata的配置中心。

官方文档参考 (opens new window)

# 配置nacos

启动我们的naocs,新建命名空间,获取到 namespaceid997dd1a9-97b1-4608-b40c-844153e10f90

seata16

seata17

# 修改registry.conf 文件

# 1、修改registry.conf文件中的registry以及config部分,同时修改nacos部分的配置

registry {
  # 使用nacos作为注册中心
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = "997dd1a9-97b1-4608-b40c-844153e10f90"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  # 使用Nacos作为配置中心
  type = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = "997dd1a9-97b1-4608-b40c-844153e10f90"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 2、下载nacos-config.脚本config.txt点击进入下载页 (opens new window)

seata01

# 3、打开git bash 或 linux 类命令行,执行sh脚本

命令:
1、指定命名空间:
	sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 997dd1a9-97b1-4608-b40c-844153e10f90 -u nacos -w nacos

2、使用默认(public)命名空间:
	sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -u nacos -w nacos

官方github可查看:https://github.com/seata/seata/tree/develop/script/config-center

-h: host, the default value is localhost.

-p: port, the default value is 8848.

-g: Configure grouping, the default value is 'SEATA_GROUP'.

-t: Tenant information, corresponding to the namespace ID field of Nacos, the default value is ''.

-u: username, nacos 1.2.0+ on permission control, the default value is ''.

-w: password, nacos 1.2.0+ on permission control, the default value is ''.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

seata18

seata19

seata20

# 4、启动 seata-server.bat,并查看nacos服务,若seata服务注册成功,表示注册中心和配置中心成功

seata21

# 5、yml配置

resources目录下不再需要file.confregistry.conf文件

server:
  port: 2002
spring:
  application:
    ## 指定服务名称,在nacos中的名字
    name: order_service
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: admin
    url: jdbc:mysql://localhost:3306/seata_storage?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        register-enabled: true
        namespace: 997dd1a9-97b1-4608-b40c-844153e10f90
      config:
        server-addr: localhost:8848
        enabled: true
        file-extension: yaml
        namespace: 997dd1a9-97b1-4608-b40c-844153e10f90
## 客户端seata的相关配置
seata:
  ## 是否开启seata,默认true
  enabled: true
  application-id: ${spring.application.name}
  # 事务群组(可以每个应用独立取名,也可以使用相同的名字),要与服务端nacos-config.txt中service.vgroup_mapping的后缀对应
  tx-service-group: ${spring.application.name}-tx-group
  ## 配置中心的配置
  config:
    ## 使用类型nacos
    type: nacos
    ## nacos作为配置中心的相关配置,需要和server在同一个注册中心下
    nacos:
      ## 命名空间,需要server端(registry和config)、nacos配置client端(registry和config)保持一致
      namespace: 997dd1a9-97b1-4608-b40c-844153e10f90
      serverAddr: localhost:8848
      # 需要server端(registry和config)、nacos配置client端(registry和config)保持一致
      group: SEATA_GROUP
      username: "nacos"
      password: "nacos"
  registry:
    type: nacos
    nacos:
      # 需要和server端保持一致,即server在nacos中的名称,默认为seata-server
      application: seata-server
      server-addr: localhost:8848
      group: SEATA_GROUP
      namespace: 997dd1a9-97b1-4608-b40c-844153e10f90
      username: "nacos"
      password: "nacos"

mybatis:
  mapperLocations: classpath:mapper/*.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

# 6、业务代码调整

/**
 * 订单服务impl
 *
 * @author LiJunYi
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService
{
    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    /**
     * 添加 @GlobalTransactional即可
     */
    @Override
    @GlobalTransactional
    public void create(Order order)
    {
        log.info("----->开始新建订单");
        //1 新建订单
        orderDao.create(order);

        //2 扣减库存
        log.info("----->订单微服务开始调用库存,做扣减Count");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("----->订单微服务开始调用库存,做扣减end");

        //3 扣减账户
        log.info("----->订单微服务开始调用账户,做扣减Money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("----->订单微服务开始调用账户,做扣减end");

        //4 修改订单状态,从零到1,1代表已经完成
        log.info("----->修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        log.info("----->修改订单状态结束");

        log.info("----->下订单结束了,O(∩_∩)O哈哈~");

    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 异常情况

没有可用服务No available service,该问题是没有连接到seata-server造成,内容如下

io.seata.common.exception.FrameworkException: No available service
1

seata-server的配置列表中有一项比较重要的配置,该配置需要手动添加

service.vgroupMapping.orderServiceGroup=default,其中orderServiceGroup是你手动指定的名称,同时bootstrap.yml中的seata.tx-service-group的值要为orderServiceGroup,如没找到则会报错

十年
陈奕迅