一、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。该做法除非一些特殊场景,不推荐使用。

Feign 注意事项的更多相关文章

  1. Spring cloud Feign 深度学习与应用

    简介 Spring Cloud Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解 ...

  2. Feign性能优化注意事项

    一.FeignClient注解 FeignClient注解被@Target(ElementType.TYPE)修饰,表示FeignClient注解的作用目标在接口上 @FeignClient(name ...

  3. Feign性能优化注意事项--超时

      Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 1. at feign ...

  4. feign调用过程注意事项

    Feign是Netflix开发的声明式.模板化的HTTP客户端, Feign可以帮助我们更快捷.优雅地调用HTTP API. 在Spring Cloud中,使用Feign非常简单——创建一个接口,并在 ...

  5. Feign使用注意事项

    使用Feign时,为了不写重复代码,需要写feign公共接口方便调用,这时候需要注意以下问题,以发邮件为例 定义公共接口 /** * @author liuyalong * @date 2020/10 ...

  6. 在dropwizard中使用feign,使用hystrix

    前言 用惯了spring全家桶之后,试试dropwizard的Hello World也别有一帆风味.为了增强对外访问API的能力,需要引入open feign.这里简单在dropwizard中使用fe ...

  7. 不使用SpringBoot如何将原生Feign集成到Spring中来简化http调用

    在微服务架构中,如果使用得是SpringCloud,那么只需要集成SpringFeign就可以了,SpringFeign可以很友好的帮我们进行服务请求,对象解析等工作. 然而SpingCloud是依赖 ...

  8. Spring Cloud 组件 —— feign

    feign 作为一个声明式的 Http Client 开源项目.在微服务领域,相比于传统的 apache httpclient 与在 spring 中较为活跃的 RestTemplate 更面向服务化 ...

  9. SpringCloud(3)服务消费者(Feign)

    上一篇文章,讲述了如何通过 RestTemplate+Ribbon 去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 1.Feign简介 Feign是一个声明式的伪Http客户端,它使得写H ...

随机推荐

  1. QSetting 说明和简单使用

    今天看到服务端代码有一个QSetting.一开始以为是STL模板中的Set(弄到QT中改了个名字而已).仔细一看吓一跳,不是STL模板.是qt特有的一个类. 用来保存或读取一些配置信息用的.看了后,感 ...

  2. springBoot_freemark配置

    Spring Boot –test 1.添加依赖 <!-- 引入 spring-boot-starter-test 集成单元测试--> <dependency> <gro ...

  3. putty使用秘钥对登录百度云系统全过程

    使用秘钥对登录百度云系统全过程 1在百度云生成秘钥对 并且绑定要登录的IP 1)创建 2) 创建成功后,会自动下载私钥到本地 3)绑定 4)绑定完毕 2把下载到本地的私钥使用putty key gen ...

  4. python库:bs4,BeautifulSoup库、Requests库

    Beautiful Soup https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ Beautiful Soup 4.2.0 文档 htt ...

  5. Mybatis七(MBG 逆向工程)

    官方地址:http://www.mybatis.org/generator/ https://github.com/mybatis/generator/releases <1>编写mbg. ...

  6. foreachPartition来写数据库

    foreachPartition,在生产环境中,通常来说,都使用foreachPartition来写数据库的 使用批处理操作(一条SQL和多组参数) 发送一条SQL语句,发送一次 一下子就批量插入10 ...

  7. php CURL模拟GET、POST请求。

    /** * get * @param string $url 请求地址 */ function GetHttp($url){ // 关闭句柄 $curl = curl_init(); // 启动一个C ...

  8. oracle查询重复的数据

    在oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是oracle中的哪一个数据文件.块.行上.在重复的记录中,可能所有列的内容都相同,但rowi ...

  9. Linux定时任务 结合PHP实现实时监控

    首先说说cron,它是一个linux下的定时执行工具.根用户以外的用户可以使用 crontab 工具来配置 cron 任务. 所有用户定义的 crontab 都被保存在/var/spool/cron ...

  10. MD5类(MD5Helper)

    项目中经常需要使用到MD5来进行加密 代码: namespace MyProject.Common { public class MD5Helper { /// <summary> /// ...