SpringCloud基于Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用的問題小結(jié)
Spring Cloud 是一個(gè)用于構(gòu)建分布式系統(tǒng)的開發(fā)工具包,它提供了一系列的微服務(wù)組件,其中之一就是 Feign。Feign 是一種聲明式的 Web 服務(wù)客戶端,它簡化了在 Spring Cloud 中進(jìn)行遠(yuǎn)程調(diào)用的過程。本文將介紹如何在 Spring Cloud 中使用 Feign 進(jìn)行遠(yuǎn)程調(diào)用。
一、引入Feign依賴
我們在 Spring Cloud 項(xiàng)目的 pom.xml 中,添加 Feign 的依賴。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
二、定義和使用Feign客戶端
在遠(yuǎn)程調(diào)用的服務(wù)模塊中,創(chuàng)建一個(gè) 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);
}
這個(gè)接口使用了 Spring MVC 的注解,定義了遠(yuǎn)程服務(wù)的調(diào)用方式。
三、啟動(dòng)類開啟Feign客戶端
啟動(dòng)類添加 @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 客戶端接口并使用它來進(jìn)行遠(yuǎn)程調(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)雅,可維護(hù)性也很強(qiáng)。

五、FeignClient應(yīng)用實(shí)例
1、實(shí)現(xiàn)負(fù)載均衡
我們可以用 FeignClient 代替 RestTemplate 以實(shí)現(xiàn)負(fù)載均衡。
我們先看下參考原有的 RestTemplate 實(shí)現(xiàn)負(fù)載均衡的代碼:
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 實(shí)現(xiàn)負(fù)載均衡時(shí),遇到?jīng)]有參數(shù)傳遞的情況還是比較方便的,但是遇到形如 url?param1=xxx¶m2=xxx¶m3=xxx¶m4=xxx 的應(yīng)用場景時(shí)就需要重構(gòu)代碼,非常的不方便。
于是我們使用自帶負(fù)載均衡的 Feign 遠(yuǎn)程調(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;
}
}以上是一個(gè)簡單的 Spring Cloud 中基于 Feign 的遠(yuǎn)程調(diào)用的示例。通過使用 Feign,你可以以聲明式的方式定義遠(yuǎn)程服務(wù)調(diào)用,而無需手動(dòng)處理 HTTP 請求和響應(yīng)。這提高了代碼的可讀性和維護(hù)性,使遠(yuǎn)程調(diào)用更加方便。
Feign 替換 RestTemplate 的好處:
| 優(yōu)勢 | 詳細(xì)內(nèi)容 |
| 聲明式 API 定義 | 使用Feign時(shí),你可以通過簡單的注解方式聲明HTTP請求,而不需要手動(dòng)構(gòu)建請求和處理響應(yīng)。Feign的注解功能使得定義和維護(hù)API變得更加直觀和容易。 |
| 集成了 負(fù)載均衡 | 在Spring Cloud環(huán)境中,F(xiàn)eign與Eureka或其他服務(wù)發(fā)現(xiàn)組件集成,可以自動(dòng)進(jìn)行負(fù)載均衡。你只需通過@FeignClient注解指定服務(wù)名,F(xiàn)eign就會(huì)在調(diào)用時(shí)自動(dòng)幫你選擇可用的服務(wù)實(shí)例。 |
| 支持多種編碼 器和解碼器 | Feign支持多種編碼器和解碼器,包括Jackson、Gson等,這使得處理不同的數(shù)據(jù)格式變得更加靈活。 |
| 支持 內(nèi)置斷路器 | Feign內(nèi)置了斷路器(Circuit Breaker)的支持,例如通過Hystrix。這使得在遠(yuǎn)程調(diào)用失敗或超時(shí)時(shí),可以采取快速失敗和降級(jí)的策略,提高系統(tǒng)的穩(wěn)定性和可靠性。 |
| 更易擴(kuò)展 | Feign的設(shè)計(jì)使得它更易于擴(kuò)展和自定義。你可以通過實(shí)現(xiàn)RequestInterceptor接口來添加自定義的請求攔截器,或者通過實(shí)現(xiàn)ErrorDecoder接口來處理自定義的錯(cuò)誤解碼邏輯。 |
| 簡化了 配置和使用 | Feign的默認(rèn)配置較為智能,使得在大多數(shù)情況下你無需進(jìn)行額外的配置就能夠正常工作。相比之下,RestTemplate通常需要手動(dòng)配置。 |
2、 實(shí)現(xiàn)多參數(shù)調(diào)用
當(dāng)使用 FeignClient 進(jìn)行遠(yuǎn)程調(diào)用時(shí),有時(shí)我們需要傳遞多個(gè)參數(shù)給目標(biāo)服務(wù)。使用 Feign 的多參數(shù)遠(yuǎn)程調(diào)用能夠使代碼更加優(yōu)雅,避免了手動(dòng)拼接 URL 或請求參數(shù)的繁瑣工作。
以下是一個(gè)關(guān)于 FeignClient 多參數(shù)遠(yuǎn)程調(diào)用的應(yīng)用實(shí)例:
① 創(chuàng)建FeignClient接口
首先,定義一個(gè)FeignClient接口,使用 @FeignClient 注解標(biāo)記目標(biāo)服務(wù)的名稱。在接口中定義多個(gè)參數(shù)的遠(yuǎn)程調(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 方法接收多個(gè)參數(shù),分別使用 @RequestParam 注解進(jìn)行標(biāo)記。
② 基于FeignClient多參數(shù)調(diào)用
注入 FeignClient 接口并使用它進(jìn)行多參數(shù)的遠(yuǎn)程調(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)用遠(yuǎn)程服務(wù)并傳遞多個(gè)參數(shù)
String result = testClient.getResource(param1, param2, param3);
return "Result from test service: " + result;
}
}在這個(gè)例子中,TestController 的 test 方法接收多個(gè)參數(shù),然后使用注入的 TestClient 進(jìn)行遠(yuǎn)程調(diào)用,并傳遞這些參數(shù)。
通過使用 Feign 的方式,我們可以更加優(yōu)雅地進(jìn)行多參數(shù)的遠(yuǎn)程調(diào)用,避免了手動(dòng)拼接URL或構(gòu)建復(fù)雜的請求體。Feign 會(huì)自動(dòng)將參數(shù)轉(zhuǎn)化為請求參數(shù),使得代碼更加清晰、簡潔。這種方式也符合 Spring Cloud 中微服務(wù)架構(gòu)的設(shè)計(jì)理念,提高了代碼的可讀性和可維護(hù)性。
到此這篇關(guān)于SpringCloud基于Feign遠(yuǎn)程調(diào)用的文章就介紹到這了,更多相關(guān)SpringCloud Feign遠(yuǎn)程調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的CopyOnWriteArrayList原理詳解
這篇文章主要介紹了Java中的CopyOnWriteArrayList原理詳解,如源碼所示,CopyOnWriteArrayList和ArrayList一樣,都在內(nèi)部維護(hù)了一個(gè)數(shù)組,操作CopyOnWriteArrayList其實(shí)就是在操作內(nèi)部的數(shù)組,需要的朋友可以參考下2023-12-12
Java中關(guān)于ThreadLocal的隱式引用詳解
這篇文章主要介紹了Java中關(guān)于ThreadLocal的隱式引用,從線程的角度看,每個(gè)線程都保持一個(gè)對其線程局部變量副本的隱式引用,只要線程是活動(dòng)的,ThreadLocal實(shí)例就是可訪問的,下面我們來具體看看2024-03-03
Java子類實(shí)例化總是默認(rèn)調(diào)用父類的無參構(gòu)造操作
這篇文章主要介紹了Java子類實(shí)例化總是默認(rèn)調(diào)用父類的無參構(gòu)造操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Java 文件傳輸助手的實(shí)現(xiàn)(單機(jī)版)
這篇文章主要介紹了Java 文件傳輸助手的實(shí)現(xiàn)(單機(jī)版),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
使用mybatis的@Interceptor實(shí)現(xiàn)攔截sql的方法詳解
攔截器是一種基于 AOP(面向切面編程)的技術(shù),它可以在目標(biāo)對象的方法執(zhí)行前后插入自定義的邏輯,本文給大家介紹了使用mybatis的@Interceptor實(shí)現(xiàn)攔截sql的方法,需要的朋友可以參考下2024-03-03
Spring Boot項(xiàng)目利用Redis實(shí)現(xiàn)集中式緩存實(shí)例
本篇文章主要介紹了Spring Boot項(xiàng)目利用Redis實(shí)現(xiàn)集中式緩存實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Java中使用HashMap時(shí)指定初始化容量性能解析
這篇文章主要為大家介紹了Java中使用HashMap時(shí)指定初始化容量性能解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Mybatis配置錯(cuò)誤:java.lang.ExceptionInInitializerError
這篇文章主要介紹了Mybatis配置錯(cuò)誤:java.lang.ExceptionInInitializerError的相關(guān)資料,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

