上一篇文章,讲述了Ribbon去做负载请求的服务消费者,本章讲述声明性REST客户端:Feign的简单使用方式

- Feign简介

Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 。它具有可插拔注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载平衡的http客户端。

官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign

- 准备工作

1.启动Consul,所有文章都将以Consul作为服务注册中心

2.创建 battcn-feign-hellobattcn-feign-hi

battcn-feign-hello

- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

- BattcnFeignHelloApplication.java

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableDiscoveryClient
public class BattcnFeignHelloApplication { public static void main(String[] args) {
SpringApplication.run(BattcnFeignHelloApplication.class, args);
}
}

创建一个打招呼的的Controller

- HelloController.java

1
2
3
4
5
6
7
8
9
10
11
@RestController
@RequestMapping("/hello")
public class HelloController { @ResponseStatus(HttpStatus.OK)
@GetMapping
public String findStudentByName(@RequestParam("name") String name) {
// TODO:不做具体代码实现,只打印Log
return "挽歌- My Name's" + name;
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8765 spring:
application:
name: battcn-feign-hello
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

访问:http://localhost:8765/hello?name=Levin

结果:挽歌- My Name's Levin 表示我们第一个服务运行一切正常

battcn-feign-hi

- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

- BattcnFeignHiApplication.java

1
2
3
4
5
6
7
8
9
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.battcn.client"})
@SpringBootApplication
public class BattcnFeignHiApplication { public static void main(String[] args) {
SpringApplication.run(BattcnFeignHiApplication.class, args);
}
}

- HelloClient.java

创建一个声明式FeignClient的接口 HelloClient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@FeignClient(name = "battcn-feign-hello")
public interface HelloClient { /**
* 在新版本中,Feign 已经可以解析 RestFul 标准的接口API,比如GET POST DELETE PATCH PUT
* 旧版中
* @RequestMapping(method = RequestMethod.GET, value = "/hello")
* 或者是
* @RequestMapping(method = RequestMethod.POST, value = "/hello")
*
* 早期文章:http://blog.battcn.com/2017/07/07/springcloud-feign-analysis/
* /
@ResponseStatus(HttpStatus.OK)
@GetMapping("/hello")
String findStudentByName(@RequestParam("name") String name);
}

- HiController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("/hi")
public class HiController { @Autowired
HelloClient helloClient; @ResponseStatus(HttpStatus.OK)
@GetMapping
public String find(@RequestParam("name") String name) {
// TODO:只是演示Feign调用的方法
return "Hi," + helloClient.findStudentByName(name);
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8766 spring:
application:
name: battcn-feign-hi
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

访问:http://localhost:8766/hi?name=Levin

结果:Hi,挽歌- My Name'sLevin 表示第二个服务(hi)通过FeignClient调用服务(hello)一切正常

- 源码分析

org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(Object... args),该方法的 args 就是我们传递的请求参数

InvocableHandlerMethod

feign.Target -> HardCodedTarget 解析出 FeignClient中的 name 形成VIP模式 GET http://battcn-feign-hello/hello?name=Levin HTTP/1.1 然后发送请求

HardCodedTarget

feign.SynchronousMethodHandler -> this.client.execute(request, this.options);

第一个就是:feign.Request的一些信息,比如headermethodbodyurl 等一些基本属性,因为这里是feign的Request所以我们servlet中的请求头是无法传递过来的(下篇会讲述这写小技巧)

第二个就是:connectTimeoutMillis 和 readTimeoutMillis 很多人肯定不陌生

SynchronousMethodHandler

通过三面三个步骤,我们不难看出 Feign 就是解析注解然后发送HTTP(阻断器,OKHTTP模式留到下篇),有兴趣的可以自己Debug(如果该处有错误也希望各位指正)

最新文章

  1. ArcGIS Engine开发之属性查询
  2. TFS API:三、TFS WorkItem添加和修改、保存
  3. 重新注册IIS
  4. Android自动化测试 - Robotium之Robotium在不同分辨率下clickonview不支持解决方案
  5. ubuntu 命令的快捷启动
  6. 开源项目剖析之apache-common-pool
  7. iOS7总显示状态栏的解决方法
  8. 打开office弹出steup error 的解决办法
  9. Oracle----&gt;基本DDL
  10. 详解Java反射各种应用
  11. [Redux] Extracting Container Components -- VisibleTodoList
  12. SpringCloud的EurekaClient : 客户端应用访问注册的微服务(无断路器场景)
  13. Python面向对象之反射
  14. python之路day04--列表的增删改查,嵌套、元组的嵌套、range、for循环嵌套
  15. eclipse设置字体_字符编码_快捷键
  16. 这几道Java集合框架面试题几乎必问
  17. linux&amp;php:ubuntu安装php-7.2
  18. easyui+webuploader+ckeditor实现插件式多图片上传
  19. Spark job 部署模式
  20. allegro 基本步骤

热门文章

  1. Java实现 蓝桥杯VIP 算法提高 P0402
  2. SQL server 常用的数据库 DDL语言
  3. Java实现第八届蓝桥杯杨辉三角
  4. 【CSS】滚动条样式
  5. Python UI自动化测试实操
  6. 如何安装vue脚手架?
  7. 体验SpringBoot(2.3)应用制作Docker镜像(官方方案)
  8. 源码分析(1)-HashMap(JDK1.8)
  9. ecshop头部添加所在城市
  10. IP组网实验(使用Cisco Packet Tracer路由器模拟软件)