【SpringCloud】OpenFeign服務介面呼叫
OpenFeign服務介面呼叫
概述
OpenFeign是什麼
官網解釋:
https://cloud spring.io/spring -cloud static/Hoxton.SR1/reference/htmlsingle/#spring cloud openfeign
Feign是一個宣告式WebService客戶端。 使用Feign能讓編寫Web Service客戶端更加簡單。
它的使用方法是定義一個服務介面然後在上面添加註解。Feign也支 持可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支援了Spring MVC標準註解和HttpMessageConverters。 Feign可以與Eureka和Ribbon組合使用以支援負載均衡
Feign是一個宣告式的Web服務客戶端,讓編寫Web服務客戶端變得非常容易,只需#建立一個介面並在介面上添加註解即可
GitHub
https://github.com/spring-cloud/spring-cloud-openfeign
能幹嘛
Feign能幹什麼
Feign旨在使編寫Java Http客戶端變得更容易。
前面在使用Ribbon+ RestTemplate時,利用RestTemplate對http請求的封裝處理,形成了一套模版化的呼叫方法。 但是在實際開發中,由於對服務依賴的呼叫可能不止一處, 往往一個介面會被多處呼叫, 所以通常都會針對每個微服務自行封裝-些客戶端類來包裝這些依賴服務的呼叫。所以,Feign在此基礎上做了進-步封裝, 由他來幫助我們定義和實現依賴服務介面的定義。在Feign的實現下,我們只需建立一個介面並使用註解的方式來配置它(以前是Dao介面 上面標註Mapper註解,現在是一個微服務介面 上面標註一個Feign註解即可),即可完成對服務提供方的介面繫結,簡化了使用Spring cloud Ribbon時,自動封裝服務呼叫客戶端的開發量。
Feign集成了Ribbon
利用Ribbon維護了Payment的服務列表資訊,並且通過輪詢實現了客戶端的負載均衡。而與Ribbon不同的是,通過feign只需要定義服務繫結介面且以宣告式的方法,優雅而簡單的實現了服務呼叫
Feign和OpenFeign兩者區別
OpenFeign使用步驟
介面+註解
微服務呼叫介面+@FeignClient
新建cloud-consumer-feign-order80
Feign在消費端使用
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>cloud2020</artifactId> <groupId>com.atguigu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-feign-order80</artifactId> <description>訂單消費者之feign</description> <dependencies> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</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>
YML
server:
port: 80
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
主啟動
@EnableFeignClients
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author zzyy
* @date 2020/02/18 17:20
**/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class, args);
}
}
業務類
業務邏輯介面+@FeignClient配置呼叫provider服務
新建PaymentFeignService介面並新增註解@FeignClient
@FeignClient
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value="/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
控制層Controller
測試
先啟動2個eureka叢集7001/7002
再啟動2個微服務8001/8002
啟動OpenFeign
http://localhost/consumer/payment/get/31
Feign自帶負載均衡配置項
小總結
OpenFeign超時控制
超時設定,故意設定超時演示出錯情況
服務提供方8001故意寫暫停程式
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (Exception e){
e.printStackTrace();
} finally {
return serverPort;
}
}
服務消費方80新增超時方法PaymentFeignService
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout();
服務消費方80新增超時方法OrderFeignController
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout() {
//openfeign-ribbon 客戶端預設等待1S
return paymentFeignService.paymentFeignTimeout();
}
測試
http://localhost/consumer/payment/feign/timeout
錯誤頁面
OpenFeign預設等待1秒鐘,超過後報錯
是什麼
預設Felign客戶端只等待一秒鐘,但是服務端處理需要超過1秒鐘,導致Feign客戶端不想等待了,直接返回報錯。
為了避免這樣的情況,有時候我們需要設定Feign客戶端的超時控制。
yml檔案中開啟配置
OpenFeign預設支援Ribbon
YML檔案裡需要開啟OpenFeign客戶端超時控制
server:
port: 80
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# 設定feign客戶端超時時間(OpenFeign預設支援ribbon)
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;
/**
* OpenFeignClient配置
*
* @author zzyy
* @create 2020/3/6 18:02
**/
@Configuration
public class FeignConfig {
/**
* feignClient配置日誌級別
*
* @return
*/
@Bean
public Logger.Level feignLoggerLevel() {
// 請求和響應的頭資訊,請求和響應的正文及元資料
return Logger.Level.FULL;
}
}
YML檔案裡需要開啟日誌的Feign客戶端
server:
port: 80
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# 設定feign客戶端超時時間(OpenFeign預設支援ribbon)
ribbon:
# 指的是建立連線所用的時間,適用於網路狀態正常的情況下,兩端連線所用的時間
ReadTimeout: 5000
# 指的是建立連線後從伺服器讀取到可用資源所用的時間
ConnectTimeout: 5000
logging:
level:
# feign日誌以什麼級別監控哪個介面
com.atguigu.springcloud.service.PaymentFeignService: debug