SpringCloud组件之Feign
Feign本质上是一个声明式的 Web 服务客户端,最大的价值就在于:写 Web 服务调用这件事,会轻松很多。用它时,只需要定义一个接口,再配上相应注解,就能把客户端描述清楚。它本身支持可插拔的注解体系,既可以用 Feign 自带注解,也可以用 JAX-RS 注解。同时,编码器和解码器也支持按需替换,扩展性相当不错。到了 Spring Cloud 这一层,又进一步补上了对 Spring MVC 注解的支持,并且默认直接复用了 Spring Web 里的 HttpMessageConverters。再加上与 Ribbon、Eureka 的集成,Feign 在使用过程中还能顺带拿到具备负载均衡能力的 HTTP 客户端。
本文将介绍Feign的原理和一些相关知识点以及如何在项目中使用
一、Feign的原理
1、程序启动后,首先会做一轮包扫描,把所有包里带有@FeignClient注解的类找出来,并注册到 Spring 的 IOC 容器中。等到定义好的 Feign 接口真正被调用时,就会通过 JDK 动态袋里生成对应的 RequestTemplate。
2、RequestTemplate 里装着一次请求所需的完整信息,比如请求参数、请求 URL 等。
3、接下来,RequestTemplate 会进一步生成 Request,然后把这个 Request 交给 client 处理。这个 client 默认用的是 JDK 的 HTTPUrlConnection,当然也可以替换成 OKhttp、Apache 的 HTTPClient 等实现。
4、最后,client 会被封装成 LoadBaLanceClient,再结合 ribbon 的负载均衡能力发起调用。
二、项目中使用
1、搭建Eureka注册中心
本文不介绍如何搭建Eureka服务,不了解Eureka的可以前往查看这篇文章学习:SpringCloud组件之Eureka
2、搭建目标服务
a、导入依赖
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client b、启动类标注Eureka客户端注解
/** * @author Gjing */@SpringBootApplication@EnableEurekaClientpublic class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}} c、yml文件配置
server:port: 8090spring:application:name: demoeureka:client:service-url:defaultZone: http://localhost:8761/eureka/ d、编写接口,提供给Feign调用
/** * @author Gjing **/@RestControllerpublic class TestController {@GetMapping("/test")public Maptest() {Map map = new HashMap<>(16);map.put("code", "ok");return map;}@GetMapping("/test2")public String test2(@RequestParam(name = "param1") String param1) {return param1;}@PostMapping("/test3")public Integer test3(Integer id) {return id;}} 3、搭建调用服务
a、导入依赖
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-openfeign b、启动类增加注解
/** * @author Gjing */@SpringBootApplication@EnableFeignClients@EnableEurekaClientpublic class FeignApplication {public static void main(String[] args) {SpringApplication.run(FeignApplication.class, args);}} c、yml配置
server:port: 8083spring:application:name: feign-demoeureka:client:service-url:defaultZone: http://localhost:8761/eureka/ d、创建service,用于调用服务
/** * @author Gjing **/@FeignClient(name = "demo")public interface FeignTestService {@RequestMapping(value = "/test", method = RequestMethod.GET)Maptest();@RequestMapping(value = "test4", method = RequestMethod.GET)String test2(@RequestParam("param1") String param1);@RequestMapping(value = "/test3", method = RequestMethod.POST)Integer test3(@RequestParam("id") Integer id);} e、编写Controller进行测试访问
/** * @author Gjing **/@RestControllerpublic class FeignTestController {@Resourceprivate FeignTestService feignTestService;@GetMapping("/test")public String test() {return feignTestService.test().toString();}@GetMapping("/test2")public ResponseEntity test2() {String test2 = feignTestService.test2("你好");return ResponseEntity.ok(test2);}@GetMapping("/test3")public ResponseEntity test3() {Integer test3 = feignTestService.test3(1);return ResponseEntity.ok(test3);}} 4、Feign如何进行回退处理
Feign本身集成了Hystrix,因此,我们直接在启动类加上@EnableCircuitBreaker即可,对Hystrix不了解的可以前往这篇文章:SpringCloud组件之Hystrix
a、pom文件增加依赖,否则会Hystrix出错
org.springframework.cloud spring-cloud-starter-netflix-hystrix b、启动类增加注解@EnableCircuitBreaker
c、yml文件开启Hystrix保护
feign:hystrix:enabled: true d、创建回退类并实现之前的Feign接口类
/** * @author Gjing **/@Componentpublic class FeignTestFallbackImpl implements FeignTestService {@Overridepublic Map test() {// TODO: 2019/7/3 这里就实现回退后的处理咯 Map map = new HashMap<>(16);map.put("code", "回退了");return map;}@Overridepublic String test2(String param1) {return null;}@Overridepublic Integer test3(Integer id) {return null;}} e、修改feign接口类,设置回退类
/** * @author Gjing **/@FeignClient(name = "demo",fallback = FeignTestFallbackImpl.class)public interface FeignTestService {@RequestMapping(value = "/test", method = RequestMethod.GET)Map test();@RequestMapping(value = "test4", method = RequestMethod.GET)String test2(@RequestParam("param1") String param1);@RequestMapping(value = "/test3", method = RequestMethod.POST)Integer test3(@RequestParam("id") Integer id);} f、调用测试
如果出现超时或者异常,将进行回退处理
本文到此就结束了,如果发现有误欢迎指正哦,Feign默认是使用HttpUrlConnection进行http请求的,还支持okHttp和httpClient的哈,这些用法大家自行研究,本文不作介绍了,如果有兴趣了解另一种使用Feign,可以参考我这篇文章:SpringCloud-Feign,Demo源代码地址:SpringCloud-Demo
-
- 元宵节猜灯谜的祝福短信
- 角色扮演 |
-
- 关于柯南的沙雕网名有哪些
- 角色扮演 | 1
- 网名
-
- 最新中性名字男女通用网名有哪些
- 角色扮演 | 1
- 网名
-
- 关于蓝色说唱的网名有哪些
- 角色扮演 | 1
- 网名
-
- 我好喜欢你是什么梗?
- 角色扮演 |
