如有帮助,欢迎留下足迹哦!

详情如下

code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 192.168.31.33:9001: 
connectex: No connection could be made because the target machine actively refused it."

此时连接状态为 TransientFailure

目录

背景

现象汇总

分析与解决

总结与附录


背景

GRPC客户端与服务端建立连接时我们都知道需要先Dial,报该错时实际上Dial操作是成功的,此时err为nil,但调用rpc接口时失败,报了该错。

现象汇总

1,  将客户端与服务端均放在本机,且地址写本机地址时也报改错,Dial成功,调用rpc接口失败;
Dial成功时的连接状态为Connecting,调用rpc接口失败前的连接状态变为TransientFailure;
    
  这两状态是什么意思呢?看下源码:

    const (// Idle indicates the ClientConn is idle. Idle 表示 ClientConn 处于空闲状态。Idle State = iota// Connecting indicates the ClientConn is connecting.  Connecting 表示 ClientConn 正在连接。Connecting// Ready indicates the ClientConn is ready for work. Ready 表示 ClientConn 已准备好工作。Ready// TransientFailure indicates the ClientConn has seen a failure but expects to recover.  TransientFailure 表示 ClientConn 已经看到失败但希望恢复。TransientFailure// Shutdown indicates the ClientConn has started shutting down.  Shutdown 表示 ClientConn 已经开始关闭。Shutdown
)

2, 将客户端与服务端均放在本机,且地址写127.0.0.1时成功,调用rpc接口也成功,一切正常;

3,将客户端与服务端均放在不同主机,且启动服务地址写127.0.0.1、客户端连接地址写服务端ip时失败也报改错,Dial成功,调用rpc接口失败;

不禁让人纳闷,到底是哪的问题?

分析与解决

以上现象出现时,服务端所在主机已开放了端口号为9001的tcp端口,因此排除端口开放问题;

另外,客户端与服务端均在本机时可成功,而在不同主机不成功,就说明了大概率还是网络不通方面的问题;

与此同时进行抓包,发现有从客户端流向服务端主机的包且端口正确,但来回都是R.和S的标记交替着,服务端给客户端一直回复R. 表明连接异常一直在被重置,同时这样的回复实际也和服务端未启动服务时的情形一致;

根据以上现象,基本确定是客户端没有识别到服务端的启动(实际上服务端启动着)

最终检查了代码得知问题出在了这里,启动服务时监听地址写的是

net.Listen("tcp", fmt.Sprintf("%s:%s", "127.0.0.1", 9001))

以下几种写法均可:

net.Listen("tcp", fmt.Sprintf(":%s", 9001))
net.Listen("tcp", fmt.Sprintf("%s:%s", "本机IP", 9001))
net.Listen("tcp", fmt.Sprintf("%s:%s", "0.0.0.0", 9001))

皆可成功!

其中第一、三个启动后打印出来的服务地址是[::]:9001
第二种是192.168.31.33:9001

总结与附录

那么总结一下原因,实际上就是127.0.0.1启动时仅本机可连接成功,只作用在本机,因为它是本机回环地址,
正常的网络包从3层进入2层,然后发出去,而发向回环地址的包,直接在IP层短路了,也就是发到IP层时直接被IP层接收、不再向下发送;
也就是说对于本机单机测试时可以直接用127.0.0.1。

1、如果监听地址是127.0.0.1:port,表示仅监听环回接口的数据,即本机发送给本机port端口的数据,其他主机发送过来的数据无法接收到。
2、如果监听地址是0.0.0.0:port,表示监听所有网络接口的数据,包括回环接口和其他网络接口,所有本机和其他主机发送的数据都可以接收到。
3、如果监听地址是本机ip:port,表示仅监听本机ip对应的网络接口。仅接收目的地址是本机ip的数据。(对本机发送给本机这种情况,如果目的主机地址是127.0.0.1则,收不到,仅当目的主机地址是本机ip时能够收到)。

