OpenFeign的介紹
OpenFeign是一種聲明式 、模板化的HTTP客戶端。
何為聲明式?
就像調用本地方法一樣調用遠程方法,無需感知操作遠程http請求。
何為模板化?
Feign會為每一個Feign接口方法創建一個RequestTemplate對象,該對象封裝了HTTP請求的全部信息,Feign的模板化就體現在這里。
OpenFeign與Feign的之間的關系
OpenFeign是由Feign演變過來,平時說的Feign指的是Netflix旗下的Feign,現在我們使用的是 OpenFeign是Pivotal 提供的。
注:Pivotal 公司可謂是大牛云集,公司的開源產品有:Spring 以及 Spring 衍生產品、Web 服務器 Tomcat、緩存中間件 Redis、消息中間件 RabbitMQ、平臺即服務的 Cloud Foundry、Greenplum 數據引擎、還有大名鼎鼎的 GemFire(12306 系統解決方案組件之一)
Feign
Fegin是Spring Cloud組件中的輕量級RESTful的HTTP服務客戶端,Feign內置了Ribbon,用來做客戶端負載均衡,去調用服務注冊中心的服務。Feign本身不支持Spring MVC的注解,它有一套自己的注解
OpenFeign
OpenFeign是Spring Cloud 在Feign的基礎上支持了Spring MVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通過動態代理的方式產生實現類,實現類中做負載均衡并調用其他服務。
springcloud F 及F版本以上 springboot 2.0 以上基本上使用openfeign,openfeign 如果從框架結構上看就是2019年feign停更后出現版本,也可以說大多數新項目都用openfeign ,2018年以前的項目在使用feign
OpenFegin中的兩個常用注解
@FeignClient:
用于通知Feign組件對該接口進行代理(不需要編寫接口實現),使用者可直接通過@Autowired注入 。
@EnableFeignClients
Spring Cloud應用在啟動時,Feign會掃描標有@FeignClient注解的接口,生成代理,并注冊到Spring容器中。
在項目中使用OpenFeign
調用關系圖
provider是具體的業務提供者,provider-api是對應服務抽出來的Api,供其他服務調用。假如provider-socre中需要調用中provider-vidoe的接口,須在provider-vidoe-api中暴露相應的接口,provider-socre中引入provider-vidoe-api的依賴,直接調用。
導入依賴
在服務中引入OpenFegin的依賴(provider-socre與provider-vidoe-api中都需要引入)
1
2
3
4
5
6
|
<!--openfeign的依賴--> < dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-openfeign</ artifactId > < version >2.2.1.RELEASE</ version > </ dependency > |
使用注解@FeignClient @EnableFeignClients
在provider-video-api中使用@FeignClient
1
2
3
4
5
6
7
8
|
@Component @FeignClient (value= "video" ) //value值是對應的服務名 //通過聲明式的注解,提供一個供其它服務調用的 Client。 public interface VideoBulletchatFeignApi { @GetMapping ( "/videoBulletchat/querySumBulletChat/{id}" ) public Wrapper querySumBulletChat( @PathVariable String id); } |
注意很重要:在video服務中需要有provider-video-api對應的實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@RestController @Slf4j @RequestMapping ( "/videoBulletchat" ) public class VideoBulletchatController { @Resource private VideoBulletChatService videoBulletChatService; @Value ( "${server.port}" ) private String port; @GetMapping ( "querySumBulletChat/{id}" ) public Wrapper querySumBulletChat( @PathVariable String id){ log.info( "視頻id為 " +id+ " 正在查詢彈幕訪問量!" ); log.info( "端口號 " +port); return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,videoBulletChatService.querySumBulletChat(id)); } } |
在provider-score中使用@EnableFeignClients
1
2
3
4
5
6
7
8
9
10
11
|
/** * @author 小小張自由 */ @EnableDiscoveryClient @EnableFeignClients @SpringBootApplication public class ScoreApplication { public static void main(String[] args) { SpringApplication.run(ScoreApplication. class ,args); } } |
注入對象、調用
在provider-score中引用OpenFegin依賴的同時,還要引用provider-video-api 的依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Slf4j @Service public class ScoreService { @Autowired //當需要調用其他服務時, // 直接注入OpenFeign接口對象就可以像調用本地方法一樣調用遠程服務。 private VideoBulletchatFeignApi feignApi; // 測試Feign public int testFegin(String id) { log.info( "開始調用Fegin" ); Wrapper Result = feignApi.querySumBulletChat(id); log.info( "調用Fegin返回成功!" ); return (Integer) Result.getResult(); } } |
總結:
我們在主程序入口添加@EnableFeignClients注解開啟對Feign Client掃描加載處理,根據Feign Client的開發規范,定義接口并添加@FeignClient注解。
當程序啟動時,會進行包掃描,掃描所有@FeignClient的注解的類,并將這些信息注入Spring IOC容器中。當定義的Feign接口中的方法被調用時,通過JDK的代理的方式,來生成具體的RequestTemplate。當生成代理時,Feign會為每個接口方法創建一個RequestTemplate對象,該對象封裝了HTTP請求的全部信息。如請求參數名、請求方法等信息都是在這個過程中確定的。
然后由RequestTemplate生成Request,然后把Request交給Client去處理。這里的Client可以是JDK原生的URLConnection、Apache的Http Client,也可以是OKhttp。最后Client被封裝到LoadBalanceClient類,這個類結合Ribbon負載均衡發起服務之間的調用。
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!
原文鏈接:https://blog.csdn.net/promsing/article/details/119315484