gpt4 book ai didi

java - 负载平衡器没有可供客户端使用的服务器 : meeting

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:00 26 4
gpt4 key购买 nike

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

  1. nettflix.zuul.exception.ZuulException: Forwarding error
  2. Caused by: com.netflix.client.ClientException: Load balancer does not have available server for client: meeting

我来分享一下服务、eureka 和 zuul 网关的 application.yml。

Eureka 客户端: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);
}
}

我的服务类( session ): 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

我的服务类( session ): 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

任何帮助将不胜感激

最佳答案

简答

ribbon:
eureka:
enabled: false

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

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

当您启动gatekeeper 应用程序时,将应用默认的ZuulProxyAutoConfiguration 配置。它导入功能区配置类:

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

HttpClientRibbonConfiguration 反过来初始化 RibbonLoadBalancingHttpClient,它负责您看到的错误消息。

默认情况下,RibbonLoadBalancingHttpClient 使用来自 com.netflix.ribbon:ribbon-loadbalancer 包的 ZoneAwareLoadBalancer:

By default Zuul load balances using the ZoneAwareLoadBalancer from Ribbon. The algorithm is a round robin of the instances available in discovery, with availability zone success tracking for resiliency. The load balancer will keep stats for each zone and will drop a zone if the failure rates are above a configurable threshold.

If you want to use your own custom load balancer you can set the NFLoadBalancerClassName property for that Ribbon client namespace or override the getLoadBalancerClass() method in the DefaultClientChannelManager. Note that your class should extend DynamicServerListLoadBalancer.

说明了Zuul将路由和负载均衡工作委托(delegate)给了Ribbon组件,证明了你确实在gatekeeper项目中使用了Ribbon。

除非您选择不同的负载均衡器,否则您应该使用我原来的答案。
希望对您有所帮助。

关于java - 负载平衡器没有可供客户端使用的服务器 : meeting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52161774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com