spring - 当服务器关闭时负载平衡失败

标签 spring load-balancing netflix-zuul netflix-ribbon

我编写了一组简单的微服务,其架构如下:enter image description here

对于所有内容,我添加了 spring-boot-starter-actuator 以添加 /health 端点。

在 Zuul/Ribbon 配置中我添加了:

zuul:
  ignoredServices: "*"
  routes:
    home-service:
      path: /service/**
      serviceId: home-service
      retryable: true

home-service:
  ribbon:
    listOfServers: localhost:8080,localhost:8081
    eureka.enabled: false
    ServerListRefreshInterval: 1

这样,每次客户端调用GET http://localhost:7070/service/home时,负载均衡器都会选择在 8080 或 8081 端口上运行的两个 HomeService 之一,并调用其端点/home

但是,当其中一个 HomeService 关闭时,负载均衡器似乎并不知道(尽管有 ServerListRefreshInterval 配置),并且如果发生以下情况,将会失败并显示 error=500尝试调用关闭实例。

我该如何解决这个问题?

最佳答案

我已收到并测试了来自 spring-cloud team 的解决方案.

解决方案是 here in github

总结一下:

  • 我已将 org.springframework.retry.spring-retry 添加到我的 zuul 类路径
  • 我已将 @EnableRetry 添加到我的 zuul 应用程序
  • 我已将以下属性放入我的 zuul 配置中

application.yml

server:
  port: ${PORT:7070}

spring:
  application:
    name: gateway

endpoints:
  health:
    enabled: true
    sensitive: true
  restart:
    enabled: true
  shutdown:
    enabled: true

zuul:
  ignoredServices: "*"
  routes:
    home-service:
      path: /service/**
      serviceId: home-service
      retryable: true
  retryable: true

home-service:
  ribbon:
    listOfServers: localhost:8080,localhost:8081
    eureka.enabled: false
    ServerListRefreshInterval: 100
    retryableStatusCodes: 500
    MaxAutoRetries: 2
    MaxAutoRetriesNextServer: 1
    OkToRetryOnAllOperations: true
    ReadTimeout: 10000
    ConnectTimeout: 10000
    EnablePrimeConnections: true

ribbon:
  eureka:
    enabled: false

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000

关于spring - 当服务器关闭时负载平衡失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168155/

相关文章:

java - 我总是得到 "{"状态“:504 ,"error" :"Gateway Timeout" ,"message" :"com.netflix.zuul.exception.ZuulException: Hystrix Readed time out"}"?

java - Zuul 过滤器复制流量

java - 如何让Spring访问下拉框值

java - 通过 jquery load/ajax 重新加载 js 脚本

java - log4j:WARN org.apache.log4j.ConsoleAppender 中没有这样的属性 [append]

java - 制作 Spring Bean 列表

c# - 在负载平衡环境中将 javascript/css 请求定向到与页面相同的服务器

netflix-zuul - ZUUL 向下游应用程序发送基本身份验证

amazon-ec2 - 我需要多少实例?

apache - 如何使用 Apache 对 Marklogic 集群进行负载平衡