ribbon的超时设置,只能按转发的serviceId来分的,无法像nginx那样直接在每个转发的链接里头设置超时时间。这里hack一下,实现url基本的ribbon超时时间设置。具体的思路就是重写RibbonApacheHttpRequest的toRequest方法,然后进行设置。

CustomRibbonApacheHttpRequest

public class CustomRibbonApacheHttpRequest extends RibbonApacheHttpRequest {
public CustomRibbonApacheHttpRequest(RibbonCommandContext context) {
super(context);
} @Override
public HttpUriRequest toRequest(RequestConfig requestConfig) {
final RequestBuilder builder = RequestBuilder.create(this.context.getMethod());
builder.setUri(this.uri);
for (final String name : this.context.getHeaders().keySet()) {
final List<String> values = this.context.getHeaders().get(name);
for (final String value : values) {
builder.addHeader(name, value);
}
} for (final String name : this.context.getParams().keySet()) {
final List<String> values = this.context.getParams().get(name);
for (final String value : values) {
builder.addParameter(name, value);
}
} if (this.context.getRequestEntity() != null) {
final BasicHttpEntity entity;
entity = new BasicHttpEntity();
entity.setContent(this.context.getRequestEntity());
// if the entity contentLength isn't set, transfer-encoding will be set
// to chunked in org.apache.http.protocol.RequestContent. See gh-1042
if (this.context.getContentLength() != null) {
entity.setContentLength(this.context.getContentLength());
} else if ("GET".equals(this.context.getMethod())) {
entity.setContentLength();
}
builder.setEntity(entity);
} customize(this.context.getRequestCustomizers(), builder); //todo 这里处理个性的timeout信息
if(uri.getPath().equals("/review/timeout")){
RequestConfig.Builder configBuilder = RequestConfig.copy(builder.getConfig());
configBuilder.setConnectionRequestTimeout(*);
configBuilder.setConnectTimeout(*);
configBuilder.setSocketTimeout(*);
builder.setConfig(configBuilder.build());
}else{
builder.setConfig(requestConfig);
} return builder.build();
}
}

CustomHttpClientRibbonCommand

public class CustomHttpClientRibbonCommand extends HttpClientRibbonCommand {

    public CustomHttpClientRibbonCommand(String commandKey, RibbonLoadBalancingHttpClient client, RibbonCommandContext context, 
ZuulProperties zuulProperties) {
super(commandKey, client, context, zuulProperties);
} public CustomHttpClientRibbonCommand(String commandKey, RibbonLoadBalancingHttpClient client, RibbonCommandContext context,
ZuulProperties zuulProperties,
ZuulFallbackProvider zuulFallbackProvider) {
super(commandKey, client, context, zuulProperties, zuulFallbackProvider);
} @Override
protected RibbonApacheHttpRequest createRequest() throws Exception {
RibbonApacheHttpRequest ribbonApacheHttpRequest = new CustomRibbonApacheHttpRequest(this.context);
return ribbonApacheHttpRequest;
}
}

CustomHttpClientRibbonCommandFactory

public CustomHttpClientRibbonCommandFactory(SpringClientFactory clientFactory, ZuulProperties zuulProperties) {
super(clientFactory, zuulProperties);
this.clientFactory = clientFactory;
this.zuulProperties = zuulProperties;
} public CustomHttpClientRibbonCommandFactory(SpringClientFactory clientFactory, ZuulProperties zuulProperties,
Set<ZuulFallbackProvider> fallbackProviders) {
super(clientFactory, zuulProperties, fallbackProviders);
this.clientFactory = clientFactory;
this.zuulProperties = zuulProperties;
} @Override
public HttpClientRibbonCommand create(RibbonCommandContext context) {
ZuulFallbackProvider zuulFallbackProvider = getFallbackProvider(context.getServiceId());
final String serviceId = context.getServiceId();
final RibbonLoadBalancingHttpClient client = this.clientFactory.getClient(
serviceId, RibbonLoadBalancingHttpClient.class);
client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId)); return new CustomHttpClientRibbonCommand(serviceId, client, context, zuulProperties, zuulFallbackProvider);
}
}

配置

@Autowired(required = false)
private Set<ZuulFallbackProvider> zuulFallbackProviders = Collections.emptySet(); @Bean
@ConditionalOnMissingBean
public RibbonCommandFactory<?> ribbonCommandFactory(
SpringClientFactory clientFactory, ZuulProperties zuulProperties) {
return new CustomHttpClientRibbonCommandFactory(clientFactory, zuulProperties, zuulFallbackProviders);
}

网关的超时层级

zuul

zuul:
max:
host:
connections: 500
host:
socket-timeout-millis: 60000
connect-timeout-millis: 60000

ribbon

ribbon:
ReadTimeout: 10000
ConnectTimeout: 10000
MaxAutoRetries: 0
MaxAutoRetriesNextServer: 1
eureka:
enabled: true

hystrix

hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 60000

小结

