Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

2年前 (2022) 程序员胖胖胖虎阿
335 0 0

文章目录

  • 一、详细报错信息
  • 二、原因分析
    • 原因1:`service.vgroupMapping`配置的服务组名称不符合Seata默认要求;
    • 原因2:`service.vgroupMapping`配置的seata集群名称没有对应的grouplist
  • 三、解决方案
    • 方案1、将file.conf中service.vgroupMapping配置调整为`${spring.application.name}-seata-service-group`;
    • 方案二、在application.yml中指定`seata.tx-service-group`
      • spring.cloud.alibaba.seata.tx-service-group 和 seata.tx-service-group
  • 四、seata集群名称的坑

一、详细报错信息

springcloud 集成 seata1.3.0 时报错:

2022-08-04 00:00:00.000 ERROR 78958 --- [eoutChecker_2_1] i.s.c.r.netty.NettyClientChannelManager  : Failed to get available servers: endpoint format should like ip:port

java.lang.IllegalArgumentException: endpoint format should like ip:port
	at io.seata.discovery.registry.FileRegistryServiceImpl.lookup(FileRegistryServiceImpl.java:95) ~[seata-all-1.3.0.jar:1.3.0]
	at io.seata.core.rpc.netty.NettyClientChannelManager.getAvailServerList(NettyClientChannelManager.java:217) ~[seata-all-1.3.0.jar:1.3.0]
	at io.seata.core.rpc.netty.NettyClientChannelManager.reconnect(NettyClientChannelManager.java:162) ~[seata-all-1.3.0.jar:1.3.0]
	at io.seata.core.rpc.netty.AbstractNettyRemotingClient$1.run(AbstractNettyRemotingClient.java:106) [seata-all-1.3.0.jar:1.3.0]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_275]
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_275]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_275]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_275]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_275]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_275]
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-all-4.1.58.Final.jar:4.1.58.Final]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_275]

二、原因分析

本文基于seata1.3.0版本,代表这1.X的版本,0.x版本的略有不同。

在文件类型的注册服务时,会通过FileRegistryServiceImpl#lookup(String)方法根据VgroupMapping的key去寻找可用的Seata-server实例;报错的体现正是无法根据service.VgroupMapping这个配置找到具体的Seata实例(IP:PORT信息);

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

几个字符串常量值如下:

        String PREFIX_SERVICE_MAPPING = "vgroupMapping.";
        String PREFIX_SERVICE_ROOT = "service";
    String CONFIG_SPLIT_CHAR = ".";
    private static final String POSTFIX_GROUPLIST = ".grouplist";
    private static final String ENDPOINT_SPLIT_CHAR = ";";
    private static final String IP_PORT_SPLIT_CHAR = ":";

获取当前服务组名称对应的seata集群名称的代码逻辑如下:

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

针对 service.vgroupMapping,官方案例:Spring Cloud 快速集成 Seata是这么说的:

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

划重点:需要注意的是 service.vgroupMapping这个配置,在 Spring Cloud 中默认是${spring.application.name}-fescar-service-group

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

再看博主debug时的默认 service.vgroupMapping配置,居然是${spring.application.name}-seata-service-group

  • 我程序的spring.application.nametrade-center
  • service.vgroupMapping的配置为trade-center-seata-service-group不说好的是${spring.application.name}-fescar-service-group吗!!!!!!!

等下我们再继续说,先让我喷一会:***********,官方文档不更新的吗?不注意版本之间的差异吗?不标明版本差异吗?******

所以原因本质上只有一个:无法根据service.VgroupMapping这个配置找到具体的Seata实例(IP:PORT信息);但导致该原因产生的方式会有很多种;

原因1:service.vgroupMapping配置的服务组名称不符合Seata默认要求;

前提:不在application.yml配置文件中手动通过seata.tx-service-group属性指定seata服务组名称。

默认 service.vgroupMapping这个配置,在 Spring Cloud 中默认是${spring.application.name}-seata-service-group

所以,当我们未按${spring.application.name}-seata-service-group这个规则配置service.vgroupMapping时会报错。

报错配置样例:
Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)
解决方案对应下面的方案一、方案二。

原因2:service.vgroupMapping配置的seata集群名称没有对应的grouplist

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

比如:这里我配置的service.vgroupMapping值为seata-server-sh,而配置的grouplist是属于default的;

所以报错是因为通过service.vgroupMapping配置找到seata集群名称seata-server-sh,但是seata-server-sh没有对应的grouplist,即seata实例信息。
Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

解决方案:把grouplist的的所属方调整为和service.vgroupMapping配置的seata集群名称一致;

三、解决方案

方案1、将file.conf中service.vgroupMapping配置调整为${spring.application.name}-seata-service-group

就博主程序而言,application.yml、file.conf配置如下:

1> application.yml:

spring:
  application:
    name: trade-center

2> file.conf关键配置:

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

方案二、在application.yml中指定seata.tx-service-group

就博主程序而言,application.yml、file.conf配置如下:

1> application.yml:

spring:
  application:
    name: trade-center
seata:
  # tx-service-group的值一定要和file.conf中service.vgroupMapping配置对应上
  tx-service-group: saint_trade_tx_group

或者使用:

spring:
  application:
    name: trade-center
  cloud:
    alibaba:
      seata:
        # tx-service-group的值一定要和file.conf中service.vgroupMapping配置对应上
        tx-service-group: saint_trade_tx_group

2> file.conf关键配置:

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

我们注意到可以使用spring.cloud.alibaba.seata.tx-service-groupseata.tx-service-group 属性指定service.vgroupMapping配置,为啥呢?

spring.cloud.alibaba.seata.tx-service-group 和 seata.tx-service-group

spring.cloud.alibaba.seata.tx-service-group 属于SpringCloudAlibabaConfiguration类:

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

seata.tx-service-group 属性属于SeataProperties类:

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

SeataProperties类又被注解@EnableConfigurationProperties(SpringCloudAlibabaConfiguration.class)标注,所以会使SpringCloudAlibabaConfiguration的配置生效;又SeataProperties类中通过@Autowired的方式组合了SpringCloudAlibabaConfiguration,在通过getTxServiceGroup()方法获取txServiceGroup属性时如果SeataProperties类自己没有配置txServiceGroup,则从SpringCloudAlibabaConfiguration中获取;

Seata Failed to get available servers: endpoint format should like ip:port 报错原因/解决方案汇总版(看完本文必解决问题)

简单来说;就是优先取 seata.tx-service-group 属性值,没有则再取spring.cloud.alibaba.seata.tx-service-group属性值。

四、seata集群名称的坑

seata-server和 项目程序 部署在不同的机器上时,可能会出现 can not connect to services-server

如果配置的 default.grouplist = “192.168.7.254:8091”,则并不会生效,default.grouplist读取的还是默认的127.0.0.1:8091

建议把seata集群名称调整为非dafault,例如:

service {
  vgroupMapping.saint_trade_tx_group = "seata-server-sh"
  seata-server-sh.grouplist = "127.0.0.1:8091"
}

相关文章

暂无评论

暂无评论...