微服务feignclient_[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用
上一節我們使用了Ribbon(基于Http/Tcp)進行微服務的調用,Ribbon的調用比較簡單,通過Ribbon組件對請求的服務進行攔截,通過Eureka Server 獲取到服務實例的IP:Port,然后再去調用API。本節課我們使用更簡單的方式來實現,使用聲明式的Web服務客戶端Feign,我們只需要使用Feign來聲明接口,利用注解來進行配置就可以使用了,是不是很簡單?實際工作中,我們也只會用到Feign來進行服務之間的調用(大多數)。接下來,我們來實例操作一把。
為了代碼的重用性,我們來創建一個新的project mscx-ad-feign-sdk作為Feign的服務調用工具。
創建項目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
只定義微服務Feign調用用到的請求對象和響應對象,而不涉及具體的實現類。
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,添加在具體的微服務中,使用我們自定義的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請求,必須在所有參數前添加{@link RequestParam},不能使用{@link Param}
* 會被自動轉發為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(加配置,工具類庫不需要,添加在具體的微服務中)
我們上面的實例中有一個問題,如果說我們的廣告提供服務出現了問題,那么我們通過使用FeignClient 調用的APIsponsorFeignClient.getUsers(username);就會報錯,如果長時間報錯,會引起大規模的服務錯誤問題,也就有是我們常說的服務雪崩效應,我們要怎樣避免一個服務出錯而拖垮整個系統的問題呢?這里我們需要引入一個組件Hystrix來處理服務錯誤。
三部曲之Step1(加依賴)
從上圖我們可以看到,我們引入Feign依賴的時候,它本身已經依賴了Hystrix,根據Maven依賴的傳遞性,我們可以知道我們自己的服務已經包含了Hystrix的依賴支持,我們可以直接使用了~
三部曲之Step2(加注解) @EnableHystrix // 開啟hystrix 斷路器
三部曲之Step3(改配置)
feign:
hystrix:
enabled: true
使用Hystrix來配置Feign實現調用容錯
@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 容器中
該類需要實現ISponsorFeignClientFeign的客戶端接口
通過上面的實現,我們的服務在調用過程中,如果發生錯誤,就會進行服務降級,調用到出錯應該調用的默認處理類中的方法,也就實現了我們想要做的短路處理來保護我們的當前服務。
總結
以上是生活随笔為你收集整理的微服务feignclient_[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日本显示面板厂商 JOLED 宣布破产,
- 下一篇: melis系统c800_一种基于Meli