走网关的话,有三层的超时时间,一个是zuul的,一个是ribbon的,还有一个是hystrix的。hystrix的超时设置,AbstractRibbonCommand这个类没有暴露设置hystrix的Setter方法出来,所以无法通过继承的方式来扩展。因此要自定义ribbon超时的话,需要这个超时时间是小于hystrix的,不然就提前被hystrix超时了,无法起到效果。

ribbon设置url级别的超时时间的更多相关文章

  1. 【转载】Extjs设置Ajax请求的超时时间timeout

    在Extjs中的Ajax请求中,Ext.Ajax.request 默认超时时间是30秒,有时候我们有比较耗时的操作需要设置更长时间,此时我们就需要修改Ext.Ajax.Requset的超时时间为更长, ...

  2. Selenium 设置管理cookie,超时时间

    可以通过option设置管理cookie,超时时间 一.cookie机制和session机制的区别 具体来说cookie机制采用的是在客户端保持状态的方案,储存在内存.而session机制采用的是在服 ...

  3. 设置mysql 事务锁超时时间 innodb_lock_wait_timeout

    Mysql数据库采用InnoDB模式,默认参数:innodb_lock_wait_timeout设置锁等待的时间是50s,一旦数据库锁超过这个时间就会报错. mysql> SHOW GLOBAL ...

  4. Java中设置方法执行的超时时间java.util.concurrent.Future

    java.util.concurrent.Future Future代表一个异步计算的结果.它提供了方法来检查是否计算已经完成,还是正在计算而处于等待状态,并且也提供了获取计算结果 方法.当计算完成后 ...

  5. 怎么设置session无响应超时时间并且自动返回登陆页面

    ------解决方案--------------------看下我的 session.setAttribute(UserInfo.USERINFO, userinfo); session.setMax ...

  6. spring cloud各种超时时间设置

    如果是zuul(网关)的超时时间需要设置zuul.hystrix.ribbon等三部分: #zuul超时设置#默认1000zuul.host.socket-timeout-millis=2000#默认 ...

  7. Go基础系列:为select设置超时时间

    Go channel系列: channel入门 为select设置超时时间 nil channel用法示例 双层channel用法示例 指定goroutine的执行顺序 After() 谁也无法保证某 ...

  8. unigui session超时时间设置

    unigui session超时时间设置 默认的SESSION超时时间是10分钟. 网络 SOCKET 程序,像 数据库,中间件,UNIGUI等...为了防止过多的僵死连接卡死服务端,服务端都会主动踢 ...

  9. 设置linux中tcp默认的20秒connect超时时间(转)

    无论你用任何语言或者是网络库,你都可以设置网络操作的超时时间,特别是connect.read.write的超时时间. 你可以在代码中把超时时间设置任意大小值,但是connect方法会有一点特殊. co ...

随机推荐

  1. 判断滚动条滚动到document底部

    滚动条没有实际的高度.只是为了呈现效果才在外型上面有长度. 在js当中也没有提供滚动条的高度API. 参考了网上有关资料:判断滚动条到底部的基本逻辑是滚动条滚动的高度加上视口的高度,正好是docume ...

  2. SQLite - Python

    SQLite - Python 安装 SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 2 ...

  3. 【Linux】- mv命令

    Linux mv命令用来为文件或目录改名.或将文件或目录移入其它位置. 语法 mv [options] source dest mv [options] source... directory 参数说 ...

  4. C语言宏中"#"和"##"的用法

    转自:https://www.cnblogs.com/hnrainll/archive/2012/08/15/2640558.html 在查看linux内核源码的过程中,遇到了许多宏,这里面有许多都涉 ...

  5. Winform 数据绑定

    1.DataGridView数据绑定 namespace WindowsFormsApplication1 { public partial class Form1 : Form { private ...

  6. Python2爬虫获取的数据存储到MySQL中时报错"Incorrect string value: '\\xE6\\x96\\xB0\\xE9\\x97\\xBB' for column 'new' at row 1"的解决办法

    由于一直使用python3进行编码,在使用Python2时,将爬虫数据连接数据库进行存储时,出现如上的报错,经查资料 是数据库编码问题. 如下转自:http://www.cnblogs.com/liu ...

  7. winform全局异常捕获

    /// <summary> /// 应用程序的主入口点. /// </summary> public static ApplicationContext context; [S ...

  8. JavaScript 面向对象开发知识基础总结

    JavaScript 面向对象开发知识基础总结 最近看了两本书,书中有些内容对自己还是很新的,有些内容是之前自己理解不够深的,所以拿出来总结一下,这两本书的名字如下: JavaScript 面向对象精 ...

  9. Mac将应用拖入Finder工具栏

    在Finder的工具栏上放一下应用,方便打开对应的文件,可以 Command + 鼠标拖动应用,将应用拖入Finder工具栏中. 本人的Finder工具栏上添加了vscode这个应用

  10. C++语言中数组指针和指针数组彻底分析

    #################################                              ##       基本知识               ##        ...