• 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

  • 相关阅读:
    windows下rabbitmq 延迟插件安装
    java实现生产者和消费者 类比消息中间件
    missing go.sum entry for module providing package <package_name>
    python匹配字符串中某个词的开始位置和结束位置
    Linux中使用Docker安装MongoDB
    pypy下载和安装
    linux配置环境变量
    python替换字符串指定位置上的元素
    Flink:状态管理
    Flink:时间和水位线
  • 原文地址:https://www.cnblogs.com/davidwang456/p/6435311.html
Copyright © 2020-2023  润新知