• ribbon的重试机制


    1.在调用端的pom.xml加入重试jar

     <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
      </dependency>

    2.修改yml文件

    spring:
      cloud:
        loadbalancer:
          retry:
            enabled: true # 开启Spring Cloud的重试功能
    service-product: #调用的服务名称
      ribbon:
        ConnectTimeout: 250 # Ribbon的连接超时时间
        ReadTimeout: 1000 # Ribbon的数据读取超时时间
        OkToRetryOnAllOperations: true # 是否对所有操作都进行重试
        MaxAutoRetriesNextServer: 1 # 切换实例的重试次数
        MaxAutoRetries: 1 # 对当前实例的重试次数
    logging:
      level:
        root: debug

    3.关闭一个服务,开始调用,然后查看日志

    2020-02-05 15:31:29.914 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.client.RestTemplate              : HTTP GET http://service-product/product/1
    2020-02-05 15:31:29.914 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.client.RestTemplate              : Accept=[application/json, application/*+json]
    2020-02-05 15:31:29.914 DEBUG 24644 --- [nio-9012-exec-4] c.n.loadbalancer.ZoneAwareLoadBalancer   : Zone aware logic disabled or there is only one zone
    2020-02-05 15:31:29.915 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate          : Retry: count=0
    2020-02-05 15:31:29.915 DEBUG 24644 --- [nio-9012-exec-4] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@357d23b55 pairs: {GET /product/1 HTTP/1.1: null}{Accept: application/json, application/*+json}{User-Agent: Java/1.8.0_121}{Host: 10.9.9.139:9011}{Connection: keep-alive}
    2020-02-05 15:31:31.920 DEBUG 24644 --- [nio-9012-exec-4] o.s.c.n.r.RibbonLoadBalancedRetryPolicy  : 10.9.9.139:9011 RetryCount: 1 Successive Failures: 2 CircuitBreakerTripped:false
    2020-02-05 15:31:31.920 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
    2020-02-05 15:31:31.920 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate          : Retry: count=1
    2020-02-05 15:31:33.923 DEBUG 24644 --- [nio-9012-exec-4] o.s.c.n.r.RibbonLoadBalancedRetryPolicy  : 10.9.9.139:9011 RetryCount: 2 Successive Failures: 4 CircuitBreakerTripped:true
    2020-02-05 15:31:33.923 DEBUG 24644 --- [nio-9012-exec-4] c.n.loadbalancer.ZoneAwareLoadBalancer   : Zone aware logic disabled or there is only one zone
    2020-02-05 15:31:33.923 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=2
    2020-02-05 15:31:33.923 DEBUG 24644 --- [nio-9012-exec-4] o.s.retry.support.RetryTemplate          : Retry: count=2
    2020-02-05 15:31:33.924 DEBUG 24644 --- [nio-9012-exec-4] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@4145dbbd5 pairs: {GET /product/1 HTTP/1.1: null}{Accept: application/json, application/*+json}{User-Agent: Java/1.8.0_121}{Host: 10.9.9.139:9001}{Connection: keep-alive}
    2020-02-05 15:31:33.932 DEBUG 24644 --- [nio-9012-exec-4] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@1df928ab6 pairs: {null: HTTP/1.1 200}{Content-Type: application/json;charset=UTF-8}{Transfer-Encoding: chunked}{Date: Wed, 05 Feb 2020 07:31:33 GMT}{Keep-Alive: timeout=60}{Connection: keep-alive}
    2020-02-05 15:31:33.932 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.client.RestTemplate              : Response 200 OK
    2020-02-05 15:31:33.932 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.client.RestTemplate              : Reading to [com.topcheer.order.entity.Product]
    2020-02-05 15:31:33.933 DEBUG 24644 --- [nio-9012-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]
    2020-02-05 15:31:33.933 DEBUG 24644 --- [nio-9012-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Writing [Product(id=1, productName=访问的服务地址:10.9.9.139:9001, status=1, price=1000.00, productDesc=抗曹, caption= (truncated)...]
    2020-02-05 15:31:33.934 DEBUG 24644 --- [nio-9012-exec-4] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
    2020-02-05 15:31:33.934 DEBUG 24644 --- [nio-9012-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

    发现在尝试一次以后,又切换到9001了,不过ribbon用的不多,用的都是feign。

  • 相关阅读:
    Matlab图像处理函数:regionprops
    Java的 volatile关键字的底层实现原理
    SQL学习(一.索引)
    solr7.7.0搜索引擎使用(四)(搜索语法)
    solr7.7.0搜索引擎使用(三)(添加文件索引)
    singleton单例模式小结
    多态小结
    quartz定时任务时间设置
    EditPlus编译运行java文件
    NoClassDefFoundError错误发生的原因
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12264051.html
Copyright © 2020-2023  润新知