OpenFeign是什麼
官方文檔
Github位址
Feign是一個聲明式WebService用戶端。使用Feign能讓編寫Web Service用戶端更加簡單。它的使用方法是定義一個服務接口然後在上面添加注解。Feign也支援可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支援了Spring MVC标準注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支援負載均衡。
Feign能幹什麼
Feign旨在使編寫Java Http用戶端變得更容易。
前面在使用Ribbon+RestTemplate時,利用RestTemplate對http請求的封裝處理,形成了一套模版化的調用方法。但是在實際開發中,由于對服務依賴的調用可能不止一處,往往一個接口會被多處調用,是以通常都會針對每個微服務自行封裝一些用戶端類來包裝這些依賴服務的調用。是以,Feign在此基礎上做了進一步封裝,由他來幫助我們定義和實作依賴服務接口的定義。在Feign的實作下,我們隻需建立一個接口并使用注解的方式來配置它(以前是Dao接口上面标注Mapper注解,現在是一個微服務接口上面标注一個Feign注解即可),即可完成對服務提供方的接口綁定,簡化了使用Spring cloud Ribbon時,自動封裝服務調用用戶端的開發量。
Feign內建了Ribbon
利用Ribbon維護了Payment的服務清單資訊,并且通過輪詢實作了用戶端的負載均衡。而與Ribbon不同的是,通過feign隻需要定義服務綁定接口且以聲明式的方法,優雅而簡單的實作了服務調用。
Feign和OpenFeign兩者差別
Feign是Spring Cloud元件中的一個輕量級RESTful的HTTP服務用戶端Feign内置了Ribbon,用來做用戶端負載均衡,去調用服務注冊中心的服務。Feign的使用方式是:使用Feign的注解定義接口,調用這個接口,就可以調用服務注冊中心的服務。
OpenFeign是Spring Cloud在Feign的基礎上支援了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@Feignclient可以解析SpringMVc的@RequestMapping注解下的接口,并通過動态代理的方式産生實作類,實作類中做負載均衡并調用其他服務。
OpenFeign服務調用
接口+注解:微服務調用接口 + @FeignClient
1.建立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>cloud2021</artifactId>
<groupId>com.ylc.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-order80</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<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-client</artifactId>
</dependency>
<!-- 引入自己定義的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.ylc.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--web-->
<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.主啟動類
package com.ylc.cloud;
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.業務類
services
package com.ylc.cloud.services;
import com.ylc.cloud.entities.CommonResult;
import com.ylc.cloud.entities.Payment;
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<Payment> getPaymentById(@PathVariable("id") Long id);
}
controller
package com.ylc.cloud.controller;
import com.ylc.cloud.entities.CommonResult;
import com.ylc.cloud.entities.Payment;
import com.ylc.cloud.services.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
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);
}
}
6.測試
先啟動2個eureka叢集7001/7002 再啟動2個微服務8001/8002
啟動OpenFeign啟動:http://localhost/consumer/payment/get/1
Feign自帶負載均衡配置項
OpenFeign逾時控制
OpenFeign預設等待1秒鐘,超過後報錯,那麼模拟逾時,故意設定逾時示範出錯情況
在
1.cloud-provider-payment8001
服務提供方8001/8002故意寫暫停程式
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout()
{
// 業務邏輯處理正确,但是需要耗費3秒鐘
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return serverPort;
}
2.服務消費方80添加逾時方法PaymentFeignService
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService{
...
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout();
}
3.服務消費方80添加逾時方法OrderFeignController
@RestController
@Slf4j
public class OrderFeignController
{
@Resource
private PaymentFeignService paymentFeignService;
...
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout()
{
// OpenFeign用戶端一般預設等待1秒鐘
return paymentFeignService.paymentFeignTimeout();
}
}
4.測試:
多次重新整理http://localhost/consumer/payment/feign/timeout
将會跳出錯誤Spring Boot預設錯誤頁面,主要異常:feign.RetryableException:Read timed out executing GET http://CLOUD-PAYMENT-SERVCE/payment/feign/timeout。
OpenFeign預設等待1秒鐘,超過後報錯
YML檔案裡需要開啟OpenFeign用戶端逾時控制
#設定feign用戶端逾時時間(OpenFeign預設支援ribbon)(機關:毫秒)
ribbon:
#指的是建立連接配接所用的時間,适用于網絡狀況正常的情況下,兩端連接配接所用的時間
ReadTimeout: 5000
#指的是建立連接配接後從伺服器讀取到可用資源所用的時間
ConnectTimeout: 5000
OpenFeign日志增強
日志列印功能
Feign提供了日志列印功能,我們可以通過配置來調整日恙級别,進而了解Feign 中 Http請求的細節。
說白了就是對Feign接口的調用情況進行監控和輸出
日志級别
- NONE:預設的,不顯示任何日志;
- BASIC:僅記錄請求方法、URL、響應狀态碼及執行時間;
- HEADERS:除了BASIC中定義的資訊之外,還有請求和響應的頭資訊;
- FULL:除了HEADERS中定義的資訊之外,還有請求和響應的正文及中繼資料。
配置日志bean
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用戶端
logging:
level:
# feign日志以什麼級别監控哪個接口
com.ylc.cloud.services.PaymentFeignService: debug