OpenFeign實(shí)現(xiàn)遠(yuǎn)程調(diào)用
本文實(shí)例為大家分享了OpenFeign遠(yuǎn)程調(diào)用實(shí)現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下
什么是OpenFeign
OpenFeign目前是Spring Cloud 二級(jí)子項(xiàng)目。平時(shí)說的Feign指的是Netflix下的Feign,現(xiàn)在我們學(xué)習(xí)的是OpenFeign,是Spring提供的。
OpenFeign是一種聲明式、模板化的HTTP客戶端(僅在Application Client中使用)(稱OpenFeign作用:聲明式服務(wù)調(diào)用)。聲明式調(diào)用是指,就像調(diào)用本地方法一樣調(diào)用遠(yuǎn)程方法,無需感知操作遠(yuǎn)程http請(qǐng)求。學(xué)習(xí)完OpenFeign后可以不使用RestTemplate進(jìn)行調(diào)用。
Spring Cloud的聲明式調(diào)用, 可以做到使用 HTTP請(qǐng)求遠(yuǎn)程服務(wù)時(shí)能就像調(diào)用本地方法一樣的體驗(yàn),開發(fā)者完全感知不到這是遠(yuǎn)程方法,更感知不到這是個(gè)HTTP請(qǐng)求。Feign的應(yīng)用,讓Spring Cloud微服務(wù)調(diào)用像Dubbo一樣,Application Client直接通過接口方法調(diào)用Application Service,而不需要通過常規(guī)的RestTemplate構(gòu)造請(qǐng)求再解析返回?cái)?shù)據(jù)。它解決了讓開發(fā)者調(diào)用遠(yuǎn)程接口就跟調(diào)用本地方法一樣,無需關(guān)注與遠(yuǎn)程的交互細(xì)節(jié),更無需關(guān)注分布式環(huán)境開發(fā)。
使用OpenFeign時(shí)就好像在寫控制器方法,OpenFeign都是寫在接口中,在聲明的方法上添加SpringMVC注解或聲明的參數(shù)上添加SpringMVC注解就可以完成調(diào)用遠(yuǎn)程的控制器方法。
OpenFeign用途
openfeign的用途:服務(wù)發(fā)現(xiàn),負(fù)載均衡,服務(wù)調(diào)用
openfeign的實(shí)現(xiàn)原理:基于@EnableFeignClients 將所有被@FeignClient注解的類 注冊(cè)到容器中。當(dāng)這些被@FeignClient注解的類被調(diào)用時(shí)會(huì)創(chuàng)建一個(gè)動(dòng)態(tài)代理的對(duì)象為我們創(chuàng)建被調(diào)用類的實(shí)例,然后都會(huì)被統(tǒng)一轉(zhuǎn)發(fā)給 Feign 框架所定義的一個(gè) InvocationHandler , 由該 Handler 完成后續(xù)的 HTTP 轉(zhuǎn)換, 發(fā)送, 接收, 翻譯HTTP響應(yīng)的工作。本文主要介紹OpenFeign服務(wù)調(diào)用的用途,實(shí)現(xiàn)微服務(wù)間的遠(yuǎn)程調(diào)用。
具體案例
springboot版本為2.3.5
<parent> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-parent</artifactId> ? ? <version>2.3.5.RELEASE</version> ? ? <relativePath/> <!-- lookup parent from repository --> </parent>
pom依賴
<!-- openfeign --> ? ? ?<dependency> ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? <artifactId>spring-cloud-starter-openfeign</artifactId> ? ? ? ? <version>2.2.5.RELEASE</version> ? ? </dependency> ? ? ?<dependency> ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> ? ? ?<version>2.2.5.RELEASE</version> </dependency>
服務(wù)請(qǐng)求方
1.在springboot啟動(dòng)類中添加注解@EnableFeignClients
package com.anjiplus.template.gaea.business;
import com.anji.plus.gaea.annotation.enabled.EnabledGaeaConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.oas.annotations.EnableOpenApi;
// openFeign注解,括號(hào)內(nèi)容為遠(yuǎn)程調(diào)用類包路徑
@EnableFeignClients("com.anjiplus.template.gaea.business.modules.model.openFeign")
@EnableOpenApi
public class ReportApplication {
? ? public static void main( String[] args ) {
? ? ? ? SpringApplication.run(ReportApplication.class);
? ? }
}2.添加遠(yuǎn)程調(diào)用接口類,接口類上需要使用@FeignClient注解標(biāo)注調(diào)用服務(wù)的服務(wù)名和服務(wù)地址
package com.anjiplus.template.gaea.business.modules.model.openFeign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
/**
?* @author 莫須有
?* @Date 2022/6/10 15:52
?* @Description openFeign服務(wù)調(diào)用類
?*/
//注解表示服務(wù)調(diào)用的服務(wù)名稱和服務(wù)地址
@FeignClient(value = "kg-data-manage", url = "http://127.0.0.1:10100/datamanage")
@Component
public interface ModelOpenFeign {
? ? /**
? ? ?* @Description 查詢用戶下所用的模型信息
? ? ?* @author 莫須有
? ? ?* @date 2022/6/17
? ? ?* @param userId 用戶編號(hào)
? ? ?* @return list
? ? ?*/
? ? @GetMapping("/dashBoard/getFactory")// 調(diào)用的路徑為服務(wù)提供方接口請(qǐng)求路徑
? ? String getFactoryList(@RequestParam("userId") String userId);
? ? @GetMapping("/dashBoard/getPointList")
? ? String getPointList(@RequestParam("taskId") String factoryId);
? ? @GetMapping("/dashBoard/getData")
? ? String getData(@RequestParam("sql") String sql);
}服務(wù)提供方
服務(wù)提供方只需像正常的controller層接口一樣編寫就可以,不需要額外的配置,根據(jù)需要在controller層進(jìn)行接口開發(fā),然后再service層中做具體的實(shí)現(xiàn)即可,需要注意的是請(qǐng)求參數(shù)和返回參數(shù)的類型需要兩邊一致,這是必須滿足的。
package com.xasj.controller.model.openFeign;
import com.xasj.entity.model.vo.DashBoardModelInfo;
import com.xasj.entity.model.vo.DashBoardPointInfo;
import com.xasj.service.model.openFeign.DashBoardOpenFeignService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
?* @author 莫須有
?* @Date 2022/6/10 17:52
?* @Description openFeign服務(wù)提供類
?*/
@RestController
public class DashBoardOpenFeignController {
? ? @Resource
? ? private DashBoardOpenFeignService dashBoardOpenFeignService;
? ? @GetMapping("/dashBoard/getFactory")
? ? public List<DashBoardModelInfo> getFactoryList(@RequestParam(name = "userId") String userId){
? ? ? ? return dashBoardOpenFeignService.getFactoryList(userId);
? ? }
? ? @GetMapping("/dashBoard/getPointList")
? ? public List<DashBoardPointInfo> getPointList(@RequestParam(name = "taskId")String taskId){
? ? ? ? return dashBoardOpenFeignService.getPointList(taskId);
? ? }
? ? @GetMapping("/dashBoard/getData")
? ? public List getData(@RequestParam("sql") String sql){
? ? ? ? return dashBoardOpenFeignService.getData(sql);
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java?C++題解leetcode672燈泡開關(guān)示例
這篇文章主要為大家介紹了Java?C++題解leetcode672燈泡開關(guān)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Spring Cache監(jiān)控配置與使用規(guī)范的建議
這篇文章主要介紹了Spring Cache監(jiān)控配置與使用規(guī)范的建議,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析
這篇文章主要為大家介紹了微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例
這篇文章主要為大家介紹了ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
詳解java接口(interface)在不同JDK版本中的變化
這篇文章主要介紹了詳解java接口(interface)在不同JDK版本中的變化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
基于Java實(shí)現(xiàn)一個(gè)簡單的數(shù)據(jù)同步組件
這篇文章主要為大家詳細(xì)介紹了如何基于Java實(shí)現(xiàn)一個(gè)簡單的數(shù)據(jù)同步組件,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2023-06-06
Java web網(wǎng)站訪問量的統(tǒng)計(jì)
這篇文章主要為大家詳細(xì)介紹了Java web網(wǎng)站訪問量的統(tǒng)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

