netflix feign概述

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

 

转载于:https://www.cnblogs.com/davidwang456/p/6435311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值