OpenFeign 服務介面呼叫
一、概述
1、是什麼
Feign 是一個宣告式的web服務客戶端,讓編寫web服務客戶端變得非常容易,只需建立一個介面並在介面上添加註解即可。
2、能幹什麼
Feign 旨在使編寫 Java Http 客戶端變得更容易。
前面在使用 Ribbon+RestTemplate 時,利用 RestTemplate 對 http 請求的封裝處理,形成了一套模版化的呼叫方法。但是在實際開發中,由於對服務依賴的呼叫可能不止一處,往往一個接囗會被多處呼叫,所以通常都會針對每個微服務自行封裝一些客戶端類來包裝這些依賴服務的呼叫。所以,Feign 在此基礎上做了進一步封裝,由他來幫助我們定義和實現依賴服務介面的定義。在 Feign 的實現下,我們只需建立一個介面並使用註解的方式來配置它(以前是Dao接囗上面標註Mapper註解,現在是一個微服務接囗上面標汪一個@Feign註解即可),即可完成對服務提供方的介面繫結,簡化了使用Springcloud Ribbon 時,自動封裝服務呼叫客戶端的開發量。
Feign 集成了 Ribbon
利用Ribbon維護了Payment的服務列表資訊,並且通過輪詢實現了客戶端的負載均衡。而與Ribbon不同的是,通過feign只需要定義服務繫結接囗且以宣告式的方法,優雅而簡單的實現了服務呼叫
3、Feign和OpenFeign兩者區別
二、OpenFeign使用步驟
1)新建cloud-consumer-feign-order80,Feign在消費端使用
2)改POM檔案
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"pom檔案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>cloud2020</artifactId> <groupId>com.atguigu.springcloud</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.atguigu.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</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/eurekayml檔案
4)啟動類
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class OrderFeignMain80 { public static void main(String[] args) { SpringApplication.run(OrderFeignMain80.class,args); } }
5)業務邏輯介面+@FeignClient配置呼叫provider服務
新建PaymentFeignService介面並新增註解@FeignClient
package com.atguigu.springcloud.service; import com.atguigu.springcloud.entities.CommonResult; import com.atguigu.springcloud.entities.Payment; import feign.Param; 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") public interface PaymentFeignService { @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id); }
6)控制層Controller
package com.atguigu.springcloud.controller; import com.atguigu.springcloud.entities.CommonResult; import com.atguigu.springcloud.entities.Payment; import com.atguigu.springcloud.service.PaymentFeignService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class OrderFeignController { @Resource private PaymentFeignService paymentFeignService; @GetMapping(value = "/consumer/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){ return paymentFeignService.getPaymentById(id); } }
7)測試
- 先啟動2個eureka叢集7001/7002
- 再啟動2個微服務8001/8002
- 啟動OpenFeign啟動
- http://localhost/consumer/payment/get/31
- Feign自帶負載均衡配置項
8)小總結
三、OpenFeign超時控制
超時設定,故意設定超時演示出錯情況
服務提供方8001故意寫暫停程式
@GetMapping(value = "/payment/feign/timeout") public String paymentFeignTimeout() { try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } return "8002"; }
服務消費方80新增超時方法PaymentFeignService
@GetMapping(value = "/payment/feign/timeout") public String paymentFeignTimeout();
服務消費方80新增超時方法OrderFeignController
@GetMapping(value = "/consumer/payment/feign/timeout") public String paymentFeignTimeout(){ return paymentFeignService.paymentFeignTimeout(); }
測試:http://localhost/consumer/payment/feign/timeout
OpenFeign預設等待一秒鐘,超過後報錯
預設Feign客戶端只等待一秒鐘,但是服務端處理需要超過1秒鐘,導致Feign客戶端不想等待了,直接返回報錯。為了避免這樣的情況,有時候我們需要設定Feign客戶端的超時控制。
yml檔案中開啟配置
server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka ribbon: ReadTimeout: 5000 ConnectTimeout: 5000
四、OpenFeign日誌列印功能
Feign提供了日誌列印功能,我們可通過配置來調整日誌級別,從而瞭解Feign中Http請求的細節。說白了就是對Feign接囗的呼叫情況進行監控和輸出
日誌級別
- NONE:預設的,不顯示任何日誌。
- BASIC:僅記錄請求方法、URL、響應狀態碼及執行時間。
- HEADERS:除了BASIC中定義的資訊之外,還有請求和響應的頭資訊。
- FULL:除了HEADERS中定義的資訊之外,還有請求和響應的正文及元資料。
配置日誌bean
package com.atguigu.springcloud.config; 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; } }
YML檔案裡需要開啟日誌的Feign客戶端
server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka ribbon: ReadTimeout: 5000 ConnectTimeout: 5000 logging: level: com.atguigu.springcloud.service.PaymentFeignService: debug