5.5、在Feign中使用HttpClient和OkhHttp

Feign 中、Client 是一个非常重要的组件,
Feign 最终发送 Request 请求以及接收 Response响应都是由 Client 组件完成的。
 
Client在Feign 源码中是一 个接口,
默认的情况下:
Client的实现类是 Client.Default
Client.Default 是由 HttpURLConnnection 来实现网络请求的。
Client 支持 HttpClient、OkhHttp 来进行网络请求。
 
FeignRibbonClient 的自动配置类 FeignRibbonClientAutoConfiguration

FeignRibbonClientAutoConfiguration.java

@ConditionalOnClass({ILoadBalancer.class, Feign.class})
@Configuration
@AutoConfigureBefore({FeignAutoConfiguration.class})
@EnableConfigurationProperties({FeignHttpClientProperties.class})
@Import({HttpClientFeignLoadBalancedConfiguration.class, OkHttpFeignLoadBalancedConfiguration.class, DefaultFeignLoadBalancedConfiguration.class})
public class FeignRibbonClientAutoConfiguration {
public FeignRibbonClientAutoConfiguration() {
} @Bean
@Primary
@ConditionalOnMissingBean
@ConditionalOnMissingClass({"org.springframework.retry.support.RetryTemplate"})
public CachingSpringLoadBalancerFactory cachingLBClientFactory(SpringClientFactory factory) {
return new CachingSpringLoadBalancerFactory(factory);
} @Bean
@Primary
@ConditionalOnMissingBean
@ConditionalOnClass(
name = {"org.springframework.retry.support.RetryTemplate"}
)
public CachingSpringLoadBalancerFactory retryabeCachingLBClientFactory(SpringClientFactory factory, LoadBalancedRetryFactory retryFactory) {
return new CachingSpringLoadBalancerFactory(factory, retryFactory);
} @Bean
@ConditionalOnMissingBean
public Options feignRequestOptions() {
return LoadBalancerFeignClient.DEFAULT_OPTIONS;
}
}
1、若要使用HttpClient
在pom添加依赖:
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>

配置文件中:

feign.httpclient.enabled=true

从新启动即可(具体看源码进行分析)

2、同理若要使用OkHttp
添加依赖:

配置文件中:
feign.okhttp.enabled=true
 
再次启动即可....

5.6、Feign如何实现负载均衡

LoadBalancerFeignClient.java

public Response execute(Request request, Options options) throws IOException {
try {
URI asUri = URI.create(request.url());
String clientName = asUri.getHost();
URI uriWithoutHost = cleanUrl(request.url(), clientName);
RibbonRequest ribbonRequest = new RibbonRequest(this.delegate, request, uriWithoutHost);
IClientConfig requestConfig = this.getClientConfig(options, clientName);
return ((RibbonResponse)this.lbClient(clientName).executeWithLoadBalancer(ribbonRequest, requestConfig)).toResponse();
} catch (ClientException var8) {
IOException io = this.findIOException(var8);
if (io != null) {
throw io;
} else {
throw new RuntimeException(var8);
}
}
}

execute执行请求的方法

