retry与Ribbon的完美集成

核心思想:

对于ConnectTimeout与ReadTimeout这两个配置,底层代码似乎有bug,代码发现并没有对超时处理进行生效,所以我建议使用RestTemplate原始配置即可。 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:断路器的超时时间需要大于ribbon的超时时间,不然不会触发重试。(对于D版本及以前有效)

 配置:yml文件

#retry重试策略:true开启
spring.cloud.loadbalancer.retry.enabled=true
##在E版本以后 对于Ribbon的重试 就不需要设置断路器超时时间了
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
custom.requestFactory.connect-timeout=10000
custom.requestFactory.connection-request-timeout=10000
custom.requestFactory.read-timeout=2000
##针对于某一个微服务进行重试策略配置: customer-service为服务
##对所有的请求都进行重试
customer-service.ribbon.OkToRetryOnAllOperations=true
##切换实例的次数
customer-service.ribbon.MaxAutoRetriesNextServer=1
##对当前实例重试的次数
customer-service.ribbon.MaxAutoRetries=2

主入口:

@Bean
@ConfigurationProperties(prefix = "custom.requestFactory")
public HttpComponentsClientHttpRequestFactory customHttpRequestFactory(){
    return new HttpComponentsClientHttpRequestFactory();
}
package com.zx.dt2b;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.web.client.RestTemplate;

/**
 * Author:码图科技
 * Date:2018至今
 * 作为服务消费者存在
 * Description:版权所有,违者必究
 */
@EnableDiscoveryClient    //标识具体的一个服务,需要向注册中心注册
@SpringBootApplication	//SpringBoot 核心配置
@EnableRetry            //开启重试
public class ErpApplication {

   /* @Bean
    @LoadBalanced //用于实现内部的服务负载均衡机制: service-id  service-name// 如果加了这个注解 , 那么就说明 具有了服务发现的特性 负载均和的机制:意味着可以通过服务名请求数据
    public RestTemplate restTemplate(){
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpComponentsClientHttpRequestFactory.setConnectTimeout(10000);
        httpComponentsClientHttpRequestFactory.setConnectionRequestTimeout(10000);
        httpComponentsClientHttpRequestFactory.setReadTimeout(20000);
        return new RestTemplate(httpComponentsClientHttpRequestFactory);
    }*/

    @Bean
    @ConfigurationProperties(prefix = "custom.requestFactory")
    public HttpComponentsClientHttpRequestFactory customHttpRequestFactory(){
        return new HttpComponentsClientHttpRequestFactory();
    }

    @Bean
    @LoadBalanced //用于实现内部的服务负载均衡机制: service-id  service-name
    public RestTemplate restTemplate(){
        return new RestTemplate(customHttpRequestFactory());
    }
    public static void main(String[] args) {
        SpringApplication.run(ErpApplication.class, args);
    }
}

基于provider进行retry

package com.zx.dt2b.controller;

import com.zx.dt2b.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RetryController {

	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping(value="/retry")
	public String retry(){
		User user = restTemplate.getForObject("http://customer-service/customerservice/getUser?id={1}", User.class, "001");
		System.err.println("username: " + user.getName());
		return "retry success!";
	}
}

restTemplate用法占位方式

User user = restTemplate.getForObject("http://customer-service/customerservice/getUser?id={1}", User.class, "001");

注意实例切换,customer-service.ribbon.MaxAutoRetriesNextServer=1 当重试次数够了还是调用失败,将会切换实例,即切换到另外一个服务名相同的服务。获取数据

新版本不需要设置:hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

断路器时间要远大于重试时间:请求一次三秒钟,连续失败后,重试三次就是九秒,
切换实例后继续请求如果需要一秒钟,就需要断路器时间10秒钟
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

断路器的超时时间需要大于ribbon的超时时间,不然不会触发重试。(对于D版本及以前有效)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

择业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值