疯狂Spring Cloud连载(12)自定义Feign客户端
来源:互联网 发布:惠普扫描软件 编辑:程序博客网 时间:2024/06/06 09:04
本文节选自《疯狂Spring Cloud微服务架构实战》
Spring Cloud教学视频:http://blog.csdn.net/boxiong86/article/details/78399104
自定义Feign客户端
Feign使用一个Client接口来发送请求,默认情况下,使用HttpURLConnection连接HTTP服务。与前面的编码器类似,客户端也采用了插件式设计,也就是说,我们可以实现自己的客户端。本小节将使用HttpClient来实现一个简单的Feign客户端。为pom.xml加入HttpClient的依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
新建feign.Client接口的实现类,具体实现请见代码清单5-13。
代码清单5-13:codes\05\5.2\feign-use\src\main\java\org\crazyit\feign\MyFeignClient.java
public class MyFeignClient implements Client {
public Response execute(Request request, Options options)
throws IOException {
System.out.println("==== 这是自定义的Feign客户端");
try {
//创建一个默认的客户端
CloseableHttpClient httpclient = HttpClients.createDefault();
//获取调用的HTTP方法
final String method = request.method();
//创建一个HttpClient的HttpRequest
HttpRequestBase httpRequest = new HttpRequestBase() {
public String getMethod() {
return method;
}
};
//设置请求地址
httpRequest.setURI(new URI(request.url()));
//执行请求,获取响应
HttpResponse httpResponse = httpclient.execute(httpRequest);
//获取响应的主体内容
byte[] body = EntityUtils.toByteArray(httpResponse.getEntity());
//将HttpClient的响应对象转换为Feign的Response
Response response = Response.builder()
.body(body)
.headers(new HashMap<String, Collection<String>>())
.status(httpResponse.getStatusLine().getStatusCode())
.build();
return response;
} catch (Exception e) {
throw new IOException(e);
}
}
}
简单讲一下自定义Feign客户端的实现过程,在实现execute方法时,将Feign的Request实例,转换为HttpClient的HttpRequestBase,再使用CloseableHttpClient来执行请求,得到响应的HttpResponse实例后,再转换为Feign的Reponse实例返回。不仅我们实现的客户端,包括Feign自定的客户端以及其他扩展的客户端,实际上就是一个对象转换的过程。在运行类中直接使用我们的自定义客户端,请见代码清单5-14。
代码清单5-14:codes\05\5.2\feign-use\src\main\java\org\crazyit\feign\MyClientTest.java
public class MyClientTest {
public static void main(String[] args) {
//获取服务接口
PersonClient personClient = Feign.builder()
.encoder(new GsonEncoder())
.client(new MyFeignClient())
.target(PersonClient.class, "http://localhost:8080/");
//请求Hello World接口
String result = personClient.sayHello();
System.out.println(" 接口响应内容:" + result);
}
}
运行代码清单5-14,输出如下:
==== 这是自定义的Feign客户端
接口响应内容:Hello World
注意:在本例的实现中,笔者简化了实现,自定义的客户端中并没有转换请求头等信息,因此使用本例的客户端,无法请求其他格式的服务。
虽然Feign也有HttpClient的实现,但本例的目的主要是向大家展示Feign客户的原理。举一反三,如果我们实现一个客户端,在实现中调用Ribbon的API,来实现负载均衡的功能,是完全可以实现的。幸运的是,Feign已经帮我们实现了RibbonClient,可以直接使用,更进一步,Spring Cloud也实现自己的Client,我们将在后面章节中讲述。
本文节选自《疯狂Spring Cloud微服务架构实战》
Spring Cloud教学视频:http://blog.csdn.net/boxiong86/article/details/78399104
本书代码共享地址:https://gitee.com/yangenxiong/SpringCloud
- 疯狂Spring Cloud连载(12)自定义Feign客户端
- 疯狂Spring Cloud连载(10)REST客户端Feign介绍
- 疯狂Spring Cloud连载(14)Spring Cloud整合Feign
- 疯狂Spring Cloud连载(21)Feign与Hystrix整合
- 疯狂Spring Cloud连载(11)Feign的编码器与解码器
- 疯狂Spring Cloud连载(13)Feign第三方注解与注解翻译器
- 疯狂Spring Cloud连载(1)Spring Cloud概述
- 疯狂Spring Cloud连载(8)Spring Cloud与Ribbon
- 疯狂Spring Cloud连载(19)Spring Cloud整合Hystrix
- Spring Cloud探路(三)REST 客户端Feign
- 疯狂Spring Cloud连载(2)开发环境搭建
- 疯狂Spring Cloud连载(4)第一个Eureka程序
- 疯狂Spring Cloud连载(5)Eureka集群搭建
- 疯狂Spring Cloud连载(6)Ribbon介绍
- 疯狂Spring Cloud连载(7)Ribbon负载均衡器
- 疯狂Spring Cloud连载(15)第一个Hystrix程序
- 疯狂Spring Cloud连载(16)Hystrix运作流程
- 疯狂Spring Cloud连载(24)Zuul路由配置
- centos YUM 升级或降级PHP
- 解决mysql建表报错ERROR 1075 (42000): Incorrect table definition...
- thinkphp5 execute执行原生sql插入时获取最新进行新增插入的id
- TOMCAT解压版配置后无法运行解决办法
- js监听滚动条向上还是向下
- 疯狂Spring Cloud连载(12)自定义Feign客户端
- 转:Puppet的部署与应用(转自cnblog的BrianZhu)
- 疯狂Spring Cloud连载(13)Feign第三方注解与注解翻译器
- R語言関連memo
- TCP/IP 寻址
- linux shell编程入门笔记
- java作业
- 浅读Tomcat源码(二)---启动过程
- 关于java