【问题标题】:Spring Cloud : Zuul throwing "Load balancer does not have available server for client"Spring Cloud:Zuul 抛出“负载均衡器没有可用于客户端的服务器”
【发布时间】:2017-06-05 02:52:16
【问题描述】:

我正在尝试让一个简单的微服务注册到 Eureka 服务器,然后让 Zuul 代理微服务。我让微服务注册到 Eureka 服务器。但是,每当我启动 Zuul 服务时,我都会看到它没有注册到 Eureka 服务器。每当我尝试路由到微服务时,都会出现以下异常:

负载均衡器没有可用于客户端的服务器:微服务

我无法弄清楚问题出在哪里。任何帮助将不胜感激

下面是我为每个组件提供的 Spring Boot 类。

微服务组件

MicroserviceApplication.java

@SpringBootApplication(scanBasePackages = { "some.package.controller", "some.package.service" })
@EnableEurekaClient
@EnableJpaRepositories("some.package.repository")
@EntityScan("some.package.entity")
public class MicroserviceApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceApplication.class, args);
    }

}

bootstrap.yml

server:
  port: 8090

info:
  component: Core Services

spring:
  application:
    name: microservice

application.yml

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
    healthcheck:
      enabled: true
    lease:
      duration: 5

#some other logging and jpa properties below

Eureka 服务器组件

EurekaServerApplication.java

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

bootstrap.yml

spring:
  application:
    name: discovery-server

application.yml

server:
  port: 8761

info:
  component: Discovery Server

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  instance:
    hostname: localhost
  server:
    waitTimeInMsWhenSyncEmpty: 0

API 网关组件

ZuulGatewayApplication.java

@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulGatewayApplication.class, args);
    }

    @Bean
    public ZuulGatewayPreFilter gatewayPreFilter() {
        return new ZuulGatewayPreFilter();
    }
}

bootstrap.yml

spring:
  application:
    name: api-gateway

application.yml

server:
  port: 8888

info:
  component: API Gateway

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

zuul:
  routes:
    microservice: 
      path: /microservice/**
      serviceId: microservice

【问题讨论】:

  • eureka 怎么说(仪表板和/eureka/apps?微服务注册了吗?这么短的续订和过期时间实际上可能会导致流失问题。您是否尝试过延长它们?
  • 你用的是什么版本?
  • 微服务注册得很好,在 Zuul 应用程序类中添加 @EnableEurekaClient 注释为我解决了这个问题,Zuul 现在作为注册服务显示在 eureka 仪表板上,并且还代理到微服务就好了。我没有指定任何特定版本,但基于 Spring Cloud 版本 Camden.SR4。对于续订和到期间隔,您有什么建议?

标签: spring-boot spring-cloud netflix-zuul netflix-eureka


【解决方案1】:

添加@EnableEurekaClient 解决了这个问题。 Zuul 应用程序现在可以在 Eureka 服务器中发现,并且能够路由请求。

【讨论】:

    【解决方案2】:

    错误的 spring.application.name 可能是导致此错误的原因,而不是 将@EnableEurekaClient 添加到Zuul 应用程序类中。

    【讨论】:

    • 通过这条评论解决了我的问题
    • 另一个可能的原因
    【解决方案3】:

    我的 Zuul 应用程序 @EnableZuulClient 上有正确的注释,但仍然出现该错误。

    问题是我同时启动了所有应用程序(Eureka、Zuul 和第三个应用程序),而不是一个一个地执行此操作。所以他们都需要时间来注册。一段时间后,错误消失了,我可以通过 Zuul 客户端访问我的服务。

    【讨论】:

    • 是的,这也是一种可能的场景,注册到Eureka服务发现需要35秒。但是,我不记得我读过的帖子/文档
    猜你喜欢
    • 2019-03-23
    • 2019-09-18
    • 2017-05-15
    • 1970-01-01
    • 2018-06-07
    • 2021-04-20
    • 2019-02-09
    • 2016-04-02
    • 2017-12-25
    相关资源
    最近更新 更多