资讯专栏INFORMATION COLUMN

Spring Cloud Feign设计原理

陈江龙 / 2606人阅读

摘要:而从角色划分上来看,他们的职能是一致的提供调用服务。没有基于全部注解来做客户端注解协议解析,个人认为这个是一个不小的坑。真正影响性能的,是处理请求的环节。我们项目内部使用的是作为连接客户端。

什么是Feign?

Feign 的英文表意为“假装,伪装,变形”, 是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。Feign通过处理注解,将请求模板化,当实际调用的时候,传入参数,根据参数再应用到请求上,进而转化成真正的请求,这种请求相对而言比较直观。
Feign被广泛应用在Spring Cloud 的解决方案中,是学习基于Spring Cloud 微服务架构不可或缺的重要组件。

Feign解决了什么问题?

封装了Http调用流程,更适合面向接口化的变成习惯
在服务调用的场景中,我们经常调用基于Http协议的服务,而我们经常使用到的框架可能有HttpURLConnection、Apache HttpComponnets、OkHttp3 、Netty等等,这些框架在基于自身的专注点提供了自身特性。而从角色划分上来看,他们的职能是一致的提供Http调用服务。具体流程如下:

Feign是如何设计的?

PHASE 1. 基于面向接口的动态代理方式生成实现类

在使用feign 时,会定义对应的接口类,在接口类上使用Http相关的注解,标识HTTP请求参数信息,如下所示:

</>复制代码

  1. interface GitHub {
  2. @RequestLine("GET /repos/{owner}/{repo}/contributors")
  3. List contributors(@Param("owner") String owner, @Param("repo") String repo);
  4. }
  5. public static class Contributor {
  6. String login;
  7. int contributions;
  8. }
  9. public class MyApp {
  10. public static void main(String... args) {
  11. GitHub github = Feign.builder()
  12. .decoder(new GsonDecoder())
  13. .target(GitHub.class, "https://api.github.com");
  14. // Fetch and print a list of the contributors to this library.
  15. List contributors = github.contributors("OpenFeign", "feign");
  16. for (Contributor contributor : contributors) {
  17. System.out.println(contributor.login + " (" + contributor.contributions + ")");
  18. }
  19. }
  20. }

在Feign 底层,通过基于面向接口的动态代理方式生成实现类,将请求调用委托到动态代理实现类,基本原理如下所示:

