负载平衡器没有可用于客户端的服务器:会议

当我尝试通过 Zuul 网关访问服务时,Zuul 无法将请求转发到相应的服务。以下错误是我面临的:meeting

  1. nettflix.zuul.exception.ZuulException: Forwarding error
  2. 由以下原因导致:com.netflix.client.ClientException: Load Balancer 没有可用于客户端的服务器:会议

让我分享一下服务的 application.yml,eureka 和 zuul gateway。

尤里卡客户: Application.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 300
  client:
    register-with-eureka: false
    fetch-registry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

ZuulGateWay: application.yml

server:
  port: 8085

spring:
  application:
    name: gatekeeper


zuul:
  routes:
    meeting: /meeting/**
    serviceId: meeting

ribbon:
  eureka:
    enabled: false

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

ZuulGateWay: SpringBootApplication

package com.sagarp.gatekeeper;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class MeetingApplication {

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

我的服务类(会议): Application.yml

server:
  port: 0
spring:
  application:
    name: meeting
  datasource:
    url: jdbc:mysql://localhost:3306/sagarp?useSSL=false
    username: myUserName
    password: myPassWord
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    hibernate:
     ddl-auto: update

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    lease-renewal-interval-in-seconds: 5

我的服务类(会议): SpringBootApplication

package com.sagarp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class MeetingApplication {

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

如您所见,该配置可确保 eureka 客户端发现我的所有服务。

在eureka控制台中,我已经验证了相同的内容,并且两者都可见。zuul gatewaymy service(meeting)

为了更好的视图,你可以访问我的git存储库。https://github.com/sagar-patro/demo-microservices

任何帮助都会非常明显


答案 1

简短的回答

ribbon:
  eureka:
    enabled: false

Spring Cloud Netflix Zuul使用Netflix的Ribbon来执行客户端负载均衡,默认情况下,Ribbon将使用Netflix Eureka进行服务发现。您正在跳过服务发现,因此已设置为 。由于 Ribbon 现在无法使用 Eureka 查找服务,因此必须为服务指定一个 url:ribbon.eureka.enabledfalsemeeting

meeting:
  ribbon:
    listOfServers: localhost:8080

扩展的答案

我会给你更清楚的。

您当前在项目中使用的依赖项 org.springframework.cloud:spring-cloud-starter-netflix-zuul 有几个编译依赖项:gatekeeper

com.netflix.zuul:zuul-core
org.springframework.boot:spring-boot-starter-web        
org.springframework.boot:spring-boot-starter-actuator       
org.springframework.cloud:spring-cloud-netflix-zuul
org.springframework.cloud:spring-cloud-starter      
org.springframework.cloud:pring-cloud-starter-netflix-hystrix
org.springframework.cloud:spring-cloud-starter-netflix-ribbon
org.springframework.cloud:spring-cloud-starter-netflix-archaius

如您所见,它构成了围绕com.netflix.zuul:zuul-core模块收集的许多组件(包括Eureka,例如发现和用于路由的Ribbon):

enter image description here

启动应用程序时,将应用默认配置。它导入功能区配置类:gatekeeperZuulProxyAutoConfiguration

@Configuration
@Import({ RibbonCommandFactoryConfiguration.RestClientRibbonConfiguration.class,
        RibbonCommandFactoryConfiguration.OkHttpRibbonConfiguration.class,
        RibbonCommandFactoryConfiguration.HttpClientRibbonConfiguration.class,
        HttpClientConfiguration.class })
@ConditionalOnBean(ZuulProxyMarkerConfiguration.Marker.class)
public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration { ... }

HttpClientRibbonConfiguration,反过来,初始化,这是您看到的错误消息的原因。RibbonLoadBalancingHttpClient

默认情况下,它使用来自包的内容:RibbonLoadBalancingHttpClientZoneAwareLoadBalancercom.netflix.ribbon:ribbon-loadbalancer

默认情况下,Zuul 使用功能区中的 ZoneAwareLoadBalancer 进行负载平衡。该算法是发现中可用实例的轮循机制,具有可用性区域成功跟踪以实现复原能力。负载均衡器将保留每个区域的统计信息,如果故障率高于可配置的阈值,则会删除一个区域。

如果要使用自己的自定义负载平衡器,可以为该功能区客户端命名空间设置属性或重写 中的方法。请注意,您的类应扩展 。NFLoadBalancerClassNamegetLoadBalancerClass()DefaultClientChannelManagerDynamicServerListLoadBalancer

它解释了 Zuul 将路由和负载平衡工作委托给功能区组件,并证明您实际上在项目中使用功能区。gatekeeper

除非您选择其他负载均衡器,否则您应该选择我的原始答案。
我希望它会有所帮助。


答案 2

推荐