使用OpenFeign實現(xiàn)服務(wù)調(diào)用的方法詳解
OpenFeign
OpenFeign是運(yùn)行在客戶端的聲明式服務(wù)調(diào)用的框架,通過聲明接口的方式來達(dá)到對服務(wù)的調(diào)用,表面上看起來就好像在調(diào)用本地方法一樣。
OpenFeign使用方法
創(chuàng)建一個Springboot的Web工程,命名為feign-consumer并引入相關(guān)依賴如下
<!-- 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è)務(wù)接口HelloService,這個步驟即是對相關(guān)的調(diào)用進(jìn)行聲明,為接口指定一個@FeignClient("myservice")注解開啟客戶端服務(wù)調(diào)用,其中myservice表示對應(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的三個方法
@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è)置注冊中心的地址
server.port=9001 spring.application.name=feign-consumer eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
最后在啟動類中添加注解開啟feign客戶端服務(wù)調(diào)用以及eureka客戶端注解
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}啟動注冊中心、注冊服務(wù)提供者和消費(fèi)者,訪問/consumer2

拋開編碼問題不談,調(diào)用成功哈哈。
完成OpenFeign服務(wù)調(diào)用的優(yōu)化
通過對比消費(fèi)者及服務(wù)提供者的相關(guān)代碼發(fā)現(xiàn),消費(fèi)者HelloService聲明式服務(wù)接口的代碼與服務(wù)提供者Controller層的服務(wù)接口代碼基本相同。為了實現(xiàn)代碼的復(fù)用以及降低代碼的耦合度,現(xiàn)在將這些代碼獨立成一個單獨的模塊。
首先創(chuàng)建一個簡單的Maven項目,取名為hello-service-api。因為要使用Spring-MVC相關(guān)注解,所以導(dǎo)入相應(yīng)的依賴
<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)用接口,因為使用到了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方法 */
}需要注意的是,必須要提供構(gòu)造函數(shù),因為OpenFeign需要將JSON數(shù)據(jù)轉(zhuǎn)換為對象,沒有會拋異常
使用Maven工具對其進(jìn)行打包后,分別對消費(fèi)者及服務(wù)提供者的代碼進(jìn)行重構(gòu)
在服務(wù)提供者的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();
}
}在服務(wù)消費(fèi)者的feign服務(wù)調(diào)用客戶端中繼承HelloService接口
@FeignClient("myservice")
public interface HelloServiceDidi extends com.didi.service.HelloService {
}最后在Controller中通過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();
}測試結(jié)果如下

拋開編碼問題不談,調(diào)用成功哈哈。
到此這篇關(guān)于使用OpenFeign實現(xiàn)服務(wù)調(diào)用的方法詳解的文章就介紹到這了,更多相關(guān)OpenFeign服務(wù)調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java語言描述存儲結(jié)構(gòu)與鄰接矩陣代碼示例
這篇文章主要介紹了Java語言描述存儲結(jié)構(gòu)與鄰接矩陣代碼示例,涉及Java存儲結(jié)構(gòu),鄰接矩陣,鄰接表的介紹與比較,然后分享了鄰接矩陣的Java實現(xiàn)等相關(guān)內(nèi)容,具有一定借鑒價值,需要的朋友可以參考。2017-11-11
springboot3.x使用@NacosValue無法獲取配置信息的解決過程
在Spring Boot 3.x中升級Nacos依賴后,使用@NacosValue無法動態(tài)獲取配置,通過引入Spring Cloud依賴和使用@RefreshScope@Value結(jié)合的方式解決了問題2025-12-12
在Java項目中實現(xiàn)CI/CD持續(xù)集成與持續(xù)部署
這篇文章主要為大家介紹了在Java項目中實現(xiàn)CI/CD持續(xù)集成與持續(xù)部署詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

