Eureka 注册了奇怪的 IP 地址并且请求无法使用 zuul

Posted

技术标签:

【中文标题】Eureka 注册了奇怪的 IP 地址并且请求无法使用 zuul【英文标题】:Eureka Registered with weird IP address and Requests not working with zuul 【发布时间】:2018-09-14 22:10:38 【问题描述】:

我正在使用 SpringBoot 开发具有微服务架构的 Rest Backend。我使用 Eureka 作为发现服务,使用 Zuul 作为 API 网关。在本地主机中工作时一切正常。但是当我在云上部署服务时,zuul api gateway 的行为并不像预期的那样。所有服务仍然在 eureka 中注册。 请求到 api 网关没有转发到所需的服务,而是卡在 api 网关上并给

com.netflix.zuul.exception.ZuulException: Filter threw Exception
    at com.netflix.zuul.FilterProcessor.processZuulFilter(FilterProcessor.java:227) ~[zuul-core-1.3.1.jar:1.3.1]
    at com.netflix.zuul.FilterProcessor.runFilters(FilterProcessor.java:157) ~[zuul-core-1.3.1.jar:1.3.1]
    at com.netflix.zuul.FilterProcessor.route(FilterProcessor.java:118) ~[zuul-core-1.3.1.jar:1.3.1]
    at com.netflix.zuul.ZuulRunner.route(ZuulRunner.java:96) ~[zuul-core-1.3.1.jar:1.3.1]
    at com.netflix.zuul.http.ZuulServlet.route(ZuulServlet.java:116) ~[zuul-core-1.3.1.jar:1.3.1]
    at com.netflix.zuul.http.ZuulServlet.service(ZuulServlet.java:81) ~[zuul-core-1.3.1.jar:1.3.1]
    at org.springframework.web.servlet.mvc.ServletWrappingController.handleRequestInternal(ServletWrappingController.java:165) [spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ...
    at com.netflix.hystrix.HystrixCommand.queue(HystrixCommand.java:378) ~[hystrix-core-1.5.12.jar:1.5.12]
    at com.netflix.hystrix.HystrixCommand.execute(HystrixCommand.java:344) ~[hystrix-core-1.5.12.jar:1.5.12]
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.forward(RibbonRoutingFilter.java:159)
    ... 96 common frames omitted
Caused by: java.lang.RuntimeException: java.net.UnknownHostException: 3b7b691a42eb: unknown error
    at rx.exceptions.Exceptions.propagate(Exceptions.java:57) ~[rxjava-1.3.6.jar:1.3.6]
    at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:463) ~[rxjava-1.3.6.jar:1.3.6]
    at rx.observables.BlockingObservable.single(BlockingObservable.java:340) ~[rxjava-1.3.6.jar:1.3.6]
    at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112) ~[ribbon-loadbalancer-2.2.5.jar:2.2.5]
    ... 158 common frames omitted
Caused by: java.net.UnknownHostException: 3b7b691a42eb: unknown error
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_65]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_65]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_65]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[na:1.8.0_65]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_65]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_65]
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381) ~[httpclient-4.5.5.jar:4.5.5]
    ...
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.6.jar:1.3.6]
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.6.jar:1.3.6]
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.6.jar:1.3.6]
    at rx.Observable.subscribe(Observable.java:10352) ~[rxjava-1.3.6.jar:1.3.6]

这是我的zuul配置

zuul:
    prefix: /api/v1
      routes:
        user-server:
          path: /user/**
          serviceId: USER-SERVER
        developer-server:
          path: /developer/**
          serviceId: DEVELOPER-SERVER

    spring:
      application:
        name: zuul-server

    server:
      port: 5000

    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone : http://eureka_hosted_ip:8070/eureka/   


hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 2000000
    ribbon:
        ConnectTimeout: 200000
        ReadTimeout: 200000

我已经在 docker 容器中部署了服务。我做错了什么?

【问题讨论】:

【参考方案1】:

我发现了错误。错误是在注册 Eureka 服务器时,我的服务没有使用正确的 IP 地址注册。因为它是在 docker 容器上运行的,所以它没有正确注册 ip。 因此,在运行 docker 时,我必须将 --network=host 部分与 docker run 命令一起使用。当我在 eureka 正确注册时,一切正常。

【讨论】:

以上是关于Eureka 注册了奇怪的 IP 地址并且请求无法使用 zuul的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Eureka源码分析---服务注册

多网卡环境下Eureka服务注册IP选择问题

Spring Cloud Eureka 使用 IP 地址进行服务注册

Spring Cloud Eureka 使用 IP 地址进行服务注册

Spring Cloud Eureka 使用 IP 地址进行服务注册

我可以强制 Eureka 注册使用 IP 地址而不是主机名吗?