</>复制代码

  1. public class ReflectiveFeign extends Feign{
  2. ///省略部分代码
  3. @Override
  4. public T newInstance(Target target) {
  5. //根据接口类和Contract协议解析方式,解析接口类上的方法和注解,转换成内部的MethodHandler处理方式
  6. Map nameToHandler = targetToHandlersByName.apply(target);
  7. Map methodToHandler = new LinkedHashMap();
  8. List defaultMethodHandlers = new LinkedList();
  9. for (Method method : target.type().getMethods()) {
  10. if (method.getDeclaringClass() == Object.class) {
  11. continue;
  12. } else if(Util.isDefault(method)) {
  13. DefaultMethodHandler handler = new DefaultMethodHandler(method);
  14. defaultMethodHandlers.add(handler);
  15. methodToHandler.put(method, handler);
  16. } else {
  17. methodToHandler.put(method, nameToHandler.get(Feign.configKey(target.type(), method)));
  18. }
  19. }
  20. InvocationHandler handler = factory.create(target, methodToHandler);
  21. // 基于Proxy.newProxyInstance 为接口类创建动态实现,将所有的请求转换给InvocationHandler 处理。
  22. T proxy = (T) Proxy.newProxyInstance(target.type().getClassLoader(), new Class[]{target.type()}, handler);
  23. for(DefaultMethodHandler defaultMethodHandler : defaultMethodHandlers) {
  24. defaultMethodHandler.bindTo(proxy);
  25. }
  26. return proxy;
  27. }
  28. //省略部分代码
PHASE 2. 根据Contract协议规则,解析接口类的注解信息,解析成内部表现:

Feign 定义了转换协议,定义如下:

</>复制代码

  1. /**
  2. * Defines what annotations and values are valid on interfaces.
  3. */
  4. public interface Contract {
  5. /**
  6. * Called to parse the methods in the class that are linked to HTTP requests.
  7. * 传入接口定义,解析成相应的方法内部元数据表示
  8. * @param targetType {@link feign.Target#type() type} of the Feign interface.
  9. */
  10. // TODO: break this and correct spelling at some point
  11. List parseAndValidatateMetadata(Class targetType);
  12. }
默认Contract 实现

Feign 默认有一套自己的协议规范,规定了一些注解,可以映射成对应的Http请求,如官方的一个例子:

</>复制代码

  1. public interface GitHub {
  2. @RequestLine("GET /repos/{owner}/{repo}/contributors")
  3. List getContributors(@Param("owner") String owner, @Param("repo") String repository);
  4. class Contributor {
  5. String login;
  6. int contributions;
  7. }
  8. }

上述的例子中,尝试调用GitHub.getContributors("foo","myrepo")的的时候,会转换成如下的HTTP请求:

</>复制代码

  1. GET /repos/foo/myrepo/contributors
  2. HOST XXXX.XXX.XXX

Feign 默认的协议规范

注解 接口Target 使用说明
@RequestLine 方法上 定义HttpMethod 和 UriTemplate. UriTemplate 中使用{} 包裹的表达式,可以通过在方法参数上使 用@Param 自动注入
@Param 方法参数 定义模板变量,模板变量的值可以使用名称的方式使用模板注入解析
@Headers 类上或者方法上 定义头部模板变量,使用@Param注解提供参数值的注入。如果该注解添加在接口类上,则所有的请求都会携带对应的Header信息;如果在方法上,则只会添加到对应的方法请求上
@QueryMap 方法上 定义一个键值对或者pojo,参数值将会转换成URL上的查询字符串上
@HeaderMap 方法上 定义一个HeaderMap,与UrlTemplate和HeaderTemplate类型,可以使用@Param注解提供参数值

具体FeignContract 是如何解析的,不在本文的介绍范围内,请自行查找。

基于Spring MVC的协议规范SpringMvcContract:

当前Spring Cloud 微服务解决方案中,为了降低学习成本,采用了Spring MVC的部分注解来完成 请求协议解析,也就是说 ,写客户端请求接口和像写服务端代码一样:客户端和服务端可以通过SDK的方式进行约定,客户端只需要引入服务端发布的SDK API,就可以使用面向接口的编码方式对接服务:

</>复制代码

  1. 我们团队内部就是按照这种思路,结合Spring Boot Starter 的特性,定义了服务端starter,
    服务消费者在使用的时候,只需要引入Starter,就可以调用服务。这个比较适合平台无关性,接口抽象出来的好处就是可以根据服务调用实现方式自有切换:

  2. 可以基于简单的Http服务调用;

  3. 可以基于Spring Cloud 微服务架构调用;

  4. 可以基于Dubbo SOA服务治理

  5. 这种模式比较适合在SaSS混合软件服务的模式下自有切换,根据客户的硬件能力选择合适的方式部署,也可以基于自身的服务集群部署微服务

当然,目前的Spring MVC的注解并不是可以完全使用的,有一些注解并不支持,如@GetMapping,@PutMapping 等,仅支持使用@RequestMapping 等,另外注解继承性方面也有些问题;具体限制细节,每个版本能会有些出入,可以参考上述的代码实现,比较简单。

</>复制代码

  1. Spring Cloud 没有基于Spring MVC 全部注解来做Feign 客户端注解协议解析,个人认为这个是一个不小的坑。在刚入手Spring Cloud 的时候,就碰到这个问题。后来是深入代码才解决的.... 这个应该有人写了增强类来处理,暂且不表,先MARK一下,是一个开源代码练手的好机会。
PHASE 3. 基于 RequestBean,动态生成Request

根据传入的Bean对象和注解信息,从中提取出相应的值,来构造Http Request 对象:

PHASE 4. 使用Encoder 将Bean转换成 Http报文正文(消息解析和转码逻辑)

Feign 最终会将请求转换成Http 消息发送出去,传入的请求对象最终会解析成消息体,如下所示:

在接口定义上Feign做的比较简单,抽象出了Encoder 和decoder 接口:

</>复制代码

  1. public interface Encoder {
  2. /** Type literal for {@code Map}, indicating the object to encode is a form. */
  3. Type MAP_STRING_WILDCARD = Util.MAP_STRING_WILDCARD;
  4. /**
  5. * Converts objects to an appropriate representation in the template.
  6. * 将实体对象转换成Http请求的消息正文中
  7. * @param object what to encode as the request body.
  8. * @param bodyType the type the object should be encoded as. {@link #MAP_STRING_WILDCARD}
  9. * indicates form encoding.
  10. * @param template the request template to populate.
  11. * @throws EncodeException when encoding failed due to a checked exception.
  12. */
  13. void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException;
  14. /**
  15. * Default implementation of {@code Encoder}.
  16. */
  17. class Default implements Encoder {
  18. @Override
  19. public void encode(Object object, Type bodyType, RequestTemplate template) {
  20. if (bodyType == String.class) {
  21. template.body(object.toString());
  22. } else if (bodyType == byte[].class) {
  23. template.body((byte[]) object, null);
  24. } else if (object != null) {
  25. throw new EncodeException(
  26. format("%s is not a type supported by this encoder.", object.getClass()));
  27. }
  28. }
  29. }
  30. }

</>复制代码

  1. public interface Decoder {
  2. /**
  3. * Decodes an http response into an object corresponding to its {@link
  4. * java.lang.reflect.Method#getGenericReturnType() generic return type}. If you need to wrap
  5. * exceptions, please do so via {@link DecodeException}.
  6. * 从Response 中提取Http消息正文,通过接口类声明的返回类型,消息自动装配
  7. * @param response the response to decode
  8. * @param type {@link java.lang.reflect.Method#getGenericReturnType() generic return type} of
  9. * the method corresponding to this {@code response}.
  10. * @return instance of {@code type}
  11. * @throws IOException will be propagated safely to the caller.
  12. * @throws DecodeException when decoding failed due to a checked exception besides IOException.
  13. * @throws FeignException when decoding succeeds, but conveys the operation failed.
  14. */
  15. Object decode(Response response, Type type) throws IOException, DecodeException, FeignException;
  16. /** Default implementation of {@code Decoder}. */
  17. public class Default extends StringDecoder {
  18. @Override
  19. public Object decode(Response response, Type type) throws IOException {
  20. if (response.status() == 404) return Util.emptyValueOf(type);
  21. if (response.body() == null) return null;
  22. if (byte[].class.equals(type)) {
  23. return Util.toByteArray(response.body().asInputStream());
  24. }
  25. return super.decode(response, type);
  26. }
  27. }
  28. }

目前Feign 有以下实现:

Encoder/ Decoder 实现 说明
JacksonEncoder,JacksonDecoder 基于 Jackson 格式的持久化转换协议
GsonEncoder,GsonDecoder 基于Google GSON 格式的持久化转换协议
SaxEncoder,SaxDecoder 基于XML 格式的Sax 库持久化转换协议
JAXBEncoder,JAXBDecoder 基于XML 格式的JAXB 库持久化转换协议
ResponseEntityEncoder,ResponseEntityDecoder Spring MVC 基于 ResponseEntity< T > 返回格式的转换协议
SpringEncoder,SpringDecoder 基于Spring MVC HttpMessageConverters 一套机制实现的转换协议 ,应用于Spring Cloud 体系中
PHASE 5. 拦截器负责对请求和返回进行装饰处理

在请求转换的过程中,Feign 抽象出来了拦截器接口,用于用户自定义对请求的操作:

</>复制代码

  1. public interface RequestInterceptor {
  2. /**
  3. * 可以在构造RequestTemplate 请求时,增加或者修改Header, Method, Body 等信息
  4. * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
  5. */
  6. void apply(RequestTemplate template);
  7. }

比如,如果希望Http消息传递过程中被压缩,可以定义一个请求拦截器:

</>复制代码

  1. public class FeignAcceptGzipEncodingInterceptor extends BaseRequestInterceptor {
  2. /**
  3. * Creates new instance of {@link FeignAcceptGzipEncodingInterceptor}.
  4. *
  5. * @param properties the encoding properties
  6. */
  7. protected FeignAcceptGzipEncodingInterceptor(FeignClientEncodingProperties properties) {
  8. super(properties);
  9. }
  10. /**
  11. * {@inheritDoc}
  12. */
  13. @Override
  14. public void apply(RequestTemplate template) {
  15. // 在Header 头部添加相应的数据信息
  16. addHeader(template, HttpEncoding.ACCEPT_ENCODING_HEADER, HttpEncoding.GZIP_ENCODING,
  17. HttpEncoding.DEFLATE_ENCODING);
  18. }
  19. }
PHASE 6. 日志记录

在发送和接收请求的时候,Feign定义了统一的日志门面来输出日志信息 , 并且将日志的输出定义了四个等级:

级别 说明
NONE 不做任何记录
BASIC 只记录输出Http 方法名称、请求URL、返回状态码和执行时间
HEADERS 记录输出Http 方法名称、请求URL、返回状态码和执行时间 和 Header 信息
FULL 记录Request 和Response的Header,Body和一些请求元数据

</>复制代码

  1. public abstract class Logger {
  2. protected static String methodTag(String configKey) {
  3. return new StringBuilder().append("[").append(configKey.substring(0, configKey.indexOf("(")))
  4. .append("] ").toString();
  5. }
  6. /**
  7. * Override to log requests and responses using your own implementation. Messages will be http
  8. * request and response text.
  9. *
  10. * @param configKey value of {@link Feign#configKey(Class, java.lang.reflect.Method)}
  11. * @param format {@link java.util.Formatter format string}
  12. * @param args arguments applied to {@code format}
  13. */
  14. protected abstract void log(String configKey, String format, Object... args);
  15. protected void logRequest(String configKey, Level logLevel, Request request) {
  16. log(configKey, "---> %s %s HTTP/1.1", request.method(), request.url());
  17. if (logLevel.ordinal() >= Level.HEADERS.ordinal()) {
  18. for (String field : request.headers().keySet()) {
  19. for (String value : valuesOrEmpty(request.headers(), field)) {
  20. log(configKey, "%s: %s", field, value);
  21. }
  22. }
  23. int bodyLength = 0;
  24. if (request.body() != null) {
  25. bodyLength = request.body().length;
  26. if (logLevel.ordinal() >= Level.FULL.ordinal()) {
  27. String
  28. bodyText =
  29. request.charset() != null ? new String(request.body(), request.charset()) : null;
  30. log(configKey, ""); // CRLF
  31. log(configKey, "%s", bodyText != null ? bodyText : "Binary data");
  32. }
  33. }
  34. log(configKey, "---> END HTTP (%s-byte body)", bodyLength);
  35. }
  36. }
  37. protected void logRetry(String configKey, Level logLevel) {
  38. log(configKey, "---> RETRYING");
  39. }
  40. protected Response logAndRebufferResponse(String configKey, Level logLevel, Response response,
  41. long elapsedTime) throws IOException {
  42. String reason = response.reason() != null && logLevel.compareTo(Level.NONE) > 0 ?
  43. " " + response.reason() : "";
  44. int status = response.status();
  45. log(configKey, "<--- HTTP/1.1 %s%s (%sms)", status, reason, elapsedTime);
  46. if (logLevel.ordinal() >= Level.HEADERS.ordinal()) {
  47. for (String field : response.headers().keySet()) {
  48. for (String value : valuesOrEmpty(response.headers(), field)) {
  49. log(configKey, "%s: %s", field, value);
  50. }
  51. }
  52. int bodyLength = 0;
  53. if (response.body() != null && !(status == 204 || status == 205)) {
  54. // HTTP 204 No Content "...response MUST NOT include a message-body"
  55. // HTTP 205 Reset Content "...response MUST NOT include an entity"
  56. if (logLevel.ordinal() >= Level.FULL.ordinal()) {
  57. log(configKey, ""); // CRLF
  58. }
  59. byte[] bodyData = Util.toByteArray(response.body().asInputStream());
  60. bodyLength = bodyData.length;
  61. if (logLevel.ordinal() >= Level.FULL.ordinal() && bodyLength > 0) {
  62. log(configKey, "%s", decodeOrDefault(bodyData, UTF_8, "Binary data"));
  63. }
  64. log(configKey, "<--- END HTTP (%s-byte body)", bodyLength);
  65. return response.toBuilder().body(bodyData).build();
  66. } else {
  67. log(configKey, "<--- END HTTP (%s-byte body)", bodyLength);
  68. }
  69. }
  70. return response;
  71. }
  72. protected IOException logIOException(String configKey, Level logLevel, IOException ioe, long elapsedTime) {
  73. log(configKey, "<--- ERROR %s: %s (%sms)", ioe.getClass().getSimpleName(), ioe.getMessage(),
  74. elapsedTime);
  75. if (logLevel.ordinal() >= Level.FULL.ordinal()) {
  76. StringWriter sw = new StringWriter();
  77. ioe.printStackTrace(new PrintWriter(sw));
  78. log(configKey, sw.toString());
  79. log(configKey, "<--- END ERROR");
  80. }
  81. return ioe;
  82. }
PHASE 7 . 基于重试器发送HTTP请求

Feign 内置了一个重试器,当HTTP请求出现IO异常时,Feign会有一个最大尝试次数发送请求,以下是Feign核心
代码逻辑:

</>复制代码

  1. final class SynchronousMethodHandler implements MethodHandler {
  2. // 省略部分代码
  3. @Override
  4. public Object invoke(Object[] argv) throws Throwable {
  5. //根据输入参数,构造Http 请求。
  6. RequestTemplate template = buildTemplateFromArgs.create(argv);
  7. // 克隆出一份重试器
  8. Retryer retryer = this.retryer.clone();
  9. // 尝试最大次数,如果中间有结果,直接返回
  10. while (true) {
  11. try {
  12. return executeAndDecode(template);
  13. } catch (RetryableException e) {
  14. retryer.continueOrPropagate(e);
  15. if (logLevel != Logger.Level.NONE) {
  16. logger.logRetry(metadata.configKey(), logLevel);
  17. }
  18. continue;
  19. }
  20. }
  21. }

重试器有如下几个控制参数:

重试参数 说明 默认值
period foo 100ms
maxPeriod 当请求连续失败时,重试的时间间隔将按照:long interval = (long) (period * Math.pow(1.5, attempt - 1)); 计算,按照等比例方式延长,但是最大间隔时间为 maxPeriod, 设置此值能够避免 重试次数过多的情况下执行周期太长 1000ms
maxAttempts 最大重试次数 5
PHASE 8. 发送Http请求

Feign 真正发送HTTP请求是委托给 feign.Client 来做的:

</>复制代码

  1. public interface Client {
  2. /**
  3. * Executes a request against its {@link Request#url() url} and returns a response.
  4. * 执行Http请求,并返回Response
  5. * @param request safe to replay.
  6. * @param options options to apply to this request.
  7. * @return connected response, {@link Response.Body} is absent or unread.
  8. * @throws IOException on a network error connecting to {@link Request#url()}.
  9. */
  10. Response execute(Request request, Options options) throws IOException;
  11. }

Feign 默认底层通过JDK 的 java.net.HttpURLConnection 实现了feign.Client接口类,在每次发送请求的时候,都会创建新的HttpURLConnection 链接,这也就是为什么默认情况下Feign的性能很差的原因。可以通过拓展该接口,使用Apache HttpClient 或者OkHttp3等基于连接池的高性能Http客户端,我们项目内部使用的就是OkHttp3作为Http 客户端。

如下是Feign 的默认实现,供参考:

</>复制代码

  1. public static class Default implements Client {
  2. private final SSLSocketFactory sslContextFactory;
  3. private final HostnameVerifier hostnameVerifier;
  4. /**
  5. * Null parameters imply platform defaults.
  6. */
  7. public Default(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier) {
  8. this.sslContextFactory = sslContextFactory;
  9. this.hostnameVerifier = hostnameVerifier;
  10. }
  11. @Override
  12. public Response execute(Request request, Options options) throws IOException {
  13. HttpURLConnection connection = convertAndSend(request, options);
  14. return convertResponse(connection).toBuilder().request(request).build();
  15. }
  16. HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
  17. final HttpURLConnection
  18. connection =
  19. (HttpURLConnection) new URL(request.url()).openConnection();
  20. if (connection instanceof HttpsURLConnection) {
  21. HttpsURLConnection sslCon = (HttpsURLConnection) connection;
  22. if (sslContextFactory != null) {
  23. sslCon.setSSLSocketFactory(sslContextFactory);
  24. }
  25. if (hostnameVerifier != null) {
  26. sslCon.setHostnameVerifier(hostnameVerifier);
  27. }
  28. }
  29. connection.setConnectTimeout(options.connectTimeoutMillis());
  30. connection.setReadTimeout(options.readTimeoutMillis());
  31. connection.setAllowUserInteraction(false);
  32. connection.setInstanceFollowRedirects(true);
  33. connection.setRequestMethod(request.method());
  34. Collection contentEncodingValues = request.headers().get(CONTENT_ENCODING);
  35. boolean
  36. gzipEncodedRequest =
  37. contentEncodingValues != null && contentEncodingValues.contains(ENCODING_GZIP);
  38. boolean
  39. deflateEncodedRequest =
  40. contentEncodingValues != null && contentEncodingValues.contains(ENCODING_DEFLATE);
  41. boolean hasAcceptHeader = false;
  42. Integer contentLength = null;
  43. for (String field : request.headers().keySet()) {
  44. if (field.equalsIgnoreCase("Accept")) {
  45. hasAcceptHeader = true;
  46. }
  47. for (String value : request.headers().get(field)) {
  48. if (field.equals(CONTENT_LENGTH)) {
  49. if (!gzipEncodedRequest && !deflateEncodedRequest) {
  50. contentLength = Integer.valueOf(value);
  51. connection.addRequestProperty(field, value);
  52. }
  53. } else {
  54. connection.addRequestProperty(field, value);
  55. }
  56. }
  57. }
  58. // Some servers choke on the default accept string.
  59. if (!hasAcceptHeader) {
  60. connection.addRequestProperty("Accept", "*/*");
  61. }
  62. if (request.body() != null) {
  63. if (contentLength != null) {
  64. connection.setFixedLengthStreamingMode(contentLength);
  65. } else {
  66. connection.setChunkedStreamingMode(8196);
  67. }
  68. connection.setDoOutput(true);
  69. OutputStream out = connection.getOutputStream();
  70. if (gzipEncodedRequest) {
  71. out = new GZIPOutputStream(out);
  72. } else if (deflateEncodedRequest) {
  73. out = new DeflaterOutputStream(out);
  74. }
  75. try {
  76. out.write(request.body());
  77. } finally {
  78. try {
  79. out.close();
  80. } catch (IOException suppressed) { // NOPMD
  81. }
  82. }
  83. }
  84. return connection;
  85. }
  86. Response convertResponse(HttpURLConnection connection) throws IOException {
  87. int status = connection.getResponseCode();
  88. String reason = connection.getResponseMessage();
  89. if (status < 0) {
  90. throw new IOException(format("Invalid status(%s) executing %s %s", status,
  91. connection.getRequestMethod(), connection.getURL()));
  92. }
  93. Map> headers = new LinkedHashMap>();
  94. for (Map.Entry> field : connection.getHeaderFields().entrySet()) {
  95. // response message
  96. if (field.getKey() != null) {
  97. headers.put(field.getKey(), field.getValue());
  98. }
  99. }
  100. Integer length = connection.getContentLength();
  101. if (length == -1) {
  102. length = null;
  103. }
  104. InputStream stream;
  105. if (status >= 400) {
  106. stream = connection.getErrorStream();
  107. } else {
  108. stream = connection.getInputStream();
  109. }
  110. return Response.builder()
  111. .status(status)
  112. .reason(reason)
  113. .headers(headers)
  114. .body(stream, length)
  115. .build();
  116. }
  117. }
Feign 的性能怎么样?

Feign 整体框架非常小巧,在处理请求转换和消息解析的过程中,基本上没什么时间消耗。真正影响性能的,是处理Http请求的环节。
如上所述,由于默认情况下,Feign采用的是JDK的HttpURLConnection,所以整体性能并不高,刚开始接触Spring Cloud 的同学,如果没注意这些细节,可能会对Spring Cloud 有很大的偏见。
我们项目内部使用的是OkHttp3 作为连接客户端。

原文:https://www.jianshu.com/p/8c7...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71840.html

相关文章

  • 拜托!面试请不要再问我Spring Cloud底层原理

    摘要:不过大多数讲解还停留在对功能使用的层面,其底层的很多原理,很多人可能并不知晓。每个线程池里的线程就仅仅用于请求那个服务。 欢迎关注微信公众号:石杉的架构笔记(id:shishan100) 每日更新!精品技术文章准时送上! 目录 一、业务场景介绍 二、Spring Cloud核心组件:Eureka 三、Spring Cloud核心组件:Feign 四、Spring Cloud核心组件:R...

    wums 评论0 收藏0
  • 拜托!面试请不要再问我Spring Cloud底层原理

    摘要:不过大多数讲解还停留在对功能使用的层面,其底层的很多原理,很多人可能并不知晓。每个线程池里的线程就仅仅用于请求那个服务。 欢迎关注微信公众号:石杉的架构笔记(id:shishan100) 每日更新!精品技术文章准时送上! 目录 一、业务场景介绍 二、Spring Cloud核心组件:Eureka 三、Spring Cloud核心组件:Feign 四、Spring Cloud核心组件:R...

    wangjuntytl 评论0 收藏0
  • spring-cloud-feign源码深度解析

    摘要:内部使用了的动态代理为目标接口生成了一个动态代理类,这里会生成一个动态代理原理统一的方法拦截器,同时为接口的每个方法生成一个拦截器,并解析方法上的元数据,生成一个请求模板。的核心源码解析到此结束了,不知道是否对您有无帮助,可留言跟我交流。 Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插拔注释支持,包...

    vibiu 评论0 收藏0
  • Spring Cloud Alibaba Sentinel 整合 Feign设计实现

    摘要:作用跟一致跟属性作用一致给设置注解绝对路径,用于替换服务名。在服务名或与之间默认是,表示当前这个生成的是否是。内部的能获取服务名信息,的实现类能拿到对应的请求路径信息。很不幸,这个类也是包级别的类。整合的代码目前已经在仓库上,但是没未发版。 作者 | Spring Cloud Alibaba 高级开发工程师洛夜来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(H...

    OldPanda 评论0 收藏0
  • Spring Cloud Alibaba Sentinel对Feign的支持

    摘要:得到得到类得到类得到调用的服务名称检查和属性省略部分代码中的方法里面进行熔断限流的处理。在的方法中进行的包装。 Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常方便的,下面来介绍下如何对 Feign 的支持以及实现原理。 集成 Feig...

    wthee 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<