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

标签 java spring spring-boot spring-cloud netflix-zuul

我目前正在尝试在 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 只是协议(protocol)、上下文路径和路径的其余部分)。

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

https://servicea.domain/servicea

在此示例中,URL 是负载均衡器的 CNAME。第二种配置是按如下方式路由

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

正在生成的 URL:

https:/servicea/v1/someapi

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

此配置是否缺少某些内容? 我目前没有在应用程序中的其他任何地方配置 Spring Cloud(除了在主类中提供 @EnableZuulProxy 和 @EnableRetry 注释之外)。

最佳答案

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

关于java - 在 Spring Cloud 应用程序中实现重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56800180/

相关文章:

java - 在 android 中膨胀对话框 fragment 时出现未知类异常

java - 如何添加 Eclipse Gradle 任务选项卡?

java - 为什么/如何返回 "locally instantiated array"在 Java 中工作?

java - 如何处理从Spring服务器调用的长异步存储过程?

spring-boot - JDK11 + Spring 启动 = JAXBException : Implementation of JAXB-API has not been found on module path or classpath

java - 如何配置 Spring Boot、Spring JPA、Spring Test 和 Hibernate 来创建、使用和删除给定的 PostgreSQL 架构?

java - Box2D Libgdx黑屏

java - 在 Jhipster 中添加新服务时出现代码错误 500

java - spring @Controller 和 @RestController 注解的区别

java - 创建类路径资源中定义的名为 'entityManagerFactory' 的 bean 时出错 - SO 的解决方案不起作用