记录 | 解决连接zookeeper报错No route to host

阅读本文大约需要3分钟。

最近在学习微服务,遇到了很多问题,今天又遇到了个连接zookeeper报错的问题,在此记录下。

如下为:学生课程管理系统简单微服务架构图:

各个服务docker化以后,用docker-compose启动,其中docker-compose.yml文件如下:

version: "3"

services:
  message-service:
    image: message-thrift-python-service:latest

  user-service:
    image: user-thrift-service:latest
    command:
    - "--mysql.address=192.168.1.103"

  user-edge-service:
    image: user-edge-service:latest
    links:
    - user-service
    - message-service
    command:
    - "--redis.address=192.168.1.103"

  course-service:
    image: course-service:latest
    links:
    - user-service
    command:
    - "--mysql.address=192.168.1.103"
    - "--zookeeper.address=192.168.1.103"

  course-edge-service:
    image: course-edge-service:latest
    links:
    - user-edge-service
    command:
    - "--zookeeper.address=192.168.1.103"

  api-gateway-zuul:
    image: api-gateway-zuul:latest
    links:
    - course-edge-service
    - user-edge-service
    ports:
    - 8080:8080

docker-compose up -d启动后,访问course-edge-service服务时报错,日志里打印如下错误:

2018-10-04 08:32:32.584  INFO 1 --- [168.1.103:2181)] org.apache.zookeeper.ClientCnxn          : Opening socket connection to server 192.168.1.103/192.168.1.103:2181. Will not attempt to authenticate using SASL (unknown error)
2018-10-04 08:32:33.590  WARN 1 --- [168.1.103:2181)] org.apache.zookeeper.ClientCnxn          : Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect

java.net.NoRouteToHostException: No route to host
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_181]
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_181]
    at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361) ~[zookeeper-3.4.6.jar!/:3.4.6-1569965]
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081) ~[zookeeper-3.4.6.jar!/:3.4.6-1569965]

于是先docker-compose down掉各服务,暂且单独启动course-edge-service单服务看是否同样报错:

docker run -ti course-edge-service:latest --zookeeper.address=192.168.1.103

启动后报同样的错。

看报错日志应该就是连不上zookeeper,在宿主机上curl zookeeper地址,是通的:

[root@localhost course-edge-service]# curl 192.168.1.103:2181
curl: (52) Empty reply from server

进入zookeeper容器内看zookeeper状态:

[root@localhost course-edge-service]# docker exec -ti c2803533c4fb bash
bash-4.4# cd bin/
bash-4.4# ./zk
zkCleanup.sh            zkEnv.cmd               zkServer.cmd            zkTxnLogToolkit.sh
zkCli.cmd               zkEnv.sh                zkServer.sh             
zkCli.sh                zkServer-initialize.sh  zkTxnLogToolkit.cmd     
bash-4.4# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
bash-4.4#

zookeeper状态正常。

在course-edge-service的容器内curl zookeeper地址:

[root@localhost course-edge-service]# docker exec -ti 5c993840aeae bash
root@5c993840aeae:/# curl 192.168.1.103:2181
curl: (7) Failed to connect to 192.168.1.103 port 2181: No route to host
root@5c993840aeae:/# 
root@5c993840aeae:/# ping 192.168.1.103
PING 192.168.1.103 (192.168.1.103) 56(84) bytes of data.
64 bytes from 192.168.1.103: icmp_seq=1 ttl=64 time=0.255 ms
64 bytes from 192.168.1.103: icmp_seq=2 ttl=64 time=0.163 ms
64 bytes from 192.168.1.103: icmp_seq=3 ttl=64 time=0.164 ms

发现确实有问题。

解决问题:

查看防火墙状态:

$ systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-10-04 00:38:32 CST; 15h ago
 Main PID: 826 (firewalld)
   Memory: 2.4M
   CGroup: /system.slice/firewalld.service
           └─826 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

关闭防火墙:

$ systemctl stop firewalld.service

course-edge-service的容器内再curl zookeeper地址:

[root@localhost course-edge-service]# docker exec -ti 5c993840aeae bash
root@5c993840aeae:/# curl 192.168.1.103:2181
curl: (52) Empty reply from server
root@5c993840aeae:/# 

重新启动各个微服务,又报错如下:

[root@localhost micro-service]# docker-compose up -d
Creating network "micro-service_default" with the default driver
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule:  (iptables failed: iptables --wait -t nat -I DOCKER -i br-07454d600e41 -j RETURN: iptables: No chain/target/match by that name.
 (exit status 1))

重启docker后问题解决:

[root@localhost micro-service]#systemctl restart docker
[root@localhost micro-service]# docker-compose up -d
Creating network "micro-service_default" with the default driver
Creating micro-service_user-service_1_11592de6bbc3    ... done
Creating micro-service_message-service_1_b225fa599858 ... done
Creating micro-service_user-edge-service_1_4f8b6edf7333 ... done
Creating micro-service_course-service_1_69e9bdcbd81e    ... done
Creating micro-service_course-edge-service_1_81d9188df304 ... done
Creating micro-service_api-gateway-zuul_1_e0bdf98eebef    ... done

原文发布于微信公众号 - 我的小碗汤(mysmallsoup)

原文发表时间:2018-10-04

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏小白安全

渗透测试利器-大数量IP地址以及端口扫描

渗透测试利器  IP 段 端口扫描 利器一: 把IP段用以下格式放到url里 https://censys.io/ipv4?q=ip:[221.133.11.0...

60950
来自专栏一个会写诗的程序员的博客

第11章 Spring Boot应用监控第11章 Spring Boot应用监控小结

在实际的生产系统中,我们怎样知道我们的应用运行良好呢?我们往往需要对系统实际运行的情况(各种cpu,io,disk,db,业务功能等指标)进行监控运维。这需要耗...

33830
来自专栏一个会写诗的程序员的博客

React脚手架 create-react-app 快速上手教程Kotlin 开发者社区

You can now view hello-react-demo in the browser.

21720
来自专栏安恒网络空间安全讲武堂

HackTheBox - Poison Writeup

来源:https://www.absolomb.com/2018-09-08-HackTheBox-Poison/

17320
来自专栏林德熙的博客

WPF 如何调试 binding

如果是写在 xaml 的绑定,很难看到是那里出错 如何做 vs 调试 binding?

23310
来自专栏Java架构师历程

使用Spring Boot,JPA,Hibernate和Postgres的多租户应用程序

多租户是一种方法,应用程序实例由不同的客户使用,从而降低软件开发和部署成本,与单一租户解决方案相比,在这种解决方案中,需要触及多个部分以提供新客户端或更新现有租...

2.7K30
来自专栏JMCui

Docker 系列七(Duubo 微服务部署实践).

    之前我们公司部署服务,就是大家都懂的那一套(安装JDK、Tomcat —> 编译好文件或者打war包上传 —> 启动Tomcat),这种部署方式一直持续...

31150
来自专栏Urahara Blog

Redis Hacking Tips

20850
来自专栏Urahara Blog

PHP Disabled_functions Bypass

68740
来自专栏运维前线

kvm基础使用及Virt-tools工具使用

kvm基础使用 查看虚拟机状态 [root@sh-kvm-1 ~]# virsh list --all Id Name ...

46880

扫码关注云+社区

领取腾讯云代金券