1、FeignClient注解git
FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的做用目标在接口上github
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标签的经常使用属性以下:spring
- 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内容以下:json
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生效.api
2、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 Clientapp
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 :ide
若是 @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。ui
若是不包含Accept header时请求,则是OK:this
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();
}
|
3、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。该作法除非一些特殊场景,不推荐使用。
解决方案有三种,以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。该作法除非一些特殊场景,不推荐使用。