詳解OpenFeign服務(wù)調(diào)用(微服務(wù))_第1頁
詳解OpenFeign服務(wù)調(diào)用(微服務(wù))_第2頁
詳解OpenFeign服務(wù)調(diào)用(微服務(wù))_第3頁
詳解OpenFeign服務(wù)調(diào)用(微服務(wù))_第4頁
詳解OpenFeign服務(wù)調(diào)用(微服務(wù))_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第詳解OpenFeign服務(wù)調(diào)用(微服務(wù))目錄OpenFeign服務(wù)調(diào)用OpenFeign是什么Feign能干什么Feign集成了RibbonFeign和OpenFeign兩者區(qū)別OpenFeign服務(wù)調(diào)用OpenFeign超時控制OpenFeign日志增強

OpenFeign服務(wù)調(diào)用

OpenFeign是什么

官方文檔

Github地址

Feignisadeclarativewebserviceclient.Itmakeswritingwebserviceclientseasier.TouseFeigncreateaninterfaceandannotateit.IthaspluggableannotationsupportincludingFeignannotationsandJAX-RSannotations.Feignalsosupportspluggableencodersanddecoders.SpringCloudaddssupportforSpringMVCannotationsandforusingthesameHttpMessageConvertersusedbydefaultinSpringWeb.SpringCloudintegratesRibbonandEureka,aswellasSpringCloudLoadBalancertoprovideaload-balancedhttpclientwhenusingFeign.link

Feign是一個聲明式WebService客戶端。使用Feign能讓編寫WebService客戶端更加簡單。它的使用方法是定義一個服務(wù)接口然后在上面添加注解。Feign也支持可拔插式的編碼器和解碼器。SpringCloud對Feign進行了封裝,使其支持了SpringMVC標(biāo)準(zhǔn)注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。

Feign能干什么

Feign旨在服務(wù)調(diào)用時,使編寫JavaHttp客戶端變得更容易。

前面在使用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ù)提供方的接口綁定,簡化了使用SpringcloudRibbon時,自動封裝服務(wù)調(diào)用客戶端的開發(fā)量。

Feign集成了Ribbon

利用Ribbon維護了Payment的服務(wù)列表信息,并且通過輪詢實現(xiàn)了客戶端的負載均衡。而與Ribbon不同的是,通過feign只需要定義服務(wù)綁定接口且以聲明式的方法,優(yōu)雅而簡單的實現(xiàn)了服務(wù)調(diào)用。

Feign和OpenFeign兩者區(qū)別

Feign是SpringCloud組件中的一個輕量級RESTful的HTTP服務(wù)客戶端Feign內(nèi)置了Ribbon,用來做客戶端負載均衡,去調(diào)用服務(wù)注冊中心的服務(wù)。Feign的使用方式是:使用Feign的注解定義接口,調(diào)用這個接口,就可以調(diào)用服務(wù)注冊中心的服務(wù)。

dependency

groupIdorg.springframework.cloud/groupId

artifactIdspring-cloud-starter-feign/artifactId

/dependency

OpenFeign是SpringCloud在Feign的基礎(chǔ)上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@Feignclient可以解析SpringMVc的@RequestMapping注解下的接口,并通過動態(tài)代理的方式產(chǎn)生實現(xiàn)類,實現(xiàn)類中做負載均衡并調(diào)用其他服務(wù)。

dependency

groupIdorg.springframework.cloud/groupId

artifactIdspring-cloud-starter-openfeign/artifactId

/dependency

OpenFeign服務(wù)調(diào)用

接口+注解:微服務(wù)調(diào)用接口+@FeignClient

1.新建cloud-consumer-feign-order80

2.POM

xmlversion="1.0"encoding="UTF-8"

projectxmlns="/POM/4.0.0"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0/xsd/maven-4.0.0.xsd"

parent

artifactIdLearnCloud/artifactId

groupIdcom.lun.springcloud/groupId

version1.0.0-SNAPSHOT/version

/parent

modelVersion4.0.0/modelVersion

artifactIdcloud-consumer-feign-order80/artifactId

dependencies

!--openfeign--

dependency

groupIdorg.springframework.cloud/groupId

artifactIdspring-cloud-starter-openfeign/artifactId

/dependency

!--eurekaclient--

dependency

groupIdorg.springframework.cloud/groupId

artifactIdspring-cloud-starter-netflix-eureka-client/artifactId

/dependency

!--引入自己定義的api通用包,可以使用Payment支付Entity--

dependency

groupIdcom.lun.springcloud/groupId

artifactIdcloud-api-commons/artifactId

version${project.version}/version

/dependency

