在Spring Cloud体系中,如果网关要支持重试要配置的点蛮多的,等我一一道来:
- 网关工程pom.xml中要加入org.springframework.retry依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> </dependencies> - 网关应用启动类要添加@EnableRetry注解
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.retry.annotation.EnableRetry; @SpringBootApplication @EnableZuulProxy @EnableEurekaClient @EnableRetry public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } } - application.yml配置
server: port: 8060 spring: application: name: microservice-gateway-zuul #打开负载局衡器支持重试开关 cloud: loadbalancer: retry: enabled: true eureka: client: service-url: defaultZone: http://peer1:8761/eureka/ instance: prefer-ip-address: true logging: config: classpath:logback-spring.xml #打开网关支持重试开关 zuul: retryable: true #要支持重试的微服务的配置 microservice-provider-user: ribbon: ConnectionTimeout: 3000 ReadTimeout: 6000 #打开所有操作都支持重试的开关 OkToRetryOnAllOperations: true #在其他Server上重试的次数(除去首次) MaxAutoRetriesNextServer: 3 #在出错或者超时的Server上重试的次数(除去首次) MaxAutoRetries: 0 #网关等待微服务处理请求的超时时间 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
首先要配置 spring.cloud.loadbalancer.retry.enable = true
再次要配置zuul.retryable = true
最后针对一个要支持重试的微服务,本例中服务名称为microservice-provider-user

本文详细介绍了如何在SpringCloud网关中配置重试功能,包括添加依赖、启用重试注解、配置YAML文件等内容,适用于需要增强微服务调用可靠性的场景。
1121

被折叠的 条评论
为什么被折叠?



