Spring?Cloud?OpenFeign模版化客戶端搭建過(guò)程
OpenFeign是什么?
OpenFeign是一個(gè)顯示聲明式的WebService客戶端。使用OpenFeign能讓編寫Web Service客戶端更加簡(jiǎn)單。使用時(shí)只需定義服務(wù)接口,然后在上面添加注解。OpenFeign也支持可拔插式的編碼和解碼器。spring cloud對(duì)feign進(jìn)行了封裝,使其支持MVC注解和HttpMessageConverts。和eureka(服務(wù)注冊(cè)中心)和ribbon組合可以實(shí)現(xiàn)負(fù)載均衡。在Spring Cloud中使用OpenFeign,可以做到使用HTTP請(qǐng)求訪問(wèn)遠(yuǎn)程服務(wù),就像調(diào)用本地方法一樣的,開發(fā)者完全感知不到這是在調(diào)用遠(yuǎn)程方法,更感知不到在訪問(wèn)HTTP請(qǐng)求,非常的方便。
OpenFeign能干啥?
- OpenFeign的設(shè)計(jì)宗旨式簡(jiǎn)化Java Http客戶端的開發(fā)。Feign在restTemplate的基礎(chǔ)上做了進(jìn)一步的封裝,由其來(lái)幫助我們定義和實(shí)現(xiàn)依賴服務(wù)接口的定義。在OpenFeign的協(xié)助下,我們只需創(chuàng)建一個(gè)接口并使用注解的方式進(jìn)行配置(類似于Dao接口上面的Mapper注解)即可完成對(duì)服務(wù)提供方的接口綁定,大大簡(jiǎn)化了Spring cloud Ribbon的開發(fā),自動(dòng)封裝服務(wù)調(diào)用客戶端的開發(fā)量。
- OpenFeign集成了Ribbon,利用ribbon維護(hù)了服務(wù)列表,并且通過(guò)ribbon實(shí)現(xiàn)了客戶端的負(fù)載均衡。與ribbon不同的是,通過(guò)OpenFeign只需要定義服務(wù)綁定接口且以申明式的方法,優(yōu)雅而簡(jiǎn)單的實(shí)現(xiàn)了服務(wù)調(diào)用。
OpenFeign使用
使用OpenFeign之前,我們首先將之前的工程還原,為了操作簡(jiǎn)單,我們只采用單節(jié)點(diǎn)的eureka。
API服務(wù)模塊搭建
由于我們使用openfeign之后,需要暴露相關(guān)接口給外部服務(wù),所以我們需要寫一個(gè)api服務(wù)。
我這里為了方便所以直接在外部創(chuàng)建了一個(gè)ms-service-api服務(wù),實(shí)際開發(fā)過(guò)程中我們基本都是將其寫在對(duì)應(yīng)的模塊中。