executeWithLoadBalancer()方法:通过负载均衡的方式来执行网络请求
代码如下:
public T executeWithLoadBalancer(final S request, final IClientConfig requestConfig) throws ClientException {
LoadBalancerCommand command = this.buildLoadBalancerCommand(request, requestConfig); try {
return (IResponse)command.submit(new ServerOperation<T>() {
public Observable<T> call(Server server) {
URI finalUri = AbstractLoadBalancerAwareClient.this.reconstructURIWithServer(server, request.getUri());
ClientRequest requestForServer = request.replaceUri(finalUri); try {
return Observable.just(AbstractLoadBalancerAwareClient.this.execute(requestForServer, requestConfig));
} catch (Exception var5) {
return Observable.error(var5);
}
}
}).toBlocking().single();
} catch (Exception var6) {
Throwable t = var6.getCause();
if (t instanceof ClientException) {
throw (ClientException)t;
} else {
throw new ClientException(var6);
}
}
}
submit()方法:
进入方法内部:
可以看出它是LoadBalancerCommand类的方法
public Observable<T> submit(final ServerOperation<T> operation) {
final LoadBalancerCommand<T>.ExecutionInfoContext context = new LoadBalancerCommand.ExecutionInfoContext();
if (this.listenerInvoker != null) {
try {
this.listenerInvoker.onExecutionStart();
} catch (AbortExecutionException var6) {
return Observable.error(var6);
}
} final int maxRetrysSame = this.retryHandler.getMaxRetriesOnSameServer();
final int maxRetrysNext = this.retryHandler.getMaxRetriesOnNextServer();
Observable<T> o = (this.server == null ? this.selectServer() : Observable.just(this.server)).concatMap(new Func1<Server, Observable<T>>() {
public Observable<T> call(Server server) {
context.setServer(server);
......

上述代码中有一个selectServe()方法:该方法就是选择服务进行负载均衡的方法

private Observable<Server> selectServer() {
return Observable.create(new OnSubscribe<Server>() {
public void call(Subscriber<? super Server> next) {
try {
Server server = LoadBalancerCommand.this.loadBalancerContext.getServerFromLoadBalancer(LoadBalancerCommand.this.loadBalancerURI, LoadBalancerCommand.this.loadBalancerKey);
next.onNext(server);
next.onCompleted();
} catch (Exception var3) {
next.onError(var3);
} }
});
}

由上述可知,最终负载均衡交给loadbalancerContext来处理

5.7、总结

1、首先通过@EnableFeignClients 注解开启 FeignClient 的功能。只有这个注解存在,才
会在程序启动时开启对@FeignClient 注解的包扫描
2、根据 Feign的规则实现接口,并在接口上面加上@FeignClient注解
3、程序启动后,会进行包扫描,扫描所有的@ FeignClient 注解 ,并将这些信息注入 IoC 容器中。
 
4、当接口的方法被调用时 通过 JDK 的代理来生成 体的 RequestTemplate模板对象
5、根据 RequestTemplate 再生成 Http 请求的 Request 对象
6、Request 对象交给 Client 去处理 其中 Client 的网络请求框架可以是 HttpURLConnect on、HttpClient、OkHttp
7、最后 Client 被封装到 LoadBalanceClient 类,这个类结合类 Ribbon 做到了负载均衡

5、Spring Cloud-声明式调用 Feign(下)的更多相关文章

  1. spring cloud服务间调用feign

    参考文章:Spring Cloud Feign设计原理 1.feign是spring cloud服务间相互调用的组件,声明式.模板化的HTTP客户端.类似的HttpURLConnection.Apac ...

  2. Spring Cloud声明式调用Feign负载均衡FeignClient详解

    为了深入理解Feign,下面将从源码的角度来讲解Feign.首先来看看FeignClient注解@FeignClient的源码,代码如下: FeignClient注解被@Target(ElementT ...

  3. spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  4. Spring-Cloud之Feign声明式调用-4

    一.Feign受Retrofit.JAXRS-2.0和WebSocket影响,采用了声明式API 接口的风格,将Java Http 客户端绑定到它的内部. Feign 首要目的是将 Java Http ...

  5. Spring Cloud项目中通过Feign进行内部服务调用发生401\407错误无返回信息的问题

    问题描述 最近在使用Spring Cloud改造现有服务的工作中,在内部服务的调用方式上选择了Feign组件,由于服务与服务之间有权限控制,发现通过Feign来进行调用时如果发生了401.407错误时 ...

  6. 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】

    环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...

  7. Spring Cloud系列之使用Feign进行服务调用

    在上一章的学习中,我们知道了微服务的基本概念,知道怎么基于Ribbon+restTemplate的方式实现服务调用,接着上篇博客,我们学习怎么基于Feign实现服务调用,请先学习上篇博客,然后再学习本 ...

  8. Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现

    作者 | Spring Cloud Alibaba 高级开发工程师洛夜 来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发...Spring Cloud ...

  9. Spring Cloud系列文,Feign整合Ribbon和Hysrix

    在本博客之前的Spring Cloud系列里,我们讲述了Feign的基本用法,这里我们将讲述下Feign整合Ribbon实现负载均衡以及整合Hystrix实现断路保护效果的方式. 1 准备Eureka ...

  10. Spring Cloud Alibaba Sentinel对Feign的支持

    Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常 ...

随机推荐

  1. 利用三层判断sql数据库中编码是否已经存在(个人拙作,不喜勿喷)

    本人.Net新手,初学三层便想记录写笔记,以供像自己一样的新人去学习,大神就不用看啦 Dal层: /// 判断编码是否存在 /// </summary> /// <param nam ...

  2. Axios的详细配置和相关使用

    Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. Features 从浏览器中创建 XMLHttpRequests 从 node.js 创建 http  ...

  3. Vue学习笔记:methods、computed、watch的区别

    自:https://www.jb51.net/article/120073.htm 首先要说,methods,watch和computed都是以函数为基础的,但各自却都不同 而从作用机制和性质上看,m ...

  4. 六、curator recipes之屏障barrier

    简介 curator针对分布式场景实现了分布式屏障:barrier.我们在分布式系统中可以使用barrier去阻塞进程,知道某个条件被触发.其实跟Java多线程的barrier是一样的. 例如:当两个 ...

  5. Google Voice号码使用说明及用途

    Google Voice号码使用说明及用途 号码 已不能网页注册获取,直接向TB购买,搜Google Voice就行了.很便宜的. 一. Google Voice介绍 Google Voice首先是一 ...

  6. 高性能分布式锁-redisson的使用

    1,概述:在一些高并发的场景中,比如秒杀,抢票,抢购这些场景,都存在对核心资源,商品库存的争夺,控制不好,库存数量可能被减少到负数,出现超卖的情况,或者 产生唯一的一个递增ID,由于web应用部署在多 ...

  7. 5.Resource注解解析

    Resource有两种使用场景 1.Resource 当Resource后面没带参数的时候是根据它所注释的属性名称到applicationContext.xml文件中查找是否有bean的id与之匹配, ...

  8. grunt-contrib-watch 监控 JS 文件改变来运行预定义的Tasks

    依赖于 GruntJs ~0.4.0 监控 JS 文件改变来运行预定义的Tasks Demo: watch: { scripts: { files: ['src/**/*.js'], tasks: [ ...

  9. iView定制主题报错问题的解决方法

    按照iView官网来是这样的: 1. 在main.js当前目录下新建themes文件夹,里面新建一个叫blue.less的文件 2. 在mian.js里面引入blue.less文件 3. blue.l ...

  10. AE+C#实现:在SceneControl里打开和保存

    来自:http://www.cnblogs.com/zhuxy/archive/2012/03/30/2424672.html 之前编写这段代码,发现一直没有C#编写的,现在贴出来,希望对大家有用 此 ...