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. c traps and pitfalls reading notes(2)

    1.运算符优先级,这个我是肯定记不住,每次遇到的时候都纠结下,然后去查下,或者直接括号,但是括号太多,你懂得,要用notepad才能理清各种层次.这里啦个下来,留着参考.

  2. 【BZOJ 1179】[Apio2009]Atm

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] tarjan强连通缩点一下. 然后把缩点之后,每个点的钱的数累加起来. 然后从S出发 开始一边做bfs一遍做dp. 最后输出有酒吧的 ...

  3. 【codeforces 67A】Partial Teacher

    [题目链接]:http://codeforces.com/problemset/problem/67/A [题意] 给一个长度为n-1的字符串; 每个字符串是'L','R','='这3种字符中的一个; ...

  4. ActiveMQ学习总结(9)——Linux中安装ActiveMQ

    1.新建一个文件夹activeMQ   mkdir /server 2.授权    chmod 777 /server 3.下载activeMQ安装包,拷贝到/activeMQ目录下 apache-a ...

  5. Jedis无法连接centOS7上的redis

    通过查看资料无法连接基本是两个原因: 1)redis.conf中的bind配置,bind的默认是 127.0.0.1 只可本机访问,我们改为 bind 0.0.0.0 即所有地址均可访问. 2)cen ...

  6. Qt之图形(简笔画-绘制漂亮的西瓜)

    简述 Summer is coming-我们呢,为大家准备了丰盛的佳果-西瓜,清爽解渴,甘味多汁. 一笔一划学简笔画,分分钟让你掌握一门新技能,下面我们来绘制一个"盛夏之王"-西瓜 ...

  7. E-UTRA channel bandwidths per operating band (36.101)

    E-UTRA channel bandwidths per operating band (36.101) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/ ...

  8. less13 颜色值函数

    //通过十进制红色,绿色,蓝色三种值 (RGB) 创建不透明的颜色对象. div{ background: rgb(255,0,0); background: rgb(100%,0%,0%); } / ...

  9. BZOJ 4269 高斯消元求线性基

    思路: 最大: 所有线性基异或一下 次大: 最大的异或一下最小的线性基 搞定~ //By SiriusRen #include <cstdio> #include <algorith ...

  10. python 3.x 学习笔记4(函数)

    1.编程方式分:面向对象.面向过程.函数式编程 2.区分面向对象---->类---->class面向过程---->过程---->def函数式编程---->函数----&g ...