1. feign自定义Configuration和root 容器有效隔离。

  • 用@Configuration注解
  • 不能在主@ComponentScan (or @SpringBootApplication)范围内,从其包名上分离
  • 注意避免包扫描重叠,最好的方法是明确的指定包名

2. Spring Cloud Netflix 提供了默认的Bean类型:

  • Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)
  • Encoder feignEncoder: SpringEncoder
  • Logger feignLogger: Slf4jLogger
  • Contract feignContract: SpringMvcContract
  • Feign.Builder feignBuilder: HystrixFeign.Builder

3. Spring Cloud Netflix没有提供默认值,但仍然可以在feign上下文配置中创建:

  • Logger.Level
  • Retryer
  • ErrorDecoder
  • Request.Options
  • Collection

4. 自定义feign的消息编码解码器:

不要在如下代码中getObject方法内new 对象,外部会频繁调用getObject方法。

1
2
3
4
5
6

ObjectFactory<HttpMessageConverters> messageConvertersObjectFactory = new ObjectFactory<HttpMessageConverters>() {
@Override
public HttpMessageConverters getObject() throws BeansException {
return httpMessageConverters;
}
};

5. 注意测试环境和生产环境,注意正确使用feign日志级别。

6. apacheHttpclient或者其他client的正确配置:

  • apacheHttpclient自定义配置放在spring root context,不要在FeignContext,否则不会起作用。
  • apacheHttpclient 连接池配置合理地连接和其他参数

7. Feign配置

1
2
3
4
5
6
7
8
9
10
11
12
13

#Hystrix支持,如果为true,hystrix库必须在classpath中
feign.hystrix.enabled=false

#请求和响应GZIP压缩支持
feign.compression.request.enabled=true
feign.compression.response.enabled=true
#支持压缩的mime types
feign.compression.request.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048

# 日志支持
logging.level.project.user.UserClient: DEBUG

8. Logger.Level支持

必须为每一个Feign Client配置来告诉Feign如何输出日志,可选:

  • NONE, No logging (DEFAULT).
  • BASIC, Log only the request method and URL and the response status code and execution time.
  • HEADERS, Log the basic information along with request and response headers.
  • FULL, Log the headers, body, and metadata for both requests and responses.

9. FeignClient.fallback 正确的使用方法

配置的fallback class也必须在FeignClient Configuration中实例化,否则会报
java.lang.IllegalStateException: No fallback instance of type class异常。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
public interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
}

public class HystrixClientFallback implements HystrixClient {
@Override
public Hello iFailSometimes() {
return new Hello("fallback");
}
}

@Configuration
public class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}

@Bean
public HystrixClientFallback fb(){
return new HystrixClientFallback();
}

}

10. 使用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
14

@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();

}

 

Spring Cloud Feign 使用方法与性能优化的更多相关文章

  1. 笔记:Spring Cloud Feign 其他配置

    请求压缩 Spring Cloud Feign 支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗,我们只需要通过下面二个参数设置,就能开启请求与响应的压缩功能,yml配置格式如下: fei ...

  2. 使用Spring Cloud Feign

    使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务 在spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就 ...

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

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

  4. 笔记:Spring Cloud Feign Ribbon 配置

    由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...

  5. 笔记:Spring Cloud Feign Hystrix 配置

    在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...

  6. 笔记:Spring Cloud Feign 声明式服务调用

    在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...

  7. 第六章:声明式服务调用:Spring Cloud Feign

    Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...

  8. Spring Cloud Feign Ribbon 配置

    由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...

  9. Spring Cloud feign

    Spring Cloud feign使用 前言 环境准备 应用模块 应用程序 应用启动 feign特性 综上 1. 前言 我们在前一篇文章中讲了一些我使用过的一些http的框架 服务间通信之Http框 ...

随机推荐

  1. VSCode插件开发全攻略(十)打包、发布、升级

    更多文章请戳VSCode插件开发全攻略系列目录导航. 发布方式 插件开发完了,如何发布出去分享给他人呢?主要有3种方法: 方法一:直接把文件夹发给别人,让别人找到vscode的插件存放目录并放进去,然 ...

  2. VSCode插件开发全攻略(七)WebView

    更多文章请戳VSCode插件开发全攻略系列目录导航. 什么是Webview 大家都知道,整个VSCode编辑器就是一张大的网页,其实,我们还可以在Visual Studio Code中创建完全自定义的 ...

  3. Android开发 - 掌握ConstraintLayout(九)分组(Group)

    使用ConstraintLayout后我们的布局是没有层级关系的,各个View之间都是平级关系,但是如果根据某个业务条件来控制多个View的显示与否,我们需要分别对每个View进行控制,需要调用多次s ...

  4. Jquery中attr()与prop()的区别

    在jQuery中,attr()函数和prop()函数都用于设置或获取指定的属性,它们的参数和用法也几乎完全相同.但是,这两个函数的用处却并不相同.下面我们来详细介绍这两个函数之间的区别. 1.操作对象 ...

  5. 一文读懂高性能网络编程中的I/O模型

    1.前言 随着互联网的发展,面对海量用户高并发业务,传统的阻塞式的服务端架构模式已经无能为力.本文(和下篇<高性能网络编程(六):一文读懂高性能网络编程中的线程模型>)旨在为大家提供有用的 ...

  6. 深入分析Java I/O的工作机制 (一)

    此篇博客看至许令波的深入分析javaWeb内幕书籍, 此篇博客写的是自己看完之后理解的重点内容,加一些理解,希望对你有帮助. 1.Java的I/O类库的基本架构 先说一下什么是类库:可以说是类的集合, ...

  7. openjtag 的硬件连接踩坑历程

    这个事情踩了不少坑,这个要记录一下: 1 代码的下载和编译按照下面的来:https://github.com/pulp-platform/pulp-debug-bridge 注意点: A 要最好使用p ...

  8. Testing - 自动化测试的几个基础概念

    自动化测试框架与模型 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块. 自动化测试框架在发展的过程中经历了几个阶段,模块驱动测试 ...

  9. “五年经验”年薪50W分享Java程序员掌握什么技术才不会被淘汰

    在这个IT系统动辄就是上亿流量的时代,Java作为大数据时代应用最广泛的语言,诞生了一批又一批的新技术,包括HBase.Hadoop.MQ.Netty.SpringCloud等等 . 一些独角兽公司以 ...

  10. 编码之痛:操作系统迁移后redis缓存无法命中

    前几天一台内网服务器从ubuntu迁移到了centos,检查一切正常后就没有太在意. 今天有同事反馈迁移后的机器上的服务一个缓存总是无法获取,对比了下环境.JVM参数,尝试了war包替换等方式照样复现 ...