add: feign支持全局connect和read的timeout设置

This commit is contained in:
Wendal Chen 2018-01-19 23:14:47 +08:00
parent cb04735ec0
commit 07dd726c2e
3 changed files with 26 additions and 1 deletions

View File

@ -7,6 +7,7 @@
* add: starter-apollo-client 对接apollo配置中心. apollo是携程框架部门研发的分布式配置中心
* add: starter-config-client NB配置中心的客户端,其服务端可以是任意支持Restful的服务器
* add: feign支持从ioc容器获取client/encoder/decoder,且自定义JsonFormat
* add: feign支持全局connect和read的timeout设置
* add: starter-eureka-server新版的status页面 by [温泉](https://github.com/ywjno)
* update: 更新HikariCP到2.7.5
* update: 更新sharding-jdbc到2.0.2,终于支持建表语句了,所以dao.create也能工作了!

View File

@ -38,6 +38,7 @@ import feign.Client;
import feign.Feign;
import feign.Logger;
import feign.Logger.Level;
import feign.Request.Options;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.gson.GsonDecoder;
@ -85,10 +86,16 @@ public class FeignStarter implements IocEventListener {
@PropDoc(value = "默认负载均衡的规则", defaultValue = "availability", possible = {"availability", "random"})
public static final String PROP_LB_RULE = PRE + "loadbalancer.rule";
@PropDoc(value = "JsonFormat", possible = {"full", "forLook", "compact", "nice", "tidy", "ioc:XXX", "{...}"})
public static final String PROP_JSON_FORMAT = PRE + "jsonFormat";
@PropDoc(value = "连接超时", defaultValue="10000", type="int")
public static final String PROP_CONNECT_TIMEOUT = PRE + "connectTimeout";
@PropDoc(value = "读取超时", defaultValue="60000", type="int")
public static final String PROP_READ_TIMEOUT = PRE + "readTimeout";
@Inject("refer:$ioc")
protected Ioc ioc;
@ -124,6 +131,13 @@ public class FeignStarter implements IocEventListener {
builder.client(client);
builder.logger(logger);
builder.logLevel(level);
int connectTimeout = fc.connectTimeout();
if (connectTimeout == 0)
connectTimeout = conf.getInt(PROP_CONNECT_TIMEOUT, 10*1000);
int readTimeout = fc.readTimeout();
if (readTimeout == 0)
readTimeout = conf.getInt(PROP_READ_TIMEOUT, 10*1000);
builder.options(new Options(connectTimeout, readTimeout));
Object t = null;
if (useHystrix) {
t = ((HystrixFeign.Builder) builder).target(apiType, url, getFallbackIocBean(apiType, fc.fallback()));

View File

@ -48,4 +48,14 @@ public @interface FeignInject {
* @return full/forLook/.... 或者 一个json字符串
*/
String jsonFormat() default "";
/**
* 设置连接超时
*/
int connectTimeout() default 0;
/**
* 设置读取超时
*/
int readTimeout() default 0;
}