微服务feignclient_[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用
上一節(jié)我們使用了Ribbon(基于Http/Tcp)進行微服務(wù)的調(diào)用,Ribbon的調(diào)用比較簡單,通過Ribbon組件對請求的服務(wù)進行攔截,通過Eureka Server 獲取到服務(wù)實例的IP:Port,然后再去調(diào)用API。本節(jié)課我們使用更簡單的方式來實現(xiàn),使用聲明式的Web服務(wù)客戶端Feign,我們只需要使用Feign來聲明接口,利用注解來進行配置就可以使用了,是不是很簡單?實際工作中,我們也只會用到Feign來進行服務(wù)之間的調(diào)用(大多數(shù))。接下來,我們來實例操作一把。
為了代碼的重用性,我們來創(chuàng)建一個新的project mscx-ad-feign-sdk作為Feign的服務(wù)調(diào)用工具。
創(chuàng)建項目mscx-ad-feign-sdk
三部曲之Step 1(加依賴)
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">
mscx-ad
com.sxzhongf
1.0-SNAPSHOT
4.0.0
jar
mscx-ad-feign-sdk
只定義微服務(wù)Feign調(diào)用用到的請求對象和響應(yīng)對象,而不涉及具體的實現(xiàn)類。
com.sxzhongf
mscx-ad-feign-sdk
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
com.sxzhongf
mscx-ad-common
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-hystrix
1.2.7.RELEASE
org.springframework.boot
spring-boot-maven-plugin
三部曲之Step 2(加注解@EnableFeignClients,添加在具體的微服務(wù)中,使用我們自定義的FeignClient)
/**
* ISponsorFeignClient for service using
*
* @author Isaac.Zhang | 若初
*/
@FeignClient(value = "mscx-ad-sponsor", fallback = SponsorClientHystrix.class)
public interface ISponsorFeignClient {
@RequestMapping(value = "/ad-sponsor/plan/get", method = RequestMethod.POST)
CommonResponse> getAdPlansUseFeign(@RequestBody AdPlanGetRequestVO requestVO);
@RequestMapping(value = "/ad-sponsor/user/get", method = RequestMethod.GET)
/**
* Feign 埋坑之 如果是Get請求,必須在所有參數(shù)前添加{@link RequestParam},不能使用{@link Param}
* 會被自動轉(zhuǎn)發(fā)為POST請求。
*/
CommonResponse getUsers(@RequestParam(value = "username") String username);
}
@GetMapping(path = "/user/get")
public CommonResponse getUsers(@Param(value = "username") String username) {
log.info("ad-search::getUsersFeign -> {}", JSON.toJSONString(username));
CommonResponse commonResponse = sponsorFeignClient.getUsers(username);
return commonResponse;
}
}
三部曲之Step 3(加配置,工具類庫不需要,添加在具體的微服務(wù)中)
我們上面的實例中有一個問題,如果說我們的廣告提供服務(wù)出現(xiàn)了問題,那么我們通過使用FeignClient 調(diào)用的APIsponsorFeignClient.getUsers(username);就會報錯,如果長時間報錯,會引起大規(guī)模的服務(wù)錯誤問題,也就有是我們常說的服務(wù)雪崩效應(yīng),我們要怎樣避免一個服務(wù)出錯而拖垮整個系統(tǒng)的問題呢?這里我們需要引入一個組件Hystrix來處理服務(wù)錯誤。
三部曲之Step1(加依賴)
從上圖我們可以看到,我們引入Feign依賴的時候,它本身已經(jīng)依賴了Hystrix,根據(jù)Maven依賴的傳遞性,我們可以知道我們自己的服務(wù)已經(jīng)包含了Hystrix的依賴支持,我們可以直接使用了~
三部曲之Step2(加注解) @EnableHystrix // 開啟hystrix 斷路器
三部曲之Step3(改配置)
feign:
hystrix:
enabled: true
使用Hystrix來配置Feign實現(xiàn)調(diào)用容錯
@Component
public class SponsorClientHystrix implements ISponsorFeignClient {
@Override
public CommonResponse> getAdPlansUseFeign(AdPlanGetRequestVO requestVO) {
return new CommonResponse<>(-1, "mscx-ad-sponsor feign & hystrix get plan error.");
}
@Override
public CommonResponse getUsers(String username) {
return new CommonResponse<>(-1, "mscx-ad-sponsor feign & hystrix get user error.");
}
}
在ISponsorFeignClient類中,添加出錯處理類(fallback)
@FeignClient(value = "mscx-ad-sponsor", fallback = SponsorClientHystrix.class)
public interface ISponsorFeignClient {
...
在SponsorClientHystrix中,我們要特別注意2點
該類必須添加@Component注解,以便可以加入Spring 容器中
該類需要實現(xiàn)ISponsorFeignClientFeign的客戶端接口
通過上面的實現(xiàn),我們的服務(wù)在調(diào)用過程中,如果發(fā)生錯誤,就會進行服務(wù)降級,調(diào)用到出錯應(yīng)該調(diào)用的默認(rèn)處理類中的方法,也就實現(xiàn)了我們想要做的短路處理來保護我們的當(dāng)前服務(wù)。
總結(jié)
以上是生活随笔為你收集整理的微服务feignclient_[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日本显示面板厂商 JOLED 宣布破产,
- 下一篇: melis系统c800_一种基于Meli