系列文章目錄
第一章 Java線程池技術(shù)應(yīng)用
第二章 CountDownLatch和Semaphone的應(yīng)用
第三章 Spring Cloud 簡(jiǎn)介
第四章 Spring Cloud Netflix 之 Eureka
第五章 Spring Cloud Netflix 之 Ribbon
第六章 Spring Cloud 之 OpenFeign
前言
OpenFeign 全稱 Spring Cloud OpenFeign,它是 Spring 官方推出的一種聲明式服務(wù)調(diào)用與負(fù)載均衡組件。我們可以像調(diào)用本地方法一樣來(lái)調(diào)用遠(yuǎn)程服務(wù),而完全感覺(jué)不到這是在進(jìn)行遠(yuǎn)程調(diào)用。
1、OpenFeign的實(shí)現(xiàn)原理和過(guò)程
OpenFeign是一個(gè)聲明式的Web服務(wù)客戶端,它使得編寫(xiě)Web服務(wù)客戶端變得更加簡(jiǎn)單。通過(guò)使用OpenFeign,開(kāi)發(fā)者可以更加容易地創(chuàng)建與遠(yuǎn)程服務(wù)進(jìn)行交互的客戶端代碼。
OpenFeign通過(guò)自動(dòng)封裝HTTP請(qǐng)求和簡(jiǎn)化遠(yuǎn)程調(diào)用代碼的過(guò)程,使得開(kāi)發(fā)者可以更加專注于業(yè)務(wù)邏輯的實(shí)現(xiàn),提高了開(kāi)發(fā)效率和代碼可讀性。
1.1、OpenFeign支持Spring Cloud體系內(nèi)的所有注解
例如@GetMapping、@PostMapping、@PathVariable、@RequestParam等,同時(shí)也支持自定義注解。
1.2、OpenFeign自動(dòng)封裝HTTP請(qǐng)求
包括HTTP方法、請(qǐng)求URL、請(qǐng)求參數(shù)、請(qǐng)求頭等,開(kāi)發(fā)者無(wú)需手動(dòng)編寫(xiě)HTTP請(qǐng)求代碼。只需要定義一個(gè)接口,并通過(guò)注解指定接口的路徑和方法,OpenFeign就會(huì)自動(dòng)發(fā)送HTTP請(qǐng)求并解析響應(yīng)數(shù)據(jù)。
1.3、OpenFeign支持請(qǐng)求攔截器和響應(yīng)攔截器
開(kāi)發(fā)者可以通過(guò)實(shí)現(xiàn)請(qǐng)求攔截器和響應(yīng)攔截器來(lái)對(duì)HTTP請(qǐng)求和響應(yīng)進(jìn)行處理,例如添加認(rèn)證信息、重試等。
1.4、在Spring項(xiàng)目中的過(guò)程
在Spring項(xiàng)目啟動(dòng)中,OpenFeign會(huì)發(fā)起一個(gè)主動(dòng)掃包的過(guò)程。從指定目錄下加載所有被@FeignClient修飾的接口,將這些接口轉(zhuǎn)換為Bean,交給Spring來(lái)管理。這些接口會(huì)經(jīng)過(guò)MVC Constract解析,將方法上的注解解析出來(lái)放到MethodMetadata中。每一個(gè)FeignClient接口會(huì)生成一個(gè)動(dòng)態(tài)代理對(duì)象,指向包含方法的MethodHandler的HashMap。
1.5、遠(yuǎn)程調(diào)用
當(dāng)服務(wù)A發(fā)起遠(yuǎn)程調(diào)用時(shí),它會(huì)從動(dòng)態(tài)代理proxy中找到一個(gè)MethodHandler的實(shí)例,生成request,包含請(qǐng)求的url。經(jīng)過(guò)負(fù)載均衡算法找到一個(gè)服務(wù)的IP地址,拼接請(qǐng)求的URL。服務(wù)B處理服務(wù)A發(fā)起的遠(yuǎn)程調(diào)用,執(zhí)行邏輯后,返回響應(yīng)給A。
2、常用注解
使用 OpenFegin 進(jìn)行遠(yuǎn)程服務(wù)調(diào)用時(shí),常用注解如下表。
注解 | 說(shuō)明 |
---|---|
@FeignClient | 該注解用于通知 OpenFeign 組件對(duì) @RequestMapping 注解下的接口進(jìn)行解析,并通過(guò)動(dòng)態(tài)代理的方式產(chǎn)生實(shí)現(xiàn)類,實(shí)現(xiàn)負(fù)載均衡和服務(wù)調(diào)用。 |
@EnableFeignClients | 該注解用于開(kāi)啟 OpenFeign 功能,當(dāng) Spring Cloud 應(yīng)用啟動(dòng)時(shí),OpenFeign 會(huì)掃描標(biāo)有 @FeignClient 注解的接口,生成代理并注冊(cè)到 Spring 容器中。 |
@RequestMapping | Spring MVC 注解,在 Spring MVC 中使用該注解映射請(qǐng)求,通過(guò)它來(lái)指定控制器(Controller)可以處理哪些 URL 請(qǐng)求,相當(dāng)于 Servlet 中 web.xml 的配置。 |
@GetMapping | Spring MVC 注解,用來(lái)映射 GET 請(qǐng)求,它是一個(gè)組合注解,相當(dāng)于 @RequestMapping(method = RequestMethod.GET) 。 |
@PostMapping | Spring MVC 注解,用來(lái)映射 POST 請(qǐng)求,它是一個(gè)組合注解,相當(dāng)于 @RequestMapping(method = RequestMethod.POST) 。 |
3、實(shí)踐
3.1、修改pom.xml配置
在上一章節(jié) [《微服務(wù)實(shí)戰(zhàn)》 第五章 Spring Cloud Netflix 之 Ribbon ] 基礎(chǔ)上,添加OpenFeign依賴項(xiàng)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.kelvin</groupId>
<artifactId>SpringCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>customer-api</artifactId>
<name>customer-api</name>
<description>customer-api</description>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--devtools 開(kāi)發(fā)工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--Spring Boot 測(cè)試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--junit 測(cè)試-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 修改后立即生效,熱部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.kelvin</groupId>
<artifactId>common-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
3.2、增加用戶服務(wù)接口,添加FeignClient配置
import com.kelvin.common.model.UserInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
/***
* @title 用戶服務(wù)接口
* @desctption 用戶服務(wù)接口
* @author kelvin
* @create 2023/5/11 16:44
**/
@Component
@FeignClient(value = "USER-SERVICE")
public interface UserService {
@RequestMapping(value = "/user/userInfoList",method = RequestMethod.GET)
public List<UserInfo> userInfoList();
}
3.3、修改控制層UserConsumerController
import com.kelvin.common.model.UserInfo;
import com.kelvin.customerapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/***
* @title UserConsumerController
* @desctption 用戶控制層
* @author kelvin
* @create 2023/5/11 14:22
**/
@RestController
@RequestMapping("/user")
public class UserConsumerController {
/* @Autowired
private UserConsumerService userConsumerService;*/
@Autowired
private UserService userService;
@GetMapping("/userInfoList")
public List<UserInfo> userInfoList(){
return userService.userInfoList();
}
}
3.4、啟動(dòng)類增加OpenFeign配置
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/***
* @title SpringBoot 啟動(dòng)類
* @desctption SpringBoot 啟動(dòng)類
* @author kelvin
* @create 2023/5/11 12:22
**/
@SpringBootApplication
@EnableFeignClients
public class CustomerApiApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApiApplication.class, args);
}
}
3.5、遠(yuǎn)程調(diào)用超時(shí)設(shè)置
openFeign其實(shí)是有默認(rèn)的超時(shí)時(shí)間的,默認(rèn)分別是連接超時(shí)時(shí)間10秒、讀超時(shí)時(shí)間60秒
可添加配置,自定義超時(shí)時(shí)間文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-713591.html
server:
port: 80
eureka:
client:
register-with-eureka: false #本微服務(wù)為服務(wù)消費(fèi)者,不需要將自己注冊(cè)到服務(wù)注冊(cè)中心
fetch-registry: true #本微服務(wù)為服務(wù)消費(fèi)者,需要到服務(wù)注冊(cè)中心搜索服務(wù)
service-url:
defaultZone: http://localhost:7001/eureka
feign:
client:
config:
default:
#建立連接所用的時(shí)間,適用于網(wǎng)絡(luò)狀況正常的情況下,兩端連接所需要的時(shí)間
connect-timeout: 5000
#指建立連接后從服務(wù)端讀取到可用資源所用的時(shí)間
read-timeout: 10000
4、總結(jié)
OpenFeign通過(guò)自動(dòng)封裝HTTP請(qǐng)求和簡(jiǎn)化遠(yuǎn)程調(diào)用代碼的過(guò)程,使得開(kāi)發(fā)者可以更加專注于業(yè)務(wù)邏輯的實(shí)現(xiàn),提高了開(kāi)發(fā)效率和代碼可讀性。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-713591.html
到了這里,關(guān)于云原生微服務(wù) 第六章 Spring Cloud中使用OpenFeign的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!