当实例关闭并转发到其他可用实例

时间:2017-03-07 14:56:39

标签: spring-cloud microservices netflix-zuul spring-cloud-netflix spring-cloud-feign

使用'Camden.SR5'进行spring-cloud-dependencies,使用spring boot'1.5.2.RELEASE'。

在我目前的设置中,我有

  • eureka服务器
  • 配置服务器(在随机端口上运行)
  • zuul网关服务器
  • 和2个服务实例(在随机端口上运行)

所有这些实例都成功注册了Eureka。

enter image description here

当所有服务都在运行时,负载均衡通过zuul正确完成,没有任何问题。

当一个实例被杀死时,Zuul仍在尝试使用相同的服务来完成请求。但是,如果在关闭实例后等到eureka注册表被提取,请求将与其他“UP”实例一起完成。

    2017-03-07 19:57:41.409 DEBUG 26658 --- [nio-5555-exec-3] c.n.l.reactive.LoadBalancerCommand       : Got error org.apache.http.conn.HttpHostConnectException: Connect to 10.99.4.151:64381 [/10.99.4.151] failed: Connection refused when executed on server 10.99.4.151:64381
2017-03-07 19:57:41.420 DEBUG 26658 --- [nio-5555-exec-3] com.netflix.hystrix.AbstractCommand      : Error executing HystrixCommand.run(). Proceeding to fallback logic ...

com.netflix.client.ClientException: null
    at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:123) ~[ribbon-loadbalancer-2.2.0.jar:2.2.0]
    at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:81) ~[ribbon-loadbalancer-2.2.0.jar:2.2.0]
    at org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand.run(AbstractRibbonCommand.java:96) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand.run(AbstractRibbonCommand.java:42) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75) ~[httpclient-4.5.3.jar:4.5.3]
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142) ~[httpclient-4.5.3.jar:4.5.3]
    ... 162 common frames omitted

2017-03-07 19:57:41.425 DEBUG 26658 --- [nio-5555-exec-3] com.netflix.hystrix.AbstractCommand      : No fallback for HystrixCommand. 

java.lang.UnsupportedOperationException: No fallback available.
    at com.netflix.hystrix.HystrixCommand.getFallback(HystrixCommand.java:292) [hystrix-core-1.5.6.jar:1.5.6]
    at org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand.getFallback(AbstractRibbonCommand.java:117) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand.getFallback(AbstractRibbonCommand.java:42) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_66]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66]

2017-03-07 19:57:41.428  WARN 26658 --- [nio-5555-exec-3] o.s.c.n.z.filters.post.SendErrorFilter   : Error during filtering

com.netflix.zuul.exception.ZuulException: Forwarding error
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.handleException(RibbonRoutingFilter.java:170) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.forward(RibbonRoutingFilter.java:145) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter.run(RibbonRoutingFilter.java:88) ~[spring-cloud-netflix-core-1.2.5.RELEASE.jar:1.2.5.RELEASE]

以下是与@EnableZuulProxy和@EnableEurekaClient一起使用的zuul配置

    server:
  port: 5555

spring:
  application:
    name: gateway-server
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIGSERVER
      fail-fast: true
      retry:
        multiplier:  1.1
        initial-interval: 1000
        max-attempts: 6
        max-interval: 2000

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 100000
        timeout:
          enabled: false

ribbon:
  ReadTimeout: 5000
  ConnectTimeout: 3000
  maxAutoRetries: 1
  MaxAutoRetriesNextServer: 2
  OkToRetryOnAllOperations: true


logging:
  level:
    ROOT: DEBUG

zuul:
  routes:
    security-service:
      retryable: true

2个服务实例正在运行唯一的instance-ids

@EnableEurekaClient
@EnableHystrix
@SpringBootApplication
public class SecurityServer implements HealthIndicator{

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

    @Override
    public Health health() {
        return Health.up().withDetail("STATUS", "SUCCESS").build();
    }
}
instanceId: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${random.uuid}}

你能帮我解决这个问题吗?实例配置,以便在实例关闭时自动将请求转发到其他可用实例。

2 个答案:

答案 0 :(得分:4)

在搜索更多信息并查看spring-cloud-netflix问题跟踪器时,william-tranryanjbaxter之间就最佳做法进行了精彩的讨论。谢谢你们两个。

https://github.com/spring-cloud/spring-cloud-netflix/issues/1290#issuecomment-242204614

https://github.com/spring-cloud/spring-cloud-netflix/issues/1295

总之,Camden没有使用Ribbon HTTP Client(不建议使用),因此没有任何ribbon。*属性可以帮助您控制重试逻辑。 Camden使用Apache HTTP客户端。

所以解决方案是使用下面的配置

在camden版本中使用Ribbon HTTP Client
ribbon.restclient.enabled=true

移至Camden.BUILD-SNAPSHOT或Dalston.BUILD-SNAPSHOT以使用弹簧重试(https://github.com/spring-projects/spring-retry

答案 1 :(得分:0)

关于Brixton和Camden发布中针对此问题的修复,here Ryan Baxter是一篇非常好的文章。