使用OpenFeign實(shí)現(xiàn)服務(wù)調(diào)用的方法詳解
OpenFeign
OpenFeign是運(yùn)行在客戶(hù)端的聲明式服務(wù)調(diào)用的框架,通過(guò)聲明接口的方式來(lái)達(dá)到對(duì)服務(wù)的調(diào)用,表面上看起來(lái)就好像在調(diào)用本地方法一樣。
OpenFeign使用方法
創(chuàng)建一個(gè)Springboot的Web工程,命名為feign-consumer并引入相關(guān)依賴(lài)如下
<!-- eureka客戶(hù)端依賴(lài) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>3.1.6</version> </dependency> <!-- springcloud-openfeign依賴(lài) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.1.7</version> </dependency>
在項(xiàng)目中創(chuàng)建一個(gè)業(yè)務(wù)接口HelloService
,這個(gè)步驟即是對(duì)相關(guān)的調(diào)用進(jìn)行聲明,為接口指定一個(gè)@FeignClient("myservice")
注解開(kāi)啟客戶(hù)端服務(wù)調(diào)用,其中myservice
表示對(duì)應(yīng)的服務(wù)名
@FeignClient("myservice") public interface HelloService { @GetMapping({"/hello4"}) String hello(@RequestParam("name") String var1); @GetMapping({"/hello5"}) String hello(@RequestHeader("name") String var1, @RequestHeader("age") int var2); @PostMapping({"/hello6"}) String hello(@RequestBody User var1); }
需要注意的是,@RequestParam
、@RequestHeader
注解中的參數(shù)名不可以省略
在Controller中添加API方法,依次調(diào)用HelloService
的三個(gè)方法
@RequestMapping(value = "/consumer2",method = RequestMethod.GET) public String helloConsumer2(){ StringBuilder sb = new StringBuilder(); sb.append(helloService.hello("張三")).append("\n"); sb.append(helloService.hello("張三",18)).append("\n"); sb.append(helloService.hello(new User("張三",18))).append("\n"); return sb.toString(); }
在配置文件中設(shè)置注冊(cè)中心的地址
server.port=9001 spring.application.name=feign-consumer eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
最后在啟動(dòng)類(lèi)中添加注解開(kāi)啟feign客戶(hù)端服務(wù)調(diào)用以及eureka客戶(hù)端注解
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
啟動(dòng)注冊(cè)中心、注冊(cè)服務(wù)提供者和消費(fèi)者,訪(fǎng)問(wèn)/consumer2
拋開(kāi)編碼問(wèn)題不談,調(diào)用成功哈哈。
完成OpenFeign服務(wù)調(diào)用的優(yōu)化
通過(guò)對(duì)比消費(fèi)者及服務(wù)提供者的相關(guān)代碼發(fā)現(xiàn),消費(fèi)者HelloService
聲明式服務(wù)接口的代碼與服務(wù)提供者Controller
層的服務(wù)接口代碼基本相同。為了實(shí)現(xiàn)代碼的復(fù)用以及降低代碼的耦合度,現(xiàn)在將這些代碼獨(dú)立成一個(gè)單獨(dú)的模塊。
首先創(chuàng)建一個(gè)簡(jiǎn)單的Maven項(xiàng)目,取名為hello-service-api
。因?yàn)橐褂肧pring-MVC相關(guān)注解,所以導(dǎo)入相應(yīng)的依賴(lài)
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.7.0</version> </dependency> </dependencies>
緊接著創(chuàng)建HelloService
服務(wù)調(diào)用接口,因?yàn)槭褂玫搅薝ser對(duì)象,所以還要?jiǎng)?chuàng)建一個(gè)User類(lèi)
@Component public interface HelloService { @GetMapping("/hello4") String hello(@RequestParam("name") String name); @GetMapping("/hello5") String hello(@RequestHeader("name") String name,@RequestHeader("age") int age); @PostMapping("/hello6") String hello(@RequestBody User user); }
public class User { private String name; private int age; public User(){} public User(String name,int age){ this.name = name; this.age = age; } /** 省略get、set、toString方法 */ }
需要注意的是,必須要提供構(gòu)造函數(shù),因?yàn)镺penFeign需要將JSON數(shù)據(jù)轉(zhuǎn)換為對(duì)象,沒(méi)有會(huì)拋異常
使用Maven工具對(duì)其進(jìn)行打包后,分別對(duì)消費(fèi)者及服務(wù)提供者的代碼進(jìn)行重構(gòu)
在服務(wù)提供者的Controller中實(shí)現(xiàn)HelloService
接口,并編寫(xiě)具體的實(shí)現(xiàn)
@RestController public class ClientController implements HelloService { @Override public String hello(String name) { return name; } @Override public String hello(String name, int age) { return name+"|"+age; } @Override public String hello(User user) { return user.getName()+"|"+user.getAge(); } }
在服務(wù)消費(fèi)者的feign服務(wù)調(diào)用客戶(hù)端中繼承HelloService
接口
@FeignClient("myservice") public interface HelloServiceDidi extends com.didi.service.HelloService { }
最后在Controller中通過(guò)helloServiceDidi
示例完成服務(wù)調(diào)用
@RequestMapping(value = "/consumer2",method = RequestMethod.GET) public String helloConsumer2(){ StringBuilder sb = new StringBuilder(); sb.append(helloServiceDidi.hello("張三")).append("\n"); sb.append(helloServiceDidi.hello("張三",18)).append("\n"); sb.append(helloServiceDidi.hello(new User("張三",18))).append("\n"); return sb.toString(); }
測(cè)試結(jié)果如下
拋開(kāi)編碼問(wèn)題不談,調(diào)用成功哈哈。
到此這篇關(guān)于使用OpenFeign實(shí)現(xiàn)服務(wù)調(diào)用的方法詳解的文章就介紹到這了,更多相關(guān)OpenFeign服務(wù)調(diào)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何在SpringBoot中優(yōu)雅地重試調(diào)用第三方API
在實(shí)際的應(yīng)用中,我們經(jīng)常需要調(diào)用第三方API來(lái)獲取數(shù)據(jù)或執(zhí)行某些操作,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12maven工程打包引入本地jar包的實(shí)現(xiàn)
我們需要將jar包發(fā)布到一些指定的第三方Maven倉(cāng)庫(kù),本文主要介紹了maven工程打包引入本地jar包的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02SpringBoot快速構(gòu)建應(yīng)用程序方法介紹
這篇文章主要介紹了SpringBoot快速構(gòu)建應(yīng)用程序方法介紹,涉及SpringBoot默認(rèn)的錯(cuò)誤頁(yè)面,嵌入式Web容器層面的約定和定制等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11Spring Boot 各種回滾操作實(shí)戰(zhàn)教程(自動(dòng)回滾、手動(dòng)回滾、部分回滾)
這篇文章主要介紹了Spring Boot 各種回滾操作實(shí)戰(zhàn)教程(自動(dòng)回滾、手動(dòng)回滾、部分回滾),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07JavaWeb中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
這篇文章主要介紹了JavaWeb中struts2文件上傳下載功能的實(shí)現(xiàn),在Web應(yīng)用系統(tǒng)開(kāi)發(fā)中,文件上傳和下載功能是非常常用的功能,需要的朋友可以參考下2016-05-05Java使用HashMap映射實(shí)現(xiàn)消費(fèi)抽獎(jiǎng)功能
這篇文章主要為大家詳細(xì)介紹了Java使用HashMap映射實(shí)現(xiàn)消費(fèi)抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09