Spring FeignClient使用


FeignClient與RestTemplate的區別比較簡單研究
題外:個人覺得可能還沒達到那種境界,還體會不到真正的實質性區別,就好比用HttpClient可以實現的用FeignClient同樣可以實現,反之也是。

JAVA 項目中接口調用怎么做 ?

Httpclient
Okhttp
Httpurlconnection
RestTemplate
上面是最常見的幾種用法,我們今天要介紹的用法比上面的更簡單,方便,它就是Feign

Feign是一個聲明式的REST客戶端,它的目的就是讓REST調用更加簡單。

Feign提供了HTTP請求的模板,通過編寫簡單的接口和插入注解,就可以定義好HTTP請求的參數、格式、地址等信息。

而Feign則會完全代理HTTP請求,我們只需要像調用方法一樣調用它就可以完成服務請求及相關處理。

SpringCloud對Feign進行了封裝,使其支持SpringMVC標准注解和HttpMessageConverters。

Feign可以與Eureka和Ribbon組合使用以支持負載均衡。

區別:

在預訂微服務中,有一個同步呼叫票價(Fare)。RestTemplate是用來制作的同步呼叫。使用RestTemplate時,URL參數是以編程方式構造的,數據被發送到其他服務。在更復雜的情況下,我們將不得不RestTemplate深入到更低級別的API提供的甚至是API 的細節。

Feign是Spring Cloud Netflix庫,用於在基於REST的服務調用上提供更高級別的抽象。Spring Cloud Feign在聲明性原則上工作。使用Feign時,我們在客戶端編寫聲明式REST服務接口,並使用這些接口來編寫客戶端程序。開發人員不用擔心這個接口的實現。這將在運行時由Spring動態配置。通過這種聲明性的方法,開發人員不需要深入了解由HTTP提供的HTTP級別API的細節的RestTemplate。

總結:

也就是說FeignClient簡化了請求的編寫,且通過動態負載進行選擇要使用哪個服務進行消費,而這一切都由Spring動態配置實現,我們不用關心這些,只管使用方法即可。再說,就是簡化了編寫,RestTemplate還需要寫上服務器IP這些信息等等,而FeignClient則不用。

不過話又再說回來,其實RestTemplate同樣可以簡化到使用FeignClient那樣簡單,無非就是自己封裝多一層去實現而已,所以,我個人覺得沒有太多絕對,只是看你的業務需求怎么定位這個選擇而已。

使用Spring Cloud搭建各種微服務之后,服務可以通過@FeignClient使用和發現服務場中的其他服務。
還是以Config Server和Config Client為例,這是服務場中的注冊的兩個微服務。
Config Server中定義了兩個服務接口(一個Post、一個Get方法)
package demo.controller;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import demo.model.User;

@RestController
@RefreshScope
public class Controller {

@GetMapping(value="s")
String helloServer(String username) {
return "Hello " + username + "!";
}

@PostMapping(value="u")
String helloUser(@RequestBody User user) {
return "Hello " + user + "!";
}

}
在Config Client(spring.application.name=test-dev)我們使用Config Server中定義的這兩個微服務。

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import cn.test.bean.User;


@FeignClient(value = "config-service")
public interface FindService {

@GetMapping(value="s")
String helloServer(@RequestParam("username")String username);

@PostMapping(value="u")
String helloUser(@RequestBody User user);

}
注意,Get方法中通過@RequestParam指定需要傳遞的參數,應用啟動時需添加@EnableFeignClients注解。
@Autowired
private FindService service;

@GetMapping("/find")
String find(String param) {
return "find " + service.helloServer(param);
}

@PostMapping("/f")
String findU(@RequestBody User user) {
return "find " + service.helloUser(user);
}
參考:

http://blog.csdn.net/u010889990/article/details/78673273

https://segmentfault.com/a/1190000009229438

https://www.packtpub.com/mapt/book/application_development/9781786466686/5/ch05lvl1sec57/feign-as-a-declarative-rest-client

https://www.jianshu.com/p/3d597e9d2d67

http://www.cnblogs.com/duanxz/p/7516676.html

https://www.cnblogs.com/duanxz/p/3510622.html

https://www.jianshu.com/p/270f3cd658f1

https://zhuanlan.zhihu.com/p/26400247


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM