SpringCloud?openfeign聲明式服務調(diào)用實現(xiàn)方法介紹
一、介紹
OpenFeign是一種聲明式、模板化的HTTP客戶端(僅在Application Client中使用)(稱OpenFeign作用:聲明式服務調(diào)用)。聲明式調(diào)用是指,就像調(diào)用本地方法一樣調(diào)用遠程方法,無需感知操作遠程http請求。OpenFeign替換RestTemplate。
二、使用
(1)導入依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
(2)在啟動類上添加掃描注解
/** * EnableFeignClients - 開啟Openfeign技術。讓spring cloud掃描Openfeign相關注解, * 生成動態(tài)代理實現(xiàn)對象。 * 可選屬性 basePackages = {"feign接口所在包1", "feign接口所在包2"} * 默認掃描當前類型所在包,及所有子孫包。 */ @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = {"com.bjsxt.feign"}) public class OpenFeignAppClientApp { public static void main(String[] args) { SpringApplication.run(OpenFeignAppClientApp.class, args); } }
(3)編寫本地接口
/** * 定義接口,基于注解,實現(xiàn)聲明式遠程服務調(diào)用。 * 技術是OpenFeign。 * 需要確定的事情: * 1. 訪問的遠程服務名稱是什么。 * 2. 訪問的遠程服務具體地址是什么。 * 3. 訪問的遠程服務請求方式是什么。 * 4. 訪問的遠程服務,參數(shù)是什么。 * 5. 訪問的遠程服務,返回結果類型是什么。 * * FeignClient - 代表當前的接口是一個OpenFeign客戶端,要訪問遠程的服務。 * 具體的實現(xiàn)類對象,由spring cloud動態(tài)生成代理對象來實現(xiàn)。 * 必要屬性: value - 要訪問的遠程服務命名是什么。 */ @FeignClient("application-service") public interface AppServiceOpenfeignClient { /** * 定義方法。使用SpringMVC注解+方法定義,實現(xiàn)遠程服務訪問規(guī)則定義。 * 建議寫法: 找到要訪問的控制器。復制對應的方法簽名即可。 * * GetMapping - 約束了請求方式 * 注解屬性value - 約束了請求的具體地址 * 方法返回值 - 約束了遠程服務返回結果類型 * 方法參數(shù)表 - 約束了遠程服務的請求參數(shù) */ @GetMapping("/getNoParams") public String getNoParams(); /** * post請求,無參數(shù) * @return */ @PostMapping("/postNoParams") public String postNoParams(); }
(4)本地接口注意事項
形參需要添加對應注解如@RequestParam,@RequestBody,@PathVariable等。
三、通訊優(yōu)化
(1)配置OpenFeign請求-應答的GZIP壓縮
# 配置openfeign請求和應答的gzip壓縮處理
feign:
compression:
request:
enabled: true # 開啟請求壓縮處理。默認false
min-request-size: 128 # 請求容量多少,開始壓縮。默認2048字節(jié)
mime-types: text/html, text/xml, text/plain, text/css, application/json # 請求頭content type是什么,做壓縮處理
response:
enabled: true # 開啟響應壓縮處理。默認false
(2)Tomcat服務器GZIP優(yōu)化配置
server:
compression:
enabled: true # 是否開啟響應壓縮處理。默認false
mime-types: text/html, text/xml, text/plain, text/css, text/javascript, application/javascript, application/json, application/xml # 響應content type什么類型,做壓縮處理。
min-response-size: 128 # 響應容量多大,做壓縮處理。 默認2048字節(jié)
到此這篇關于SpringCloud openfeign聲明式服務調(diào)用實現(xiàn)方法介紹的文章就介紹到這了,更多相關SpringCloud openfeign內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java常用類庫Apache Commons工具類說明及使用實例詳解
這篇文章主要介紹了Java常用類庫Apache Commons工具類說明及使用實例詳解,需要的朋友可以參考下2020-02-02解讀System.getProperty("ENM_HOME")中的值從哪獲取的
這篇文章主要介紹了解讀System.getProperty("ENM_HOME")中的值從哪獲取的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12java統(tǒng)計字符串中指定元素出現(xiàn)次數(shù)方法
這篇文章主要介紹了java統(tǒng)計字符串中指定元素出現(xiàn)次數(shù)方法,需要的朋友可以參考下2015-12-12Spring?boot框架JWT實現(xiàn)用戶賬戶密碼登錄驗證流程
這篇文章主要介紹了Springboot框架JWT實現(xiàn)用戶賬戶密碼登錄驗證,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06使用Spring注解@EventListener實現(xiàn)監(jiān)聽原理
這篇文章主要介紹了使用Spring注解@EventListener實現(xiàn)監(jiān)聽原理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08利用Spring boot如何創(chuàng)建簡單的web交互應用
這篇文章主要介紹了利用Spring boot如何創(chuàng)建簡單的web交互應用的相關資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04