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