!--web--

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-web/artifactId

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-actuator/artifactId

/dependency

!--一般基礎(chǔ)通用配置--

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-devtools/artifactId

scoperuntime/scope

optionaltrue/optional

/dependency

dependency

groupIdjectlombok/groupId

artifactIdlombok/artifactId

optionaltrue/optional

/dependency

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-starter-test/artifactId

scopetest/scope

/dependency

/dependencies

/project

3.YML

server:

port:80

eureka:

client:

register-with-eureka:false

service-url:

defaultZone:http://localhost:7001/eureka,http://localhost:7002/eureka

4.主啟動

packagecn.kt.springcloud;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.cloud.openfeign.EnableFeignClients;

*Createdbytao.

*Date:2025/7/623:01

*描述:

@SpringBootApplication

@EnableFeignClients//開啟feign客戶端

publicclassOrderFeignMain80{

publicstaticvoidmain(String[]args){

SpringApplication.run(OrderFeignMain80.class,args);

}

5.業(yè)務(wù)類

業(yè)務(wù)邏輯接口+@FeignClient配置調(diào)用provider服務(wù)

新建PaymentFeignService接口并新增注解@FeignClient

importcom.lun.springcloud.entities.CommonResult;

importcom.lun.springcloud.entities.Payment;

importorg.springframework.cloud.openfeign.FeignClient;

importorg.springframework.stereotype.Component;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.PathVariable;

@Component

@FeignClient(value="CLOUD-PAYMENT-SERVICE")

publicinterfacePaymentFeignService

@GetMapping(value="/payment/get/{id}")

publicCommonResultPaymentgetPaymentById(@PathVariable("id")Longid);

}

控制層Controller

packagecn.kt.springcloud.controller;

importcn.kt.springcloud.entities.CommonResult;

importcn.kt.springcloud.entities.Payment;

importcn.kt.springcloud.service.PaymentFeignService;

importlombok.extern.slf4j.Slf4j;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.PathVariable;

importorg.springframework.web.bind.annotation.RestController;

importjavax.annotation.Resource;

*Createdbytao.

*Date:2025/7/623:07

*描述:

@RestController

@Slf4j

publicclassOrderFeignController{

@Resource

privatePaymentFeignServicepaymentFeignService;

@GetMapping(value="/consumer/payment/get/{id}")

publicCommonResultPaymentgetPaymentById(@PathVariable("id")Longid){

returnpaymentFeignService.getPaymentById(id);

}

6.測試

先啟動2個eureka集群7001/7002

再啟動2個微服務(wù)8001/8002

啟動OpenFeign80啟動

http://localhost/consumer/payment/get/31

Feign自帶負載均衡配置項

配置對應(yīng)關(guān)系如下圖:

OpenFeign超時控制

超時設(shè)置,故意設(shè)置超時演示出錯情況

1.服務(wù)提供方8001/8002故意寫暫停程序

@RestController

@Slf4j

publicclassPaymentController{

@Value("${server.port}")

privateStringserverPort;

@GetMapping(value="/payment/feign/timeout")

publicStringpaymentFeignTimeout()

//業(yè)務(wù)邏輯處理正確,但是需要耗費3秒鐘

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

returnserverPort;

2.服務(wù)消費方80添加超時方法PaymentFeignService

@Component

@FeignClient(value="CLOUD-PAYMENT-SERVICE")

publicinterfacePaymentFeignService{

@GetMapping(value="/payment/feign/timeout")

publicStringpaymentFeignTimeout();

}

3.服務(wù)消費方80添加超時方法OrderFeignController

@RestController

@Slf4j

publicclassOrderFeignController

@Resource

privatePaymentFeignServicepaymentFeignService;

@GetMapping(value="/consumer/payment/feign/timeout")

publicStringpaymentFeignTimeout()

//OpenFeign客戶端一般默認等待1秒鐘

returnpaymentFeignService.paymentFeignTimeout();

}

4.測試:

多次刷新:http://localhost/consumer/payment/feign/timeout

將會跳出錯誤SpringBoot默認錯誤頁面,主要異常:feign.RetryableException:ReadtimedoutexecutingGEThttp://CLOUD-PAYMENT-SERVCE/payment/feign/timeout。

OpenFeign默認等待1秒鐘,超過后報錯

YML文件里需要開啟OpenFeign客戶端超時控制

#設(shè)置feign客戶端超時時間(OpenFeign默認支持ribbon)(單位:毫秒)

ribbon:

#指的是建立連接所用的時間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所用的時間

ReadTimeout:5000

#指的是建立連接后從服務(wù)器讀取到可用資源所

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論