Spring Cloud之网关Zuul开启重试

  在Spring Cloud体系中,如果网关要支持重试要配置的点蛮多的,等我一一道来:


  1. 网关工程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>


  2. 网关应用启动类要添加@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);
      }
    }


  3. 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值