在專案中使用OpenFeign
OpenFeign的介紹
OpenFeign是一種宣告式 、模板化的HTTP客戶端,
OpenFeign與Feign的之間的關系
OpenFegin中的兩個常用注解
在專案中使用OpenFeign
呼叫關系圖
匯入依賴
使用注解@FeignClient @EnableFeignClients
注入物件、呼叫
總結:
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中都需要引入)
<!--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
@Component
@FeignClient(value="video") //value值是對應的服務名
//通過宣告式的注解,提供一個供其它服務呼叫的 Client,
public interface VideoBulletchatFeignApi {
@GetMapping("/videoBulletchat/querySumBulletChat/{id}")
public Wrapper querySumBulletChat(@PathVariable String id);
}
注意很重要:在video服務中需要有provider-video-api對應的實作
@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
/**
* @author 小小張自由
*/
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ScoreApplication {
public static void main(String[] args) {
SpringApplication.run(ScoreApplication.class,args);
}
}
注入物件、呼叫
在provider-score中參考OpenFegin依賴的同時,還要參考provider-video-api 的依賴
@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://www.uj5u.com/houduan/293716.html
標籤:java
