当前位置: 移动技术网 > IT编程>开发语言>Java > FeignClient注解及参数

FeignClient注解及参数

2019年04月12日  | 移动技术网IT编程  | 我要评论

一、feignclient注解

  feignclient注解被@target(elementtype.type)修饰,表示feignclient注解的作用目标在接口上

1
2
3
4
5
@feignclient(name = "github-client", url = "https://api.github.com", configuration = githubexampleconfig.class)
public interface githubclient {
    @requestmapping(value = "/search/repositories", method = requestmethod.get)
    string searchrepo(@requestparam("q") string querystr);
}

 声明接口之后,在代码中通过@resource注入之后即可使用。@feignclient标签的常用属性如下:

  • name:指定feignclient的名称,如果项目使用了ribbon,name属性会作为微服务的名称,用于服务发现
  • url: url一般用于调试,可以手动指定@feignclient调用的地址
  • decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出feignexception
  • configuration: feign配置类,可以自定义feign的encoder、decoder、loglevel、contract
  • fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@feignclient标记的接口
  • fallbackfactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码
  • path: 定义当前feignclient的统一前缀
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@feignclient(name = "github-client",
        url = "https://api.github.com",
        configuration = githubexampleconfig.class,
        fallback = githubclient.defaultfallback.class)
public interface githubclient {
    @requestmapping(value = "/search/repositories", method = requestmethod.get)
    string searchrepo(@requestparam("q") string querystr);
 
    /**
     * 容错处理类,当调用失败时,简单返回空字符串
     */
    @component
    public class defaultfallback implements githubclient {
        @override
        public string searchrepo(@requestparam("q") string querystr) {
            return "";
        }
    }
}

 在使用fallback属性时,需要使用@component注解,保证fallback类被spring容器扫描到,githubexampleconfig内容如下:

1
2
3
4
5
6
7
@configuration
public class githubexampleconfig {
    @bean
    logger.level feignloggerlevel() {
        return logger.level.full;
    }
}

  在使用feignclient时,spring会按name创建不同的applicationcontext,通过不同的context来隔离feignclient的配置信息,在使用配置类时,不能把配置类放到spring app component scan的路径下,否则,配置类会对所有feignclient生效.

二、feign client 和@requestmapping
当前工程中有和feign client中一样的endpoint时,feign client的类上不能用@requestmapping注解否则,当前工程该endpoint http请求且使用accpet时会报404
controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
@restcontroller
@requestmapping("/v1/card")
public class indexapi {
 
    @postmapping("balance")
    @responsebody
    public info index() {
        info.builder builder = new info.builder();
        builder.withdetail("x", 2);
        builder.withdetail("y", 2);
        return builder.build();
    }
}

feign client

1
2
3
4
5
6
7
8
9
10
11
12
13
@feignclient(
        name = "card",
        url = "http://localhost:7913",
        fallback = cardfeignclientfallback.class,
        configuration = feignclientconfiguration.class
)
@requestmapping(value = "/v1/card")
public interface cardfeignclient {
 
    @requestmapping(value = "/balance", method = requestmethod.post, produces = mediatype.application_json_value)
    info info();
 
}  

if @requestmapping is used on class, when invoke http /v1/card/balance, like this :

如果 @requestmapping注解被用在feignclient类上,当像如下代码请求/v1/card/balance时,注意有accept header:

1
2
3
4
content-type:application/json
accept:application/json
 
post http://localhost:7913/v1/card/balance

那么会返回 404。

如果不包含accept header时请求,则是ok:

1
2
content-type:application/json
post http://localhost:7913/v1/card/balance

或者像下面不在feign client上使用@requestmapping注解,请求也是ok,无论是否包含accept:

1
2
3
4
5
6
7
8
9
10
11
12
13
@feignclient(
        name = "card",
        url = "http://localhost:7913",
        fallback = cardfeignclientfallback.class,
        configuration = feignclientconfiguration.class
)
 
public interface cardfeignclient {
 
    @requestmapping(value = "/v1/card/balance", method = requestmethod.post, produces = mediatype.application_json_value)
    info info();
 
}
 
三、feign请求超时问题
hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。而首次请求往往会比较慢(因为spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了
解决方案有三种,以feign为例。
方法一
hystrix.command.default.execution.isolation.thread.timeoutinmilliseconds: 5000
该配置是让hystrix的超时时间改为5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
该配置,用于禁用hystrix的超时时间
方法三
feign.hystrix.enabled: false
该配置,用于索性禁用feign的hystrix。该做法除非一些特殊场景,不推荐使用。
参见:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网