SpringCloud學(xué)習(xí)筆記之OpenFeign進行服務(wù)調(diào)用
前言
Feign是一個聲明式的Web服務(wù)客戶端,是面向接口編程的。也就是說使用Feign,只需要創(chuàng)建一個接口并使用注解方式配置它,就可以完成對微服務(wù)提供方的接口綁定。
在使用RestTemplate時,每次調(diào)用服務(wù)都需要指定服務(wù)的具體路徑,當(dāng)在多個地方同時使用時要寫多次,顯得代碼冗余也難以維護,而openfeign就可以避免這種操作。
1、OpenFeign
1.1、OpenFeign概述
1. 是什么?
Feign是一個聲明式的web服務(wù)客戶端,讓編寫web服務(wù)客戶端變得非常容易,只需創(chuàng)建一個接口并在接口上添加注解即可。
2. 能干嘛?
- Feign旨在使編寫Java Http客戶端變得更容易。
- 前面的服務(wù)調(diào)用在使用Ribbon + RestTemplate時,利用RestTemplate對http請求的封裝處理,形成了一套模板化的調(diào)用方法。但是在實際開發(fā)中,由于對服務(wù)依賴的調(diào)用可能不止一處,往往一個接口會被多處調(diào)用,所以通常都會針對每個微服務(wù)自行封裝一些客戶端類來包裝這些以來服務(wù)的調(diào)用。所以,F(xiàn)eign在此基礎(chǔ)上做了進一步封裝,由他來幫助我們定義和實現(xiàn)依賴服務(wù)接口的定義。在Feign的實現(xiàn)下,我們只需創(chuàng)建一個接口并使用注解的方式來配置它(以前是Dao接口上面標(biāo)注Mapper注解,現(xiàn)在是一個微服務(wù)接口上面標(biāo)志一個Feign注解即可),即可完成對服務(wù)提供方的接口綁定,簡化了使用Spring Cloud Ribbon時,自動封裝服務(wù)調(diào)用客戶端的開發(fā)量。
- Feign集成了Ribbon,利用Ribbon維護了Payment的服務(wù)列表信息,并且通過輪詢實現(xiàn)了客戶端的負(fù)載均衡。而與Ribbon不同的是,通過Feign只需要定義服務(wù)綁定接口且以聲明式的方法,優(yōu)雅而簡單的實現(xiàn)了服務(wù)調(diào)用。
3. Feign和OpenFeign的區(qū)別
Feign | OpenFeign |
---|---|
Feign是SpringCloud組件中的一個輕量級restful的Http服務(wù)客戶端,F(xiàn)eign內(nèi)置了Ribbon,用來做客戶端負(fù)載均衡,去調(diào)用服務(wù)注冊中心的服務(wù)。Feign的使用方式是:使用Feign的注解定義接口,調(diào)用這個接口,就可以調(diào)用服務(wù)注冊中心的服務(wù) | OpenFeign是SpringCloud在Feign的基礎(chǔ)上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通過動態(tài)代理的方式實現(xiàn)類,實現(xiàn)類中做負(fù)載均衡并調(diào)用其他服務(wù) |
1.2、OpenFeign的使用步驟
1. 建Module
Module的名稱為cloud-consumer-feign-order80。
2. 改POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud02</artifactId> <groupId>com.xiao</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-feign-order80</artifactId> <!--openfeign--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.xiao</groupId> <artifactId>cloud-api-commons</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
3. 改YML
server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
4. 主啟動
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients //開啟OpenFeign public class OrderFeignMain80 { public static void main(String[] args) { SpringApplication.run(OrderFeignMain80.class,args); } }
@EnableFeignClients開啟OpenFeign
5. PaymentFeignService接口
import com.xiao.cloud.entities.CommonResult; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") // 使用OpenFeign public interface PaymentFeignService { @GetMapping("/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id); }
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")使用OpenFeign。
6. Controller類
import com.xiao.cloud.entities.CommonResult; import com.xiao.cloud.entities.Payment; import com.xiao.cloud.service.PaymentFeignService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @Slf4j public class PaymentFeignController { @Autowired private PaymentFeignService paymentFeignService; @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){ return paymentFeignService.getPaymentById(id); } }
7. 測試結(jié)果
當(dāng)我們多次點擊刷新時,端口在8001和8002之間依次變化。
8. 小總結(jié)
1.3、超時控制
1.3.1、是什么?
默認(rèn)Feign客戶端只等待一秒鐘,但是服務(wù)端處理需要超過1秒鐘,導(dǎo)致Feign客戶端不想等待了,直接返回報錯。為了避免這樣的情況,有時候我們需要設(shè)置Feign客戶端的超時控制。
在yml文件中開啟配置。
1.3.2、修改代碼設(shè)置超時錯誤
1. 修改支付模塊8001的Controller
添加以下代碼
@GetMapping(value = "/payment/feign/timeout") public String timeOUt(){ try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return ServerPort; }
2. 修改訂單模塊的PaymentFeignService
添加以下代碼
@GetMapping(value = "/payment/feign/timeout") public String timeOUt();
3. 修改訂單模塊的OrderFeignController
添加以下代碼
@GetMapping(value = "/consumer/payment/feign/timeout") public String timeOUt(){ return paymentFeignService.timeOUt(); }
4. 測試結(jié)果
直接對支付模塊8001的暴露的服務(wù)接口進行調(diào)用,測驗通過。
如果通過訂單模塊進行調(diào)用,那么就會報超時錯誤。
1.3.3、進行超時配置
在訂單模塊的YML文件中添加以下代碼
ribbon: ReadTimeout: 8000 ConnectTimeout: 8000
1. 測試結(jié)果
自測通過。
1.4、日志打印
1.4.1、是什么?
Feign
提供了日志打印功能,我們可以通過配置來調(diào)整日志級別,從而了解Feign
中Http
請求的細(xì)節(jié)。就是對feign
接口調(diào)用的情況進行監(jiān)控和輸出。
1.4.2、日志級別
- NONE:默認(rèn)的,不顯示任何日志
- BASIC:僅記錄請求方法、URL、響應(yīng)狀態(tài)碼及執(zhí)行時間
- HEADERS:除了BASIC中定義的信息之外,還有請求和響應(yīng)的頭信息
- FULL:除了HEADERS中定義的信息之外,還有請求和響應(yīng)的正文及元數(shù)據(jù)。
1.4.3、如何開啟日志打印
1. 編寫日志配置類
import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
2. 在YML文件中進行相關(guān)配置
server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka ribbon: ReadTimeout: 8000 ConnectTimeout: 8000 logging: level: com.atguigu.springcloud.service.PaymentFeignService: debug
3. 測試結(jié)果
4. 包結(jié)構(gòu)圖示
總結(jié)
到此這篇關(guān)于SpringCloud學(xué)習(xí)筆記之OpenFeign進行服務(wù)調(diào)用的文章就介紹到這了,更多相關(guān)SpringCloud OpenFeign服務(wù)調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud服務(wù)接口調(diào)用OpenFeign及使用詳解
- SpringCloud?openfeign聲明式服務(wù)調(diào)用實現(xiàn)方法介紹
- SpringCloud Ribbon與OpenFeign詳解如何實現(xiàn)服務(wù)調(diào)用
- SpringCloud?OpenFeign?服務(wù)調(diào)用傳遞?token的場景分析
- SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法
- Springcloud基于OpenFeign實現(xiàn)服務(wù)調(diào)用代碼實例
- SpringCloud中的openFeign調(diào)用服務(wù)并傳參的過程
相關(guān)文章
Springboot通用mapper和mybatis-generator代碼示例
這篇文章主要介紹了Springboot通用mapper和mybatis-generator代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12一文掌握J(rèn)ava開發(fā)工具Maven(簡單上手)
掌握maven的相關(guān)知識是Java開發(fā)必備的技能,今天通過本文從入門安裝開始,逐步深入講解maven的相關(guān)知識,包括maven的安裝到簡單上手maven項目開發(fā),感興趣的朋友跟隨小編一起看看吧2021-06-06springboot2.0如何通過fastdfs實現(xiàn)文件分布式上傳
這篇文章主要介紹了springboot2.0如何通過fastdfs實現(xiàn)文件分布式上傳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12Spring循環(huán)依賴正確性及Bean注入的順序關(guān)系詳解
這篇文章主要給大家介紹了關(guān)于Spring循環(huán)依賴的正確性,以及Bean注入的順序關(guān)系的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01