java - 在 Spring Cloud 应用程序中实现重试
问题描述
我目前正在尝试在 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 Cloud 应用程序的路径:
/servicea/v1/someeapi
正在生成的网址:
https:/servicea/v1/someapi
如您所见,应用程序正在从导致请求失败的 URL 中删除主机和域。
我错过了这个配置的东西吗?我目前没有在应用程序的其他任何地方配置 Spring Cloud(除了在主类中 提供@EnableZuulProxy
和注释)。@EnableRetry
解决方案
重试失败的请求 Spring Cloud Netflix 提供了多种方式来发出 HTTP 请求。您可以使用负载平衡的 RestTemplate、Ribbon 或 Feign。无论您选择如何创建 HTTP 请求,请求总是有可能失败。当请求失败时,您可能希望自动重试请求。要在使用 Sping Cloud Netflix 时这样做,您需要在应用程序的类路径中包含 Spring Retry。当 Spring Retry 存在时,负载平衡的 RestTemplates、Feign 和 Zuul 会自动重试任何失败的请求(假设您的配置允许这样做)。
此处的文档:春季云重试
推荐阅读
- matlab - 如何保存在 MATLAB simulink 操作中间生成的值
- python - 熊猫阻止我下载我不想拥有的文件
- android - 异常:视图没有设置 navController
- c - 未找到 C 结构类型,包括库,错误“未知类型名称”
- android - LocationAwareException:任务“:app:processDebugManifest”执行失败
- java - 为对象创建定义的值
- go - 如果查询限制很大,goroutines 中的 GORM 会冻结
- java - Java - 扫描仪读取没有赋值的字符串(已解决)
- r - 表频率和直方图之间的差异... geom_histogram 向下取整?
- html - 数组的每个值的垫输入