can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry

一、前言

最近在公司遇到分布式事务嵌套子事务的问题,用的也是seata,于是就准备自己研究一下seata。在搭建项目的过程中,发现一直无法将服务注册到Seata服务中,报错如下:

can not get cluster name in registry config ‘service.vgroupMapping.my-tx-group’, please make sure registry config correct

从日志来看,每10s疯狂报错:
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第1张图片
我们下面从两种模式:file和nacos进行介绍;

二、配置说明:

seata-server的配置从file.conf文件中取。

1、Seata-server配置

register.conf中 配置的config(配置中心)为type=“file”

# 配置中心
# 如果type=file,则从本地file.conf中获取配置参数,并且只有这种情况才从file.conf中加载配置参数
config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "file"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = ""
    group = "SEATA_GROUP"
  }
  
  file {
    name = "file.conf"
  }
}

再看file.conf:

service {
  # 事务组名称
  vgroup_mapping.my-tx-group = "seata-server"
  disableGlobalTransaction = false
}

注意:

1、vgroup_mapping.my-tx-group = "seata-server"为事务组名称,这里的值需要和TC中配置的service.vgroup-mapping.my-tx-group一致;
2、my-tx-group自定义名称,可以随便取,seata-server这个值也是一样;
3、事务组的命名不要用下划线’_‘,可以用’-'因为在seata的高版本中使用underline下划线 将导致service not to be found。

2、TC配置

application.yml

# seata 配置
seata:
  # 使用的事务组
  tx-service-group: my-tx-group
  enabled: true

看一些大佬的方式是在resources/目录下增加file.conf配置事务组my-tx-group的值,如下所示:
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第2张图片
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第3张图片
然后运行服务就崩了,大佬不会坑我吧,然后百思不得其解;于是便开始了百度、Google、再到StackOverflow的过程;最后在seata官方的issue 411中发现了一个类似的问题:https://github.com/seata/seata-samples/issues/411。

不同的是issue中采用的是nacos作为配置中心,而我采用的是file。
其中提到了一点,必须要在nacos配置一个service.vgroupMapping.my-tx-group=seata-server。
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第4张图片
要不我直接切成nacos做为配置中心吧,但是我的问题还没解决啊,作为一个有专研精神的群体,不能就此放弃呀。

分析一下这个异常,can not get cluster name in registry config,在注册表配置中获取不到群集名称;其在resources/file.conf中配置了呀。我丢,nacos、consul等配置中心本质上只是把application.yml中的配置提到了云端;会不会是要在application.yml中配置的?

去官方看看配置说明:https://seata.io/zh-cn/docs/user/configurations.html

全局搜索 command(Ctrl) + F 搜索service.vgroupMapping
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第5张图片

我们再去application.yml中配置上试试看:

# seata 配置
seata:
  # 使用哪个事务组
  tx-service-group: my-tx-group
  service:
    # 事务组对应的集群民称
    vgroup-mapping.my-tx-group: seata-server
    # seata-server的地址
    grouplist.seata-server: 127.0.0.1:8091
  enabled: true

运行,内心OS(阿门,给我成):
在这里插入图片描述
注册RM成功,再看看seata-server端日志:
在这里插入图片描述
OK,搞定!

三、采用Nacos作为配置中心

在nacos中新增一个dataId为对应事务组的数据即可,可以参考上面提到的seata官方issue411:https://github.com/seata/seata-samples/issues/411。
在这里插入图片描述
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第6张图片

四、总结

虽说三人行必有我师焉,但听完要实践验证加固记忆。
比如这里我就犯了一个比较低级的错误,SpringBoot项目的配置怎么就从resources目录下的非application.yml、bootstrap.yml中拿了呢?
当我们也不确定谁对谁错的时候,找源码入口,debug源码后答案自见分晓。

我们这里的报错提示是在NettyClientChannelManager类中,我们就进入这个类找一下入口,怎么知道是哪个方法呢?从方法的命名我们来猜,猜了几次还不对,就打断点看。
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第7张图片
这里一共就这几个方法,从命名来看,经验告诉我们应该和Server信息相关,会不会是getAvailServerList()方法呢,试试看看吧,打断点hold住,debug启动:
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第8张图片
下面大家顺着往下走,会发现出问题的地方(这里记得把application.yml中的事务组信息注掉):
can not get cluster name in registry config ‘service.vgroupMapping.xx‘, please make sure registry_第9张图片

希望我们都会用、善用这种方式解决问题。

回想自己以前负责的一个服务中因为引入配置中心consul,导致项目中的定时任务全部失效,也是通过这种方式找到了问题所在。

你可能感兴趣的:(微服务,java,微服务,spring,cloud,seata,nacos)