1.什么是feign?feign的作用是什么?

Feign is a java to http client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to http apis regardless of restfulness.

2.为什么使用feign?

You can use tools like Jersey and CXF to write java clients for ReST or SOAP services. You can write your own code on top of http transport libraries like Apache HC. Feign aims to connect your code to http apis with minimal overhead and code. Via customizable decoders and error handling, you should be able to write to any text-based http api.

3.feign工作机制

Feign works by processing annotations into a templatized request. Just before sending it off, arguments are applied to these templates in a straightforward fashion. While this limits Feign to only supporting text-based apis, it dramatically simplified system aspects such as replaying requests. It is also stupid easy to unit test your conversions knowing this.

4.主要类及其层次结构

1.Feign's purpose is to ease development against http apis that feign restfulness.  In implementation, Feign is a {@link Feign#newInstance factory} for generating {@link Target targeted} http apis.

重要方法:newInstance():Returns a new instance of an HTTP API, defined by annotations in the {@link Feign Contract}, for the specified {@code target}. You should cache this result.

2.Contract: Defines what annotations and values are valid on interfaces.

3.Client:Submits HTTP {@link Request requests}. Implementations are expected to be thread-safe.

4.Retryer:Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}.Implementations may keep state to determine if retry operations should continue or not.

5.Encoder

Encodes an object into an HTTP request body. Like {@code javax.websocket.Encoder}. {@code
Encoder} is used when a method parameter has no {@code @Param} annotation. For example:

  @POST
@Path("/")
void create(User user);

Example implementation:

  public class GsonEncoder implements Encoder {
private final Gson gson; public GsonEncoder(Gson gson) {
this.gson = gson;
} @Override
public void encode(Object object, Type bodyType, RequestTemplate template) {
template.body(gson.toJson(object, bodyType));
}
}

Form encoding If any parameters are found in {@link
feign.MethodMetadata#formParams()}, they will be collected and passed to the Encoder as a map.

Ex. The following is a form. Notice the parameters aren't consumed in the request line. A map
including "username" and "password" keys will passed to the encoder, and the body type will be
{@link #MAP_STRING_WILDCARD}.

  @RequestLine("POST /")
Session login(@Param("username") String username, @Param("password") String
password);

6.Decoder

Decodes an HTTP response into a single object of the given {@code type}. Invoked when {@link
Response#status()} is in the 2xx range and the return type is neither {@code void} nor {@code
Response}. Example Implementation:

  public class GsonDecoder implements Decoder {
private final Gson gson = new Gson(); @Override
public Object decode(Response response, Type type) throws IOException {
try {
return gson.fromJson(response.body().asReader(), type);
} catch (JsonIOException e) {
if (e.getCause() != null &&
e.getCause() instanceof IOException) {
throw IOException.class.cast(e.getCause());
}
throw e;
}
}
}

<h3>Implementation Note</h3> The {@code type} parameter will correspond to the {@link java.lang.reflect.Method#getGenericReturnType() generic return type} of an {@link feign.Target#type() interface} processed by {@link feign.Feign#newInstance(feign.Target)}. When writing your implementation of Decoder, ensure you also test parameterized types such as {@code List<Foo>}.
<h3>Note on exception propagation</h3> Exceptions thrown by {@link Decoder}s get wrapped in a {@link DecodeException} unless they are a subclass of {@link FeignException} already, and unless
the client was configured with {@link Feign.Builder#decode404()}.

7. InvocationHandlerFactory:Controls reflective method dispatch

netflix feign概述的更多相关文章

  1. SpringCloud学习笔记(六、SpringCloud Netflix Feign)

    目录: feign简介 feign应用 feign简介: feign是一款Netflix开源的声明式.模板化的http客户端,它可以更加便捷.优雅的调用http api:SpringCloud对Net ...

  2. SpringCloud Netflix Feign

    调用其它机器上的服务(远程调用)有2种技术:REST.RPC. REST 注入RestTempalte,服务提供者的url要写成RESTful风格,在url中传递参数. 如果参数很多,url会有一长串 ...

  3. netflix ribbon概述

    LB方案分类 目前主流的LB方案可分成两类:一种是集中式LB, 即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责把访问请求通过某种策略 ...

  4. netflix turbine概述

    1.turbine是什么?它的作用是什么? Turbine is a tool for aggregating streams of Server-Sent Event (SSE) JSON data ...

  5. Spring Cloud netflix feign【服务间通信】

    一.简介 1,进程间通讯的本质是交换消息 2,服务间通信的两种方式 (1)RESTFul风格 (2)RPC风格 (3)两种风格的比较 3.基于RESTFul风格服务调用模型 4.基于Spring Cl ...

  6. 什么是 Netflix Feign?它的优点是什么?

    Feign 是受到 Retrofit,JAXRS-2.0 和 WebSocket 启发的 java 客户端联编程序. Feign 的第一个目标是将约束分母的复杂性统一到 http apis,而不考虑其 ...

  7. 什么是Netflix Feign?它的优点是什么?

    Feign是受到Retrofit,JAXRS-2.0和WebSocket启发的java客户端联编程序.Feign的第一个目标是将约束分母的复杂性统一到http apis,而不考虑其稳定性.在emplo ...

  8. Feign报错Caused by: com.netflix.client.ClientException: Load balancer does not have available server for client

    问题描述 使用Feign调用微服务接口报错,如下: java.lang.RuntimeException: com.netflix.client.ClientException: Load balan ...

  9. SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性

    1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...

随机推荐

  1. 红黑树(RBTREE)之上-------构造红黑树

    该怎么说呢,现在写代码的速度还是很快的,很高兴,o(^▽^)o. 光棍节到了,早上没忍住,手贱了一般,看到*D的优惠,买了个机械键盘,晚上就到了,敲着还是很舒服的,和老婆炫耀了一把哈哈. 光棍节再去* ...

  2. centos7 bond0 双网卡配置

    [root@openldap ~]# ifconfig bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500 ...

  3. Vue内置指令

    v-text 类型: string用法: 更新元素的 textContent.如果要更新部分的 textContent ,需要使用 {{ Mustache }} 插值. v-html 类型: stri ...

  4. WPF打字机效果

    public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); TypewriteTex ...

  5. SpringBoot实战(三)代码热部署

    每次代码改动后都需要重新手动Run项目,心累,在网上找了下,发现SpringBoot提供了热部署的方案,改动代码后自动编译打包,现在将热部署的配置方法记下来: 第一步:在pom.xml中添加依赖,导入 ...

  6. ZOJ 2702 Unrhymable Rhymes

    Unrhymable Rhymes Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu De ...

  7. Unix发展史

    简述 了解过去,我们才能知其然,更知所以然.总结过去,我们才会知道明天该何去何从.在时间的滚轮中,许许多多的东西就像流星一样一闪而逝,而有些东西却能经受着时间的考验散发着经久的魅力,让人津津乐道.流传 ...

  8. Qt之QDesktopServices

    简述 QDesktopServices类提供的函数用于访问常见的桌面服务. 许多桌面环境都会提供一系列服务,可以通过应用程序来执行常见任务,如:以用户应用程序首选项的方式,打开一个网页. 此类包含为服 ...

  9. Qt之QPushButton

    简述 前面章节我们分享过Qt之QAbstractButton,讲解了QAbstractButton的基本用法,本节着重讲解QPushButton. 简述 常用状态 效果 源码 QSS 更多参考 关于Q ...

  10. asm下重定向数据文件小例(使用toad工具)

    原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46863991 场景 ...