7 OpenFeign
7.1 问题引出
-
在上一章,我们利用Nacos实现了服务的治理,利用RestTemplate实现了服务的远程调用。但是我们在
6.5.3 发现并调用服务
中编写的远程调用的代码太复杂了,而且这种调用方式,与原本的本地方法调用差异太大,编程时的体验也不统一,一会儿远程调用,一会儿本地调用; -
因此,必须想办法改变远程调用的开发模式,让远程调用像本地方法调用一样简单。而这就要用到 OpenFeign 组件了;
-
其实远程调用的关键点就在于四个:
- 请求方式
- 请求路径
- 请求参数
- 返回值类型
-
所以,OpenFeign 就利用 SpringMVC 的相关注解来声明上述4个参数,然后基于动态代理帮我们生成远程调用的代码,而无需我们手动再编写,非常方便。
7.2 快速入门
7.2.1 引入依赖
-
在
cart-service
服务的pom.xml
中引入OpenFeign
的依赖和loadBalancer
依赖:<!--openFeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--负载均衡器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
7.2.2 启用OpenFeign
-
接下来,在
cart-service
的CartApplication
启动类上添加注解,启动 OpenFeign 功能:@EnableFeignClients
7.2.3 编写OpenFeign客户端
-
在
cart-service
中,定义一个ItemClient
接口,编写Feign客户端:package com.hmall.cart.client; import com.hmall.cart.domain.dto.ItemDTO; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import java.util.Collection; import java.util.List; @FeignClient("item-service") public interface ItemClient { @GetMapping("/items") List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> ids); }
@FeignClient("item-service")
:声明服务名称;@GetMapping
:声明请求方式;@GetMapping("/items")
:声明请求路径;@RequestParam("ids") Collection<Long> ids
:声明请求参数;List<ItemDTO>
:返回值类型;
-
有了上述信息,OpenFeign就可以利用动态代理帮我们实现这个方法,并且向
http://item-service/items
发送一个GET
请求,携带ids
为请求参数,并自动将返回值处理为List<ItemDTO>
; -
只需要直接调用这个方法,即可实现远程调用了。
7.2.4 使用FeignClient
-
在
cart-service
的com.hmall.cart.service.impl.CartServiceImpl
中改造代码,直接调用ItemClient
的方法:private final ItemClient itemClient; private void handleCartItems(List<CartVO> vos) { // 1.获取商品id Set<Long> itemIds = vos.stream().map(CartVO::getItemId).collect(Collectors.toSet()); // 2.查询商品 List<ItemDTO> items = itemClient.queryItemByIds(itemIds); //直接调用 ItemClient 接口中的方法 if (CollUtils.isEmpty(items)) { return; } // 3. 转为 id 到 item 的map Map<Long, ItemDTO> itemMap = items.stream().collect(Collectors.toMap(ItemDTO::getId, Function.identity())); // 4.写入vo for (CartVO v : vos) { ItemDTO item = itemMap.get(v.getItemId()); if (item == null) { continue; } v.setNewPrice(item.getPrice()); v.setStatus(item.getStatus()); v.setStock(item.getStock()); } }
-
OpenFeign 替我们完成了服务拉取、负载均衡、发送http请求的所有工作,是不是看起来优雅多了?而且,这里我们不再需要RestTemplate了,还省去了RestTemplate的注册。
7.3 连接池
-
OpenFeign 底层发起http请求,依赖于其它的框架。其底层支持的http客户端实现包括:
- HttpURLConnection:默认实现,不支持连接池
- Apache HttpClient :支持连接池
- OKHttp:支持连接池
-
因此我们通常会使用带有连接池的客户端来代替默认的
HttpURLConnection
。比如,我们使用OKHttp。
7.3.1 引入依赖
-
在
cart-service
的pom.xml
中引入依赖:<!--OK http 的依赖 --> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-okhttp</artifactId> </dependency>
7.3.2 开启连接池
-
在
cart-service
的application.yml
配置文件中开启Feign的连接池功能:# OKHttp feign: okhttp: enabled: true
-
重启服务,连接池就生效了。
7.4 最佳实践
-
将来我们要把与下单有关的业务抽取为一个独立微服务:
trade-service
,不过我们先来看一下hm-service
中原本与下单有关的业务逻辑:-
入口在
com.hmall.controller.OrderController
的createOrder
方法,然后调用了IOrderService
中的createOrder
方法; -
由于下单时前端提交了商品id,为了计算订单总价,需要查询商品信息:
-
也就是说,如果拆分了交易微服务(
trade-service
),它也需要远程调用item-service
中的根据id批量查询商品功能。这个需求与cart-service
中是一样的; -
因此,我们就需要在
trade-service
中再次定义ItemClient
接口,这不是重复编码吗? 有什么办法能加避免重复编码呢?
-
7.4.1 思路分析
-
避免重复编码的办法就是抽取。不过这里有两种抽取思路:
- 思路1:抽取到微服务之外的公共module
- 思路2:每个微服务自己抽取一个module
-
方案1抽取更加简单,工程结构也比较清晰,但缺点是整个项目耦合度偏高;
-
方案2抽取相对麻烦,工程结构相对更复杂,但服务之间耦合度降低;
-
由于item-service已经创建好,无法继续拆分,因此这里采用方案1。
7.4.2 抽取Feign客户端
-
在父工程下定义一个新的module,命名为
hm-api
: -
依赖如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.heima</groupId> <artifactId>hmall</artifactId> <version>1.0.0</version> </parent> <artifactId>hm-api</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--open feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- load balancer--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> <!-- swagger 注解依赖 --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.6.6</version> <scope>compile</scope> </dependency> </dependencies> </project>
cart-service
的pom.xml
中的 openFeign 和 负载均衡器 这两个依赖就可以注释掉,因为其他模块都会引用hm-api
模块,所以也都相当于引入了这两个依赖;
-
把
ItemDTO
和ItemClient
都拷贝过来,最终结构如下: -
现在,任何微服务要调用
item-service
中的接口,只需要引入hm-api
模块依赖即可,无需自己编写Feign客户端了。
7.4.3 扫描包
-
在
cart-service
的pom.xml
中引入hm-api
模块:<!--feign模块--> <dependency> <groupId>com.heima</groupId> <artifactId>hm-api</artifactId> <version>1.0.0</version> </dependency>
-
删除
cart-service
中原来的ItemDTO
和ItemClient
,然后修改 CartServiceImpl 中对ItemClient
和ItemDTO
的引用:import com.hmaall.api.client.ItemClient; import com.hmaall.api.domain.dto.ItemDTO;
-
重启
CartApplication
后,会发现报错如下: -
这是因为
ItemClient
现在定义到了com.hmall.api.client
包下,而cart-service
的启动类定义在com.hmall.cart
包下,扫描不到ItemClient
,所以报错了; -
在
cart-service
的启动类上添加声明即可,两种方式:-
方式1:声明扫描包:
-
方式2:声明要用的FeignClient
-
7.5 日志配置
7.5.1 基本介绍
- OpenFeign只会在FeignClient所在包的日志级别为DEBUG时,才会输出日志。而且其日志级别有4级:
- NONE:不记录任何日志信息,这是默认值。
- BASIC:仅记录请求的方法,URL以及响应状态码和执行时间
- HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
- FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据
- Feign默认的日志级别就是NONE,所以默认我们看不到请求日志。
7.5.2 定义日志级别
-
在
hm-api
模块下新建一个配置类,定义Feign的日志级别:
7.5.3 配置
-
接下来,要让日志级别生效,有两种使用方式:
-
局部生效:在某个
FeignClient
中配置,只对当前FeignClient
生效@FeignClient(value = "item-service", configuration = DefaultFeignConfig.class)
-
全局生效:在
@EnableFeignClients
中配置,针对所有FeignClient
生效@EnableFeignClients(defaultConfiguration = DefaultFeignConfig.class)
-
-
修改
cart-service
中的启动类:@EnableFeignClients(basePackages = "com.hmall.api.client", defaultConfiguration = DefaultFeignConfig.class)
-
使用接口文档,调用查询购物车列表接口,输出日志如下: