使用OpenFeign實現(xiàn)服務調(diào)用的方法詳解
OpenFeign
OpenFeign是運行在客戶端的聲明式服務調(diào)用的框架,通過聲明接口的方式來達到對服務的調(diào)用,表面上看起來就好像在調(diào)用本地方法一樣。
OpenFeign使用方法
創(chuàng)建一個Springboot的Web工程,命名為feign-consumer并引入相關依賴如下
<!-- eureka客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>3.1.6</version> </dependency> <!-- springcloud-openfeign依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.1.7</version> </dependency>
在項目中創(chuàng)建一個業(yè)務接口HelloService
,這個步驟即是對相關的調(diào)用進行聲明,為接口指定一個@FeignClient("myservice")
注解開啟客戶端服務調(diào)用,其中myservice
表示對應的服務名
@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
的三個方法
@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(); }
在配置文件中設置注冊中心的地址
server.port=9001 spring.application.name=feign-consumer eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
最后在啟動類中添加注解開啟feign客戶端服務調(diào)用以及eureka客戶端注解
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class FeignConsumerApplication { public static void main(String[] args) { SpringApplication.run(FeignConsumerApplication.class, args); } }
啟動注冊中心、注冊服務提供者和消費者,訪問/consumer2
拋開編碼問題不談,調(diào)用成功哈哈。
完成OpenFeign服務調(diào)用的優(yōu)化
通過對比消費者及服務提供者的相關代碼發(fā)現(xiàn),消費者HelloService
聲明式服務接口的代碼與服務提供者Controller
層的服務接口代碼基本相同。為了實現(xiàn)代碼的復用以及降低代碼的耦合度,現(xiàn)在將這些代碼獨立成一個單獨的模塊。
首先創(chuàng)建一個簡單的Maven項目,取名為hello-service-api
。因為要使用Spring-MVC相關注解,所以導入相應的依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.7.0</version> </dependency> </dependencies>
緊接著創(chuàng)建HelloService
服務調(diào)用接口,因為使用到了User對象,所以還要創(chuàng)建一個User類
@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方法 */ }
需要注意的是,必須要提供構造函數(shù),因為OpenFeign需要將JSON數(shù)據(jù)轉換為對象,沒有會拋異常
使用Maven工具對其進行打包后,分別對消費者及服務提供者的代碼進行重構
在服務提供者的Controller中實現(xiàn)HelloService
接口,并編寫具體的實現(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(); } }
在服務消費者的feign服務調(diào)用客戶端中繼承HelloService
接口
@FeignClient("myservice") public interface HelloServiceDidi extends com.didi.service.HelloService { }
最后在Controller中通過helloServiceDidi
示例完成服務調(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(); }
測試結果如下
拋開編碼問題不談,調(diào)用成功哈哈。
到此這篇關于使用OpenFeign實現(xiàn)服務調(diào)用的方法詳解的文章就介紹到這了,更多相關OpenFeign服務調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解如何在SpringBoot中優(yōu)雅地重試調(diào)用第三方API
在實際的應用中,我們經(jīng)常需要調(diào)用第三方API來獲取數(shù)據(jù)或執(zhí)行某些操作,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12Spring Boot 各種回滾操作實戰(zhàn)教程(自動回滾、手動回滾、部分回滾)
這篇文章主要介紹了Spring Boot 各種回滾操作實戰(zhàn)教程(自動回滾、手動回滾、部分回滾),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07JavaWeb中struts2實現(xiàn)文件上傳下載功能實例解析
這篇文章主要介紹了JavaWeb中struts2文件上傳下載功能的實現(xiàn),在Web應用系統(tǒng)開發(fā)中,文件上傳和下載功能是非常常用的功能,需要的朋友可以參考下2016-05-05