Spring 云:Zuul 投掷 "Load balancer does not have available server for client"

Spring Cloud : Zuul throwing "Load balancer does not have available server for client"

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

Load balancer does not have available server for client: microservice

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

下面是每个组件的 Spring 启动 类。

微服务组件

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

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

错误 spring.application.name 可能是此错误的其他原因 将 @EnableEurekaClient 添加到 Zuul 应用程序 class.

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

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