整體模塊創(chuàng)建完成后,我們便可以定義相關(guān)接口供外部調(diào)用了,每個(gè)服務(wù)一一對(duì)應(yīng)即可。
這里只演示一個(gè)goods-service-api項(xiàng)目的搭建,項(xiàng)目代碼在文末會(huì)給出。
引入依賴
引入 open feign的相關(guān)依賴。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>創(chuàng)建接口
這里對(duì)應(yīng)的是goods-service項(xiàng)目中的GoodsService類,這里定義了其中的方法。
public interface IGoodsService {
@GetMapping("/goods")
String getGoodsById();
}這個(gè)時(shí)候還需要將goods-service項(xiàng)目的類繼承該接口:
首先需要將我們創(chuàng)建的api服務(wù)依賴添加到項(xiàng)目中:
<dependency>
<groupId>com.example</groupId>
<artifactId>goods-service-api</artifactId>
</dependency>然后開始修改:
@Slf4j
@RestController
public class GoodsService implements IGoodsService {
@Value("${server.port}")
private String port;
/**
* 根據(jù)ID查詢商品信息
*
* @return
*/
@GetMapping("/goods")
public String getGoodsById() {
log.info("收到請(qǐng)求,端口為:{}", port);
return "返回商品信息";
}
}
這里為了區(qū)分職責(zé),類似我們寫mapper和service這種,又額外寫了一個(gè)對(duì)外暴露的接口。
FeignClient中的name不能隨意寫,它對(duì)應(yīng)各個(gè)服務(wù)在eureka中注冊(cè)的名字。如果不寫該接口的話,可以將該注解加在IGoodsService上。
@FeignClient(name = "goods-service")
public interface IGoodsServiceFeignClient extends IGoodsService {
}
以上便是一個(gè)api服務(wù)的搭建過(guò)程。
外部調(diào)用
當(dāng)我們的API服務(wù)調(diào)用完成之后,如何在聚合服務(wù)中調(diào)用呢?
首先我們需要在聚合服務(wù)中引入openfeign以及各api服務(wù)的依賴:
<!-- open fiegn-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>goods-service-api</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>marking-service-api</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>order-service-api</artifactId>
</dependency>引入相關(guān)依賴后,我們就可以不需要之前的代碼了,全面采用面向接口的形式來(lái)開發(fā)。
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
// 代理對(duì)象
@Autowired
private IGoodsService goodsService;
@Autowired
private IPromotionService promotionService;
@Autowired
private IOrderService orderService;
@GetMapping
public String order() {
log.info("begin do order");
// 使用openfiegn
String goods = goodsService.getGoodsById();
String promotion = promotionService.getPromotionById();
String result = orderService.createOrder(goods, promotion);
return result;
}
}
這個(gè)時(shí)候還沒(méi)有完全結(jié)束,我們還需要在啟動(dòng)類上配置掃碼相關(guān)的feign類。
@EnableFeignClients(basePackages = "com.example.feignclient")
@SpringBootApplication
public class MallProtalApplication {
public static void main(String[] args) {
SpringApplication.run(MallProtalApplication.class, args);
}
}以上便是我們集成openfeign的全部步驟。
項(xiàng)目代碼
OpenFeign相關(guān)特性
- Gzip壓縮
- Looger
- 底層通信框架:(sun.net.www.protocol.http.HttpURLConnection)
- 也可以自定義替換為OKHttp
Logger 日志使用
使用OpenFeign的日志功能,我們需要進(jìn)行如下幾個(gè)操作:
創(chuàng)建配置類
@Configuration
public class FeignClientLogConfiguration {
/**
* NONE
* BASIC
* HEAD
* FULL
* @return
*/
@Bean
Logger.Level feignLogger(){
return Logger.Level.FULL;
}
}
指定配置類
@FeignClient(name = "goods-service",configuration = FeignClientLogConfiguration.class)
public interface IGoodsServiceFeignClient extends IGoodsService {
}
最后還需要在聚合服務(wù)出添加相關(guān)日志配置
feignclient所在的路徑
# openfeign 日志 logging.level.[com.example.feignclient]=DEBUG

使用OKHttp通信
引入依賴
<!-- ophttp-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
修改配置
# openfeign替換底層通信框架 feign.httpclient.enabled=false feign.okhttp.enabled=true
到此這篇關(guān)于Spring Cloud OpenFeign模版化客戶端的文章就介紹到這了,更多相關(guān)Spring Cloud OpenFeign客戶端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud OpenFeign超詳細(xì)講解模板化遠(yuǎn)程通信的實(shí)現(xiàn)
- 分享Spring?Cloud?OpenFeign?的五個(gè)優(yōu)化技巧
- SpringCloud學(xué)習(xí)筆記之OpenFeign進(jìn)行服務(wù)調(diào)用
- 解決啟用 Spring-Cloud-OpenFeign 配置可刷新項(xiàng)目無(wú)法啟動(dòng)的問(wèn)題
- 一篇文章教你如何在SpringCloud項(xiàng)目中使用OpenFeign
- 完美解決SpringCloud-OpenFeign使用okhttp替換不生效問(wèn)題
- 詳解SpringCloud-OpenFeign組件的使用
- Spring Cloud 系列之服務(wù)調(diào)用 OpenFeign的實(shí)現(xiàn)
- Springcloud基于OpenFeign實(shí)現(xiàn)服務(wù)調(diào)用代碼實(shí)例
- Spring Cloud詳解實(shí)現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法
相關(guān)文章
Java 中文字符按Unicode排序的實(shí)現(xiàn)方法
這篇文章主要介紹了Java 中文字符按Unicode排序的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例
這篇文章主要介紹了JAVA格式化時(shí)間日期的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-11-11
Spring整合WebSocket應(yīng)用示例(上)
以下教程是小編在參與開發(fā)公司的一個(gè)crm系統(tǒng),整理些相關(guān)資料,在該系統(tǒng)中有很多消息推送功能,在其中用到了websocket技術(shù)。下面小編整理分享到腳本之家平臺(tái)供大家參考2016-04-04
hibernate中HQL如何調(diào)用自定義函數(shù)
這篇文章主要介紹了hibernate中HQL如何調(diào)用自定義函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

