OpenFeign服務接口調用的過程詳解
OpenFeign服務接口調用
1、概述
Feign是一個聲明式WebService客戶端。使用Feign能讓編寫Web Service客戶端更加簡單。它的使用方法是定義一個服務接口然后在上面添加注解。
Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標準注解和HttpMessageConverters。
Feign可以與Eureka和Ribbon組合使用以支持負載均衡
源碼地址:https://github.com/spring-cloud/spring-cloud-openfeign
Feign能干什么
Feign旨在使編寫Java Http客戶端變得更容易。前面在使用Ribbon+RestTemplate時,利用RestTemplate對http請求的封裝處理,形成了一套模版化的調用方法。但是在實際開發(fā)中,由于對服務依賴的調用可能不止一處,往往一個接口會被多處調用,所以通常都會針對每個微服務自行封裝一些客戶端類來包裝這些依賴服務的調用。所以,F(xiàn)eign在此基礎上做了進一步封裝,由他來幫助我們定義和實現(xiàn)依賴服務接口的定義。在Feign的實現(xiàn)下,我們只需創(chuàng)建一個接口并使用注解的方式來配置它(以前是Dao接口上面標注Mapper注解,現(xiàn)在是一個微服務接口上面標注一個Feign注解即可),即可完成對服務提供方的接口綁定,簡化了使用Spring cloud Ribbon時,自動封裝服務調用客戶端的開發(fā)量。
Feign集成了Ribbon
利用Ribbon維護了Payment的服務列表信息,并且通過輪詢實現(xiàn)了客戶端的負載均衡。而與Ribbon不同的是,通過feign只需要定義服務綁定接口且以聲明式的方法,優(yōu)雅而簡單的實現(xiàn)了服務調用
Feign和OpenFeign兩者的區(qū)別
Feign | OpenFeign |
---|---|
Feign是Spring Cloud組件中的一個輕量級RESTful的HTTP服務客戶端 Feign內置了Ribbon,用來做客戶端負載均衡,去調用服務注冊中心的服務。Feign的使用方式是:使用Feign的注解定義接口,調用這個接口,就可以調用服務注冊中心的服務 | OpenFeign是Spring Cloud 在Feign的基礎上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通過動態(tài)代理的方式產生實現(xiàn)類,實現(xiàn)類中做負載均衡并調用其他服務。 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> | <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> |
2、OpenFeign使用步驟
1、創(chuàng)建接口和使用注解@FeignClient
2、新建模塊cloud-consumer-feign-order80
3、修改pom.xml依賴文件
<?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>springcloud2022</artifactId> <groupId>com.zcl.springcloud</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.zcl.springcloud</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>
4、添加YAML配置文件
不將模塊注冊到Eureka服務中心
server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
5、創(chuàng)建啟動類
既然使用的是
OpenFeign
那必須使用@EnableFeignClients
注解進行開啟
package com.zcl.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; /** * 描述:Feign項目啟動類 * * @author zhong * @date 2022-09-19 13:32 */ @SpringBootApplication @EnableFeignClients public class OrderFeignMain80 { public static void main(String[] args) { SpringApplication.run(OrderFeignMain80.class, args); } }
6、業(yè)務類
業(yè)務邏輯接口+
@FeignClient
注解完成服務調用,找指定微服務的實例和調用地址,訪問的就是8001對外暴露的地址主啟動類上必須使用
@EnableFeignClients
注解進行激活開啟
package com.zcl.springcloud.service; import com.zcl.springcloud.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; /** * 描述:Feign業(yè)務測試接口 * * @author zhong * @date 2022-09-19 13:35 */ @Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentFeignService { /** * 根據id去調用 * @param id * @return */ @GetMapping("/payment/get/{id}") CommonResult getPaymentById(@PathVariable("id") Long id); }
上面定義的接口其實就是
服務提供者
對外暴露的接口
7、創(chuàng)建控制器
package com.zcl.springcloud.controller; import com.zcl.springcloud.entities.CommonResult; import com.zcl.springcloud.entities.Payment; import com.zcl.springcloud.service.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; /** * 描述:測試控制器 * * @author zhong * @date 2022-09-19 14:11 */ @Slf4j @RestController public class OrderFeignController { /** * 注入Feign業(yè)務接口 */ @Resource private PaymentFeignService paymentFeignService; /** * 測試調用服務接口 * @param id * @return */ @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPayment(@PathVariable("id") Long id) { // 調用服務接口 return paymentFeignService.getPaymentById(id); } }
8、啟動項目測試
- 先啟動7001、7002Eureka集群
- 再啟動8001、8002兩個微服務提供者
- 啟動OpenFeign項目80
- 調用接口訪問
http://localhost/consumer/payment/get/1
多請求兩回會發(fā)現(xiàn)一樣的有輪詢負載均衡的效果
Feign自帶負載均衡配置項
{ "code": 200, "message": "查詢成功,當前提供者端口號為:8002", "data": { "id": 1, "serial": "1515154151515" } }
3、OpenFeign超時控制
1、在8001提供者上添加一個新的控制器
/** * 測試Feign程序訪問停止三秒 * @return */ @GetMapping("/feign/timeout") public String paymentFeignTimeOut(){ try { // 程序停止三秒 TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return serverPort; }
2、在消費者Feign80上添加接口
package com.zcl.springcloud.service; import com.zcl.springcloud.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; /** * 描述:Feign業(yè)務測試接口 * * @author zhong * @date 2022-09-19 13:35 */ @Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentFeignService { /** * 測試請求三秒不響應接口 * @return */ @GetMapping("/payment/feign/timeout") String paymentFeignTimeOut(); }
3、在FeignOrder80消費者控制器上添加接口訪問
/** * 注入Feign業(yè)務接口 */ @Resource private PaymentFeignService paymentFeignService; /** * 測試超時 * @return */ @GetMapping("/payment/feign/timeout") public String paymentFeignTimeOut() { return paymentFeignService.paymentFeignTimeOut(); }
4、啟動項目測試
- 先自測訪問提供者的業(yè)務是否正常:http://localhost:8001/payment/feign/timeout(等待三秒再響應結果)
- 通過Feign-Order80消費者的接口測試調用提供者的訪問是否正常:http://localhost/consumer/payment/feign/timeout
默認連接是1秒鐘,程序設置了三秒導致連接超時
后端超時報錯
5、在YAML配置文件中開啟超時連接
設置配置文件下面的
ribbon
連接時間后等待三秒訪問正常不會再報錯
server: port: 80 eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ #設置feign客戶端超時時間(OpenFeign默認支持ribbon) ribbon: #指的是建立連接所用的時間,適用于網絡狀況正常的情況下,兩端連接所用的時間 ReadTimeout: 5000 #指的是建立連接后從服務器讀取到可用資源所用的時間 ConnectTimeout: 5000
4、OpenFeign日志打印功能
Feign 提供了日志打印功能,我們可以通過配置來調整日志級別,從而了解 Feign 中 Http 請求的細節(jié)。
就是對Feign接口的調用情況進行監(jiān)控和輸出
1、日記級別
NONE:默認的,不顯示任何日志;
BASIC:僅記錄請求方法、URL、響應狀態(tài)碼及執(zhí)行時間;
HEADERS:除了 BASIC 中定義的信息之外,還有請求和響應的頭信息;
FULL:除了 HEADERS 中定義的信息之外,還有請求和響應的正文及元數(shù)據。
2、配置日記級別配置類
注意不要導錯包
返回的日記級別對應上面的4個
package com.zcl.springcloud.config; import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 描述:Feign日記級別配置 * * @author zhong * @date 2022-09-19 15:27 */ @Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
3、YAML配置文件開啟日記信息打印
logging: level: # feign日志以什么級別監(jiān)控哪個接口 com.zcl.springcloud.service.PaymentFeignService: debug
啟動羨慕測試輸出內容
到此這篇關于OpenFeign服務接口調用的文章就介紹到這了,更多相關OpenFeign接口調用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring boot + jpa + kotlin入門實例詳解
這篇文章主要介紹了spring boot + jpa + kotlin入門實例詳解 ,需要的朋友可以參考下2017-07-07java使用Nagao算法實現(xiàn)新詞發(fā)現(xiàn)、熱門詞的挖掘
這篇文章主要介紹了java使用Nagao算法實現(xiàn)新詞發(fā)現(xiàn)、熱門詞的挖掘的思路和詳細代碼,需要的朋友可以參考下2015-07-07實戰(zhàn)分布式醫(yī)療掛號通用模塊統(tǒng)一返回結果異常日志處理
這篇文章主要為大家介紹了實戰(zhàn)分布式醫(yī)療掛號系統(tǒng)之統(tǒng)一返回結果統(tǒng)一異常處理,統(tǒng)一日志處理到通用模塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-04-04