springboot中如何使用openfeign進(jìn)行接口調(diào)用
在項(xiàng)目開發(fā)過程中,我們開發(fā)的項(xiàng)目或多或少都會(huì)與其他交互的系統(tǒng)進(jìn)行對(duì)接集成,或者是微服務(wù)項(xiàng)目之間進(jìn)行調(diào)用,雙方相互調(diào)用接口來獲取或者傳遞數(shù)據(jù),以往我們使用的方式可能有 httpClient、okhttp、httpUrlConnection 等等,來學(xué)習(xí)一下如何使用 openfeign 來進(jìn)行調(diào)用。
一、feign 和 openfeign ?
Feign 是 Springcloud 組件中的一個(gè)輕量級(jí) Restful 的 HTTP 服務(wù)客戶端,F(xiàn)eign 內(nèi)置了 Ribbon,用來做客戶端負(fù)載均衡,去調(diào)用服務(wù)注冊(cè)中心的服務(wù)。
Feign 的使用方式是:使用Feign的注解定義接口,調(diào)用這個(gè)接口,就可以調(diào)用服務(wù)注冊(cè)中心的服務(wù)。
OpenFeign 是 springcloud 在Feign的基礎(chǔ)上支持了 SpringMVC 的注解,如 @RequestMapping 等等。
OpenFeign 的 @FeignClient 可以解析 SpringMVC 的 @RequestMapping 注解下的接口,并通過動(dòng)態(tài)代理的方式產(chǎn)生實(shí)現(xiàn)類,實(shí)現(xiàn)類中做負(fù)載均衡并調(diào)用其他服務(wù)。
二、為什么要使用 openfeign ?
1、Feign 本身不支持 Spring MVC 的注解,它有一套自己的注解
2、Fegin 是 Netflix 公司產(chǎn)品,停止更新了。
3、使用 OpenFeign 可以簡(jiǎn)化項(xiàng)目之間接口的調(diào)用,我們不需要關(guān)心 http 調(diào)用的代碼
三、創(chuàng)建一個(gè)簡(jiǎn)單的服務(wù)提供方
創(chuàng)建一個(gè)基本的 springboot 項(xiàng)目
1、引入基礎(chǔ)依賴
<dependencies> <dependency> <groupId> org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、application.yml 配置文件
server: port: 8081
3、創(chuàng)建一個(gè) controller
@RestController @RequestMapping("/demo") public class FeignDemoController { @GetMapping("/test") public String test(){ return "hello openfegin"; } }
4、啟動(dòng)服務(wù)
四、服務(wù)調(diào)用方 openfeign 調(diào)用服務(wù)提供方
創(chuàng)建一個(gè)基本的 springboot 項(xiàng)目
1、引入相關(guān)依賴
這里要注意springboot 和 springCloud 版本
當(dāng)前 springboot 版本 2.3.4.RELEASE
springcloud 版本 Hoxton.SR4
<properties> <java.version>1.8</java.version> <spring.cloud-version>Hoxton.SR4</spring.cloud-version> </properties> <dependencies> <dependency> <groupId> org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud-version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2、啟動(dòng)類中增加注解
開啟 feign 功能
@SpringBootApplication @EnableFeignClients public class OpenfeginApplication { public static void main(String[] args) { SpringApplication.run(OpenfeginApplication.class, args); } }
3、編寫客戶端調(diào)用接口
@FeignClient 標(biāo)注該類是一個(gè)feign接口
name
:因?yàn)檫@里并未進(jìn)行服務(wù)注冊(cè),所以就隨便命名一個(gè)url
:被調(diào)用的服務(wù)提供方的接口地址
@FeignClient(name = "demo",url = "http://localhost:8081/demo") public interface FeginDemo { @GetMapping("test") //被調(diào)用接口的請(qǐng)求類型 String test(); }
4、測(cè)試調(diào)用服務(wù)端提供端接口
@RestController @RequestMapping("/feginDemo") public class FeginDemoController { @Autowired FeginDemo feginDemo; @GetMapping("/test") public String test(){ String test = feginDemo.test(); return test; } }
5、測(cè)試
啟動(dòng)項(xiàng)目,訪問 http://localhost:8080/feginDemo/test
接口調(diào)用成功!
那么這里呢,我們就實(shí)現(xiàn)了通過 openfeign 完成一次簡(jiǎn)單的接口調(diào)用,如果沒有使用 openfeign 這里的調(diào)用我們就只能通過 httpclient、okhttp 等方式來調(diào)用了,使用了 openfeign 之后,你會(huì)發(fā)現(xiàn),調(diào)用別人的接口竟是如此簡(jiǎn)單。
五、openfeign 服務(wù)熔斷
在調(diào)用接口過程中,由于是調(diào)用別人接口,肯定會(huì)存在一些意外因素,比如被調(diào)用的服務(wù)提供方系統(tǒng)宕機(jī)了,訪問不到了等情況,如下比如我停止 被調(diào)用的服務(wù)提供方,再去進(jìn)行調(diào)用
那么這個(gè)時(shí)候我們就需要用到服務(wù)熔斷了,在對(duì)方服務(wù)異常的時(shí)候,返回這個(gè)信息肯定不太友好,我們來讓它稍微友好一些
openfeign 中默認(rèn)集成了 Hystrix
1、在 application.yml 配置文件中開啟
feign: hystrix: enabled: true
2、配置 fallback 的處理類
創(chuàng)建一個(gè)類,實(shí)現(xiàn)我們的 FeginDemo ,然后實(shí)現(xiàn)接口中的方法,進(jìn)行重寫
@Component public class ErrorMessage implements FeginDemo { @Override public String test() { return "服務(wù)器開小差了哦,請(qǐng)稍后再試"; } }
3、改造 feignClient
fallback:配置剛才的實(shí)現(xiàn)類 ErrorMessage
@FeignClient(name = "demo",url = "http://localhost:8081/demo",fallback = ErrorMessage.class) public interface FeginDemo { @GetMapping("test") String test(); }
4、測(cè)試熔斷效果
重新啟動(dòng)一下服務(wù)再進(jìn)行訪問
對(duì)于用戶來說,這樣是不是更加友好一點(diǎn)了呢?總比你一連串的英文往人家臉上呼來的好吧
六、配置日志
1、配置項(xiàng)目日志級(jí)別
logging: level: com.wxw.openfeign: debuglogging: level: com.wxw.openfeign: debug
2、配置 openfeign 日志級(jí)別
@Configuration public class FeignLogConfiguration { // NONE:不記錄任何日志信息,這是默認(rèn)值。 // BASIC:僅記錄請(qǐng)求的方法,URL以及響應(yīng)狀態(tài)碼和執(zhí)行時(shí)間 // HEADERS:在BASIC的基礎(chǔ)上,額外記錄了請(qǐng)求和響應(yīng)的頭信息 // FULL:記錄所有請(qǐng)求和響應(yīng)的明細(xì),包括頭信息、請(qǐng)求體、元數(shù)據(jù) @Bean Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
3、配置日志類
@FeignClient(name = "demo",url = "http://localhost:8081/demo", fallback = ErrorMessage.class,configuration = FeignLogConfiguration.class) public interface FeginDemo { @GetMapping("test") String test(); }
4、調(diào)用之后查看日志
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
教你1秒將本地SpringBoot項(xiàng)目jar包部署到Linux環(huán)境(超詳細(xì)!)
spring Boot簡(jiǎn)化了Spring應(yīng)用的開發(fā)過程,遵循約定優(yōu)先配置的原則提供了各類開箱即用(out-of-the-box)的框架配置,下面這篇文章主要給大家介紹了關(guān)于1秒將本地SpringBoot項(xiàng)目jar包部署到Linux環(huán)境的相關(guān)資料,超級(jí)詳細(xì),需要的朋友可以參考下2023-04-04解決idea中maven新增的配置文件xx.xml沒生效問題
這篇文章主要介紹了如何解決idea中maven新增的配置文件xx.xml沒生效問題,公司項(xiàng)目有用自己的`私服,Maven正常去私服下載jar包是沒問題的,但阿里云鏡像找不到相關(guān)的jar包報(bào)錯(cuò),文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06關(guān)于Java Interface接口的簡(jiǎn)單練習(xí)題
這篇文章主要給大家分享的是關(guān)于Java Interface接口的簡(jiǎn)單練習(xí)題,難度不算大,但是要有一個(gè)清晰的邏輯建立接口和鏈接Java類。下面來看看文章的詳細(xì)介紹吧,需要的朋友可以參考一下2021-11-11吊打Java面試官之Lambda表達(dá)式 Stream API
這篇文章主要介紹了吊打Java之jdk8的新特性包括Lambda表達(dá)式、函數(shù)式接口、Stream API全面刨析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09