有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Spring云应用程序中实现重试

我目前正在尝试在Zuul代理应用程序中实现重试功能,该应用程序目前直接在路由配置下提供URL。当您直接在路由下指定URL时(如下面的示例所示),是否可以实现重试功能

zuul:
  prefix: /api
  sensitive-headers: Cookie,Set-Cookie
  routes:
    servicea:
      path: /servicea
      stripPrefix: true
      url: ${servicea.url}
    serviceb:
      path: /serviceab
      stripPrefix: true
      url: ${serviceb.url}

ribbon:
  ReadTimeout: 60000

该应用程序指向由负载均衡器(ALB)前置的外部应用程序,因此在这种情况下不需要客户端负载平衡和服务发现

应用程序正在对Zuul使用以下依赖项:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  <version>2.1.2.RELEASE</version>
</dependency>

假设这是不可能的(文档似乎指出了这一点),我希望在理解如何配置应用程序以启用重试时得到一些帮助。根据我读到的内容,以下配置应该可以工作:

zuul:
  prefix: /api
  sensitive-headers: Cookie,Set-Cookie
  routes:
    servicea:
      path: /servicea
      stripPrefix: true
      retryable: true
      serviceId: servicea
    serviceb:
      path: /serviceab
      stripPrefix: true
      retryable: true
      serviceId: serviceb

 servicea:
   ribbon:
     ReadTimeout: 10000
     ConnectTimeout: 10000
     NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
     listOfServers: ${servicea.url}
     stripPrefix: true
     MaxAutoRetries: 1
     OkToRetryOnAllOperations: true

 serviceb:
   ribbon:
     ReadTimeout: 10000
     ConnectTimeout: 10000
     NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
     listOfServers: ${serviceb.url}
     stripPrefix: true
     MaxAutoRetries: 1
     OkToRetryOnAllOperations: true

 ribbon:
   IsSecure: true
   eureka:
     enabled: false
   ReadTimeout: 60000

当我试图以这种方式实现它时,我遇到了一个问题,即应用程序不包括listOfServers属性中指定的主机名。HTTP请求显然因此失败(URL只是协议、上下文路径和路径的其余部分)

配置中的URL在启动期间被注入PropertySource。其中一个URL如下所示

https://servicea.domain/servicea

在本例中,URL是负载平衡器的CNAME。第二种配置是按以下方式进行路由

Spring云应用程序的路径: /servicea/v1/someeapi

正在生成的URL:

https:/servicea/v1/someapi

如您所见,应用程序正在从URL中删除主机和域,这导致请求失败

这个配置有什么遗漏吗? 我目前没有在应用程序的任何其他地方配置Spring Cloud(除了在主类中提供@EnableZuulProxy@EnableRetry注释)


共 (1) 个答案

  1. # 1 楼答案

    Retrying Failed Requests Spring Cloud Netflix offers a variety of ways to make HTTP requests. You can use a load balanced RestTemplate, Ribbon, or Feign. No matter how you choose to create your HTTP requests, there is always a chance that a request may fail. When a request fails, you may want to have the request be retried automatically. To do so when using Sping Cloud Netflix, you need to include Spring Retry on your application’s classpath. When Spring Retry is present, load-balanced RestTemplates, Feign, and Zuul automatically retry any failed requests (assuming your configuration allows doing so).

    这里的文件:spring cloud retry