SpringCloud基于Feign實現(xiàn)遠程調(diào)用的問題小結(jié)
Spring Cloud 是一個用于構(gòu)建分布式系統(tǒng)的開發(fā)工具包,它提供了一系列的微服務(wù)組件,其中之一就是 Feign。Feign 是一種聲明式的 Web 服務(wù)客戶端,它簡化了在 Spring Cloud 中進行遠程調(diào)用的過程。本文將介紹如何在 Spring Cloud 中使用 Feign 進行遠程調(diào)用。
一、引入Feign依賴
我們在 Spring Cloud 項目的 pom.xml 中,添加 Feign 的依賴。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
二、定義和使用Feign客戶端
在遠程調(diào)用的服務(wù)模塊中,創(chuàng)建一個 Feign 客戶端接口
package com.example.eurekaconsumer.demos.web; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient ("userseryice") public interface UserClient { @GetMapping("/user/{name}") User findById(@PathVariable("name") String name); }
這個接口使用了 Spring MVC 的注解,定義了遠程服務(wù)的調(diào)用方式。
三、啟動類開啟Feign客戶端
啟動類添加 @EnableFeignClients 注解:
package com.example.eurekaconsumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableFeignClients @EnableEurekaClient @SpringBootApplication public class EurekaConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaConsumerApplication.class, args); } }
四、調(diào)用FeignClient接口
在需要應(yīng)用的模塊中,注入 Feign 客戶端接口并使用它來進行遠程調(diào)用。
package com.example.eurekaconsumer.demos.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserClient userClient; @RequestMapping("/showUser") @ResponseBody public User showUser() { User userInfo = userClient.findByName("Damon"); return userInfo; } }
可以看到,使用 Feign 調(diào)用的方法非常優(yōu)雅,可維護性也很強。
五、FeignClient應(yīng)用實例
1、實現(xiàn)負載均衡
我們可以用 FeignClient 代替 RestTemplate 以實現(xiàn)負載均衡。
我們先看下參考原有的 RestTemplate 實現(xiàn)負載均衡的代碼:
package com.example.eurekaconsumer.demos.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class UserController { @Autowired private RestTemplate restTemplate; @RequestMapping("/showUser") @ResponseBody public User showUser() { String baseUrl = "http://" + "eureka-provider" + "/user"; User userInfo = restTemplate.getForObject(baseUrl, User.class); return userInfo; } }
可以看到我們用 RestTemplate 實現(xiàn)負載均衡時,遇到?jīng)]有參數(shù)傳遞的情況還是比較方便的,但是遇到形如 url?param1=xxx¶m2=xxx¶m3=xxx¶m4=xxx 的應(yīng)用場景時就需要重構(gòu)代碼,非常的不方便。
于是我們使用自帶負載均衡的 Feign 遠程調(diào)用方法,改造后的方法如下:
package com.example.eurekaconsumer.demos.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserClient userClient; @RequestMapping("/showUser") @ResponseBody public User showUser() { User userInfo = userClient.findByName("Damon"); return userInfo; } }
以上是一個簡單的 Spring Cloud 中基于 Feign 的遠程調(diào)用的示例。通過使用 Feign,你可以以聲明式的方式定義遠程服務(wù)調(diào)用,而無需手動處理 HTTP 請求和響應(yīng)。這提高了代碼的可讀性和維護性,使遠程調(diào)用更加方便。
Feign 替換 RestTemplate 的好處:
優(yōu)勢 | 詳細內(nèi)容 |
聲明式 API 定義 | 使用Feign時,你可以通過簡單的注解方式聲明HTTP請求,而不需要手動構(gòu)建請求和處理響應(yīng)。Feign的注解功能使得定義和維護API變得更加直觀和容易。 |
集成了 負載均衡 | 在Spring Cloud環(huán)境中,F(xiàn)eign與Eureka或其他服務(wù)發(fā)現(xiàn)組件集成,可以自動進行負載均衡。你只需通過@FeignClient 注解指定服務(wù)名,F(xiàn)eign就會在調(diào)用時自動幫你選擇可用的服務(wù)實例。 |
支持多種編碼 器和解碼器 | Feign支持多種編碼器和解碼器,包括Jackson 、Gson 等,這使得處理不同的數(shù)據(jù)格式變得更加靈活。 |
支持 內(nèi)置斷路器 | Feign內(nèi)置了斷路器(Circuit Breaker)的支持,例如通過Hystrix。這使得在遠程調(diào)用失敗或超時時,可以采取快速失敗和降級的策略,提高系統(tǒng)的穩(wěn)定性和可靠性。 |
更易擴展 | Feign的設(shè)計使得它更易于擴展和自定義。你可以通過實現(xiàn)RequestInterceptor 接口來添加自定義的請求攔截器,或者通過實現(xiàn)ErrorDecoder 接口來處理自定義的錯誤解碼邏輯。 |
簡化了 配置和使用 | Feign的默認配置較為智能,使得在大多數(shù)情況下你無需進行額外的配置就能夠正常工作。相比之下,RestTemplate 通常需要手動配置。 |
2、 實現(xiàn)多參數(shù)調(diào)用
當(dāng)使用 FeignClient 進行遠程調(diào)用時,有時我們需要傳遞多個參數(shù)給目標(biāo)服務(wù)。使用 Feign 的多參數(shù)遠程調(diào)用能夠使代碼更加優(yōu)雅,避免了手動拼接 URL 或請求參數(shù)的繁瑣工作。
以下是一個關(guān)于 FeignClient 多參數(shù)遠程調(diào)用的應(yīng)用實例:
① 創(chuàng)建FeignClient接口
首先,定義一個FeignClient接口,使用 @FeignClient 注解標(biāo)記目標(biāo)服務(wù)的名稱。在接口中定義多個參數(shù)的遠程調(diào)用方法。
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name = "test-service") public interface TestClient { @GetMapping("/api/test") String getResource(@RequestParam("param1") String param1, @RequestParam("param2") int param2, @RequestParam("param3") boolean param3); }
在上述例子中,getResource 方法接收多個參數(shù),分別使用 @RequestParam 注解進行標(biāo)記。
② 基于FeignClient多參數(shù)調(diào)用
注入 FeignClient 接口并使用它進行多參數(shù)的遠程調(diào)用。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @Autowired private TestClient testClient; @GetMapping("/test") public String test(@RequestParam("param1") String param1, @RequestParam("param2") int param2, @RequestParam("param3") boolean param3) { // 調(diào)用遠程服務(wù)并傳遞多個參數(shù) String result = testClient.getResource(param1, param2, param3); return "Result from test service: " + result; } }
在這個例子中,TestController 的 test 方法接收多個參數(shù),然后使用注入的 TestClient 進行遠程調(diào)用,并傳遞這些參數(shù)。
通過使用 Feign 的方式,我們可以更加優(yōu)雅地進行多參數(shù)的遠程調(diào)用,避免了手動拼接URL或構(gòu)建復(fù)雜的請求體。Feign 會自動將參數(shù)轉(zhuǎn)化為請求參數(shù),使得代碼更加清晰、簡潔。這種方式也符合 Spring Cloud 中微服務(wù)架構(gòu)的設(shè)計理念,提高了代碼的可讀性和可維護性。
到此這篇關(guān)于SpringCloud基于Feign遠程調(diào)用的文章就介紹到這了,更多相關(guān)SpringCloud Feign遠程調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何解決報錯:java.net.BindException:無法指定被請求的地址問題
在Linux虛擬機上安裝并啟動Tomcat時遇到啟動失敗的問題,通過檢查端口及配置文件未發(fā)現(xiàn)異常,后發(fā)現(xiàn)/etc/hosts文件中缺少localhost的映射,添加后重啟Tomcat成功,Tomcat啟動時會檢查localhost的IP映射,缺失或錯誤都可能導(dǎo)致啟動失敗2024-10-10Spring Boot中的 6 種API請求參數(shù)讀取方式示例詳解
使用Spring Boot開發(fā)API的時候,讀取請求參數(shù)是服務(wù)端編碼中最基本的一項操作,Spring Boot中也提供了多種機制來滿足不同的API設(shè)計要求,這篇文章主要介紹了Spring Boot中的 6 種API請求參數(shù)讀取方式示例詳解,需要的朋友可以參考下2024-05-052020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝過程
這篇文章主要介紹了2020macOS Big Sur配置Java開發(fā)環(huán)境之jdk安裝,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Java CharacterEncodingFilter案例詳解
這篇文章主要介紹了Java CharacterEncodingFilter案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于SpringBoot內(nèi)存數(shù)據(jù)導(dǎo)出成Excel的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12利用SpringBoot實現(xiàn)多數(shù)據(jù)源的兩種方式總結(jié)
關(guān)于動態(tài)數(shù)據(jù)源的切換的方案有很多,核心只有兩種,一種是構(gòu)建多套環(huán)境,另一種是基于spring原生的AbstractRoutingDataSource切換,這篇文章主要給大家介紹了關(guān)于利用SpringBoot實現(xiàn)多數(shù)據(jù)源的兩種方式,需要的朋友可以參考下2021-10-10