一:重试路由器的过滤器RetryGatewayFilter
1.1 RetryGatewayFilter 是 Spring Cloud Gateway 对请求重试提供的一个 GatewayFilter Factory
1.2 修改 application-predicate-path.yml 文件,代码如下:
server:
port: 8769
#--- #三个横线表示再创建一个配置文件
spring:
#profiles: predicate-path #配置文件名 和 spring.profiles.active 相对应
#配置程序名为eureka-gateway-client
application:
name: eureka-gateway-client
cloud:
#设置路由规则
gateway:
discovery:
locator:
#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。
#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能
enabled: true
##表示将请求路径的服务名配置改成小写 因为服务注册的时候,向注册中心注册时将服务名转成大写的了
lower-case-service-id: true
routes:
#我们自定义的路由 ID,保持唯一性
- id: predicate_path
#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发
uri: lb://eureka-client
#uri: http://localhost:8762
#断言
predicates:
#表示将以/HiController开头的请求转发到uri为lb://eureka-client的地址上
#转发地址格式为 uri/HiController/**
- Path=/HiController/**
filters:
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY
# Retry GatewayFilter 通过这四个参数来控制重试机制: retries, statuses, methods, 和 series。
# retries:重试次数,默认值是 3 次
# statuses:HTTP 的状态返回码,取值请参考:org.springframework.http.HttpStatus
# methods:指定哪些方法的请求需要进行重试逻辑,默认值是 GET 方法,取值参考:org.springframework.http.HttpMethod
# series:一些列的状态码配置,取值参考:org.springframework.http.HttpStatus.Series。
# 符合的某段状态码才会进行重试逻辑,默认值是 SERVER_ERROR,值是 5,也就是 5XX(5 开头的状态码),共有5 个值。
logging:
level:
org.springframework.cloud.gateway: debug
eureka:
client:
#服务注册地址
serviceUrl:
#注意: Eureka Server 的注册地址
#将服务提供者注册到三个Eureka Server中去
#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
#defaultZone: http://peer1:8001/eureka/
defaultZone: http://localhost:8761/eureka/
增加了这部分的代码
- Retry GatewayFilter 通过这四个参数来控制重试机制: retries, statuses, methods, 和 series。
- retries:重试次数,默认值是 3 次
- statuses:HTTP 的状态返回码,取值请参考org.springframework.http.HttpStatus
- methods:指定哪些方法的请求需要进行重试逻辑,默认值是 GET 方法,取值参考:org.springframework.http.HttpMethod
- series:一些列的状态码配置,取值参考:org.springframework.http.HttpStatus.Series。符合的某段状态码才会进行重试逻辑,默认值是 SERVER_ERROR,值是 5,也就是 5XX(5 开头的状态码),共有5 个值。