1.OpenFeign
上篇文章我们利用Nacos实现了服务的治理,利用利用RestTemplate实现了服务的远程调用。但是远程调用的代码太复杂了:

而且这种调用方式,与原本的本地方法调用差异太大,编程时的体验也不统一,一会儿远程调用,一会儿本地调用。因此,我们必须想办法改变远程调用的开发模式,让远程调用像本地方法调用一样简单。而这就要用到OpenFeign组件了。
其实远程调用的关键点就在于四个:
-  
请求方式
 -  
请求路径
 -  
请求参数
 -  
返回值类型
 
所以,OpenFeign就利用SpringMVC的相关注解来声明上述4个参数,然后基于动态代理帮我们生成远程调用的代码,而无需我们手动再编写,非常方便。
1.1 快速入门
还是以cart-service中的查询我的购物车为例。因此下面的操作都是在cart-service中进行。
1.1.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> 
1.2 启动OpenFeign
在cart-service的CartApplication启动类上添加注解,启动OpenFeign功能:

1.3 编写OpenFeign客户端
在cart-service中,定义一个新的接口:
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.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>。我们只需要直接调用这个方法,即可实现远程调用了。
1.4 使用FeignClient
最后,我们在cart-service的com.hmall.cart.service.impl.CartServiceImpl中改造代码,直接调用ItemClient的方法:

feign替我们完成了服务拉取、负载均衡、发送http请求的所有工作。
2. 连接池
Feign底层发起http请求,依赖于其它的框架。其底层支持的http客户端实现包括:
-  
HttpURLConnection:默认实现,不支持连接池
 -  
Apache HttpClient :支持连接池
 -  
OKHttp:支持连接池
 
因此我们通常会使用带有连接池的客户端来代替默认的HttpURLConnection。比如,我们使用OK Http.
2.1 引入依赖
在cart-service的pom.xml中引入依赖:
<!--OK http 的依赖 -->
<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-okhttp</artifactId>
</dependency> 
2.2 开启连接池
在cart-service的application.yml配置文件中开启Feign的连接池功能:
feign:
  okhttp:
    enabled: true # 开启OKHttp功能 
重启服务,连接池就生效了。
2.3 最佳实践
其他微服务,也可能需要远程调用item-service中根据id批量查询商品功能。这个需求与cart-service中是一样的。因此,我们就需要在trade-service中再次定义ItemClient接口,这不是重复编码吗? 有什么办法能加避免重复编码呢?
2.3.1 思路分析
相信大家都能想到,避免重复编码的办法就是抽取。不过这里有两种抽取思路:
-  
思路1:抽取到微服务之外的公共module
 -  
思路2:每个微服务自己抽取一个module
 
如图:

方案1抽取更加简单,工程结构也比较清晰,但缺点是整个项目耦合度偏高。
方案2抽取相对麻烦,工程结构相对更复杂,但服务之间耦合度降低。
由于item-service已经创建好,无法继续拆分,因此这里我们采用方案1.
2.3.2 抽取Feign客户端
在hmall下定义一个新的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">
    <parent>
        <artifactId>hmall</artifactId>
        <groupId>com.heima</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>hm-api</artifactId>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </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> 
然后把ItemDTO和ItemClient都拷贝过来,最终结构如下:

2.3.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,重启项目,发现报错了:

这里因为ItemClient现在定义到了com.hmall.api.client包下,而cart-service的启动类定义在com.hmall.cart包下,扫描不到ItemClient,所以报错了。
解决办法很简单,在cart-service的启动类上添加声明即可,两种方式:
-  
方式1:声明扫描包:
 

-  
方式2:声明要用的FeignClient
 

2.4 日志配置
OpenFeign只会在FeignClient所在包的日志级别为DEBUG时,才会输出日志。而且其日志级别有4级:
-  
NONE:不记录任何日志信息,这是默认值。
 -  
BASIC:仅记录请求的方法,URL以及响应状态码和执行时间
 -  
HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
 -  
FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。
 
Feign默认的日志级别就是NONE,所以默认我们看不到请求日志。
定义日志级别:
在hm-api模块下新建一个配置类,定义Feign的日志级别:

让日志级别生效,还需要配置这个类。有两种方式:
-  
局部生效:在某个
FeignClient中配置,只对当前FeignClient生效 
@FeignClient(value = "item-service", configuration = DefaultFeignConfig.class) 
-  
全局生效:在
@EnableFeignClients中配置,针对所有FeignClient生效。 
@EnableFeignClients(defaultConfiguration = DefaultFeignConfig.class) 
                


