实际上0.0.0.0风险较大,建议使用本机IP为妥。

解决grpc连接报错connectex: No connection could be made because the target machine actively refused it相关推荐

  1. consul注册报错 connectex: No connection could be made because the target machine actively refused it.

    consul注册报错 connectex: No connection could be made because the target machine actively refused it. Ge ...

  2. connectex: No connection could be made because the target machine actively refused it.

    关于阿里云服务器出现以下错误的原因: connectex: No connection could be made because the target machine actively refuse ...

  3. 关于docker报错:No connection could be made because the target machine actively refused it.

    我是在win10下跑的docker 事情是这样的,今天安装好douker跑起来后报了一个很奇怪的错误` C:\Users\Administrator>docker ps error during ...

  4. 【TDengine】解决 connectex: No connection could be made because the target machine actively refused it.

    目录 1.遇到问题 2.问题描述 3.问题解决 1.遇到问题 .connectex: No conne ction could be made because the target machine a ...

  5. Redis错误:No connection could be made because the target machine actively refused it

    问题: 利用go连接远程阿里云centos主机的redis,代码如下: redisCli := redis.NewClient(&redis.Options{Addr: url,Passwor ...

  6. grpc client No connection could be made because the target machine actively refused it

    grpc client No connection could be made because the target machine actively refused it 禁用防火墙 查看yaml中 ...

  7. 【无标题】nginx 502 解决 No connection could be made because the target machine actively refused it

    win11 已经搭建好的环境突然全部nginx502,127.0.01拒绝访问 nginx日志如下 无法连接fastcgi9000端口 connect() failed (10061: No conn ...

  8. nginx connect()错误(10061: No connection could be made because the target machine actively refused it)

    nginx偶尔会报下面这个错误: 2019/05/20 10:54:56 [error] 717920#661892: *16943633 connect() failed (10061: No co ...

  9. connect() failed (10061: No connection could be made because the target machine actively refused it

    nginx运行web前端项目的时候出现了以下错误 项目在加载保存的图片的过程中,报错了,看了一下Nginx的错误日志 日志出现了connect() failed (10061: No connecti ...

最新文章

  1. C#实现注册码功能编程总结
  2. getparameter的使用
  3. kubectl 创建pvc_动态挂载云盘(PVC)
  4. 腾讯游戏自研学术成果:基于图分割的网络表征学习初始化技术
  5. django开发个人博客
  6. 关于Trie的一些算法
  7. Linux centos 使用yum安装MySQL
  8. 5个元素排序java_Android-java-如何按对象内的某个值对对象列表进行排序
  9. bootstrap-multiselect.js如何动态更新select里的数据
  10. 大学计算机 学生成绩表格,学生成绩分析系统的设计与实现
  11. 极客大学产品经理训练营 用例Use Case 第8课总结
  12. 算法设计与分析期末复习题
  13. 读取WIND数据(行情、基金净值等)的SAS程序
  14. 创建React脚手架项目
  15. 抢Google等巨头生意,纽约大学小伙挖掘并出售自己数据
  16. 《管理者必读12篇》如何购买
  17. 【A40I-LVDS】
  18. Python知识点大全--如何学好Python
  19. 图书馆管理系统SRS
  20. Socket和Websocket

热门文章

  1. 【微信小程序】图片违法违规内容鉴别
  2. 126 篇殿堂级深度学习论文分类整理 从入门到应用 | 干货
  3. 刷脸支付开拓创走在时代的前沿
  4. 腾讯开放平台-QQ互联认证-未提交审核
  5. 2020年如何通过GitHub学生包认证Azure学生认证
  6. python实用性函数分享_17.【Python学习分享文章】function(函数)1
  7. Android 中英文切换及遇到的问题
  8. 解决xampp安装后遇到的常见问题
  9. 基于单片机的5V可调电源系统设计(#0457)
  10. 从零编写linux0.11 - 第三章 printk函数