亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

OpenFeign實現(xiàn)遠程調(diào)用

 更新時間:2022年08月14日 09:31:30   作者:莫須有007  
這篇文章主要為大家詳細介紹了OpenFeign實現(xiàn)遠程調(diào)用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了OpenFeign遠程調(diào)用實現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下

什么是OpenFeign

OpenFeign目前是Spring Cloud 二級子項目。平時說的Feign指的是Netflix下的Feign,現(xiàn)在我們學習的是OpenFeign,是Spring提供的。

OpenFeign是一種聲明式、模板化的HTTP客戶端(僅在Application Client中使用)(稱OpenFeign作用:聲明式服務(wù)調(diào)用)。聲明式調(diào)用是指,就像調(diào)用本地方法一樣調(diào)用遠程方法,無需感知操作遠程http請求。學習完OpenFeign后可以不使用RestTemplate進行調(diào)用。

Spring Cloud的聲明式調(diào)用, 可以做到使用 HTTP請求遠程服務(wù)時能就像調(diào)用本地方法一樣的體驗,開發(fā)者完全感知不到這是遠程方法,更感知不到這是個HTTP請求。Feign的應(yīng)用,讓Spring Cloud微服務(wù)調(diào)用像Dubbo一樣,Application Client直接通過接口方法調(diào)用Application Service,而不需要通過常規(guī)的RestTemplate構(gòu)造請求再解析返回數(shù)據(jù)。它解決了讓開發(fā)者調(diào)用遠程接口就跟調(diào)用本地方法一樣,無需關(guān)注與遠程的交互細節(jié),更無需關(guān)注分布式環(huán)境開發(fā)。

使用OpenFeign時就好像在寫控制器方法,OpenFeign都是寫在接口中,在聲明的方法上添加SpringMVC注解或聲明的參數(shù)上添加SpringMVC注解就可以完成調(diào)用遠程的控制器方法。

OpenFeign用途

openfeign的用途:服務(wù)發(fā)現(xiàn),負載均衡,服務(wù)調(diào)用

openfeign的實現(xiàn)原理:基于@EnableFeignClients 將所有被@FeignClient注解的類 注冊到容器中。當這些被@FeignClient注解的類被調(diào)用時會創(chuàng)建一個動態(tài)代理的對象為我們創(chuàng)建被調(diào)用類的實例,然后都會被統(tǒng)一轉(zhuǎn)發(fā)給 Feign 框架所定義的一個 InvocationHandler , 由該 Handler 完成后續(xù)的 HTTP 轉(zhuǎn)換, 發(fā)送, 接收, 翻譯HTTP響應(yīng)的工作。本文主要介紹OpenFeign服務(wù)調(diào)用的用途,實現(xiàn)微服務(wù)間的遠程調(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ù)請求方

1.在springboot啟動類中添加注解@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注解,括號內(nèi)容為遠程調(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.添加遠程調(diào)用接口類,接口類上需要使用@FeignClient注解標注調(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 用戶編號
? ? ?* @return list
? ? ?*/
? ? @GetMapping("/dashBoard/getFactory")// 調(diào)用的路徑為服務(wù)提供方接口請求路徑
? ? 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層進行接口開發(fā),然后再service層中做具體的實現(xiàn)即可,需要注意的是請求參數(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?C++題解leetcode672燈泡開關(guān)示例

    Java?C++題解leetcode672燈泡開關(guān)示例

    這篇文章主要為大家介紹了Java?C++題解leetcode672燈泡開關(guān)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Spring Cache監(jiān)控配置與使用規(guī)范的建議

    Spring Cache監(jiān)控配置與使用規(guī)范的建議

    這篇文章主要介紹了Spring Cache監(jiān)控配置與使用規(guī)范的建議,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析

    微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析

    這篇文章主要為大家介紹了微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例

    ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例

    這篇文章主要為大家介紹了ServletWebServerApplicationContext創(chuàng)建Web容器Tomcat示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • SpringBootTest--踩坑錯誤的解決

    SpringBootTest--踩坑錯誤的解決

    這篇文章主要介紹了SpringBootTest--踩坑錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解java接口(interface)在不同JDK版本中的變化

    詳解java接口(interface)在不同JDK版本中的變化

    這篇文章主要介紹了詳解java接口(interface)在不同JDK版本中的變化,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • Swagger中@ApiIgnore注解的使用詳解

    Swagger中@ApiIgnore注解的使用詳解

    這篇文章主要介紹了Swagger中@ApiIgnore注解的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中的定時器Timer詳解

    Java中的定時器Timer詳解

    這篇文章主要為大家詳細介紹了Java定時器Timer的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 基于Java實現(xiàn)一個簡單的數(shù)據(jù)同步組件

    基于Java實現(xiàn)一個簡單的數(shù)據(jù)同步組件

    這篇文章主要為大家詳細介紹了如何基于Java實現(xiàn)一個簡單的數(shù)據(jù)同步組件,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解一下
    2023-06-06
  • Java web網(wǎng)站訪問量的統(tǒng)計

    Java web網(wǎng)站訪問量的統(tǒng)計

    這篇文章主要為大家詳細介紹了Java web網(wǎng)站訪問量的統(tǒng)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01

最新評論