【Spring Cloud Alibaba】006-OpenFeign

113 阅读5分钟

【Spring Cloud Alibaba】006-OpenFeign

[toc]

一、概述

1、Java 项目实现接口调用的方法

Httpclient

HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 Http 协议的客户端编程工具包,并且它支持 HTTP 协议最新版本和建议。HttpClient 相比传统 JDK 自带的 URLConnection,提升了易用性和灵活性,使客户端发送 HTTP 请求变得容易,提高了开发的效率。

Okhttp

一个处理网络请求的开源项目,是安卓端最火的轻量级框架,由 Square 公司贡献,用于替代 HttpUrlConnection 和 Apache HttpClient。OkHttp 拥有简洁的 API、高效的性能,并支持多种协议(HTTP/2 和 SPDY)。

HttpURLConnection

HttpURLConnection 是 Java 的标准类,它继承自 URLConnection,可用于向指定网站发送 GET 请求、POST 请求。HttpURLConnection 使用比较复杂,不像 HttpClient 那样容易使用。

RestTemplate WebClient

RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程 HTTP 服务的方法,能够大大提高客户端的编写效率。

2、Feign 概述

Feign 是 Netflix 开发的声明式、模板化的 HTTP 客户端,其灵感来自 Retrofit、JAXRS-2.0以及 WebSocket。Feign 可帮助我们更加便捷、优雅地调用 HTTP API。

Feign 支持多种注解,例如 Feign 自带的注解或者 JAX-RS注 解等。

Spring Cloud openfeign 对 Feign 进行了增强,使其支持 Spring MVC 注解,另外还整合了 Ribbon 和 Nacos,从而使得 Feign 的使用更加方便。

Feign 可以做到使用 HTTP 请求远程服务时就像调用本地方法一样的体验,开发者完全感知不到这是远程方法,更感知不到这是个 HTTP 请求。它像 Dubbo 一样,consumer 直接调用接口方法调用 provider,而不需要通过常规的 Http Client 构造请求再解析返回数据。它解决了让开发者调用远程接口就跟调用本地方法一样,无需关注与远程的交互细节,更无需关注分布式环境开发。

二、Spring Cloud Alibaba 快速整合 OpenFeign

日常交流,Feign = OpenFeign

1、添加依赖

<!-- openFeign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、启动类加注解

@EnableFeignClients

3、编写调用接口+ @FeignClient 注解

package com.zibo.alibaba.order.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(name = "stock-service", path = "/stock")
public interface StockFeignService {

    @RequestMapping("/reduce")
    String reduce();

}

4、修改 OrderController

package com.zibo.alibaba.order.controller;

import com.zibo.alibaba.order.feign.StockFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private StockFeignService stockFeignService;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功!");
        // 远程调用库存服务
        String forObject = stockFeignService.reduce();
        return "Hello Feign 下单成功!" + forObject;
    }

}

5、重启项目并访问

image-20230222161644432

三、日志配置

1、四种日志级别

  • **NONE **【性能最佳,适用于生产】:不记录任何日志(默认值)。

  • BASIC 【适用于生产环境追踪问题】:仅记录请求方法、URL、响应状态代码以及执行时间。

  • HEADERS :记录BASIC级别的基础上,记录请求和响应的header。

  • FULL 【比较适用于开发及测试环境定位问题】:记录请求和响应的header、body 和元数据。

2、Feign 配置类

全局与局部配置

**全局配置:**在配置类上使用 @Configuration 注解;

**局部配置:**在 @FeignClient 注解中指定使用的配置类,不使用 @Configuration 注解。

配置类

全局配置

package com.zibo.alibaba.order.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {

    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

}

局部配置

@FeignClient(name = "stock-service", path = "/stock", configuration = FeignConfig.class)

设置 Spring Boot 的日志级别

默认是 info ,feign 的日志是 debug 级别,因此不会输出!

# spring boot 默认是 info ,feign 的日志是 debug 级别,因此不会输出!
logging:
  level:
    com.zibo.alibaba.order.feign: debug

运行测试

image-20230223130643973

3、yaml 写法

# feign 的日志级别
feign:
  client:
    config:
      stock-service: # 这个是服务名
        loggerLevel: full

四、契约配置

1、概述

Spring Cloud 在 Feign 的基础上做了扩展,使用 Spring MVC 的注解来完成 Feign 的功能。原生的 Feign 是不支持 Spring MVC 注解的,如果你想在 Spring Cloud 中使用原生的注解方式来定义客户端也是可以的,通过配置契约来改变这个配置,Spring Cloud 中默认的是 SpringMvcContract。

2、契约配置

在配置类中添加 Bean

/**
 * 修改契约配置,支持Feign原生的注解
 * @return Contract
 */
@Bean
public Contract feignContract() {
    return new Contract.Default();
}

修改使用方式

注意:修改契约配置后,OrderFeignService 不再支持 spring mvc 的注解,需要使用 Feign 原生的注解。

package com.zibo.alibaba.order.feign;

import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;

@FeignClient(name = "stock-service", path = "/stock")
public interface StockFeignService {

    @RequestLine("GET /reduce")
    String reduce();

}

带参数写法

@RequestLine("GET /reduce/{userId}")
String reduce(@Param("userId") Integer userId);

运行测试

image-20230223135302295

3、yaml 配置方式

feign:
  client:
    config:
      stock-service: # 这个是服务名
        loggerLevel: full
      contract: feign.Contract.Default # 指定 Feign 原生注解契约配置

五、超时时间配置

feign:
  client:
    config:
      stock-service: # 这个是服务名
        loggerLevel: full
        contract: feign.Contract.Default # 指定 Feign 原生注解契约配置
        # 连接超时时间
        connectTimeout: 5000
        # 读取超时时间
        readTimeout: 5000

六、自定义拦截器

1、拦截器

package com.zibo.alibaba.order.intercptor.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;

import java.util.UUID;

public class FeignAuthRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 添加请求头,下面写法仅作示例
        String access_token = UUID.randomUUID().toString();
        requestTemplate.header("Authorization",access_token);
        System.out.println("url is " + requestTemplate.url());
    }
}

2、配置类中添加

@Bean
public FeignAuthRequestInterceptor feignAuthRequestInterceptor() {
    return new FeignAuthRequestInterceptor();
}

3、运行测试

image-20230223141311205

七、配置客户端

1、说明

Feign 中默认使用 JDK 原生的 URLConnection 发送 HTTP 请求,我们可以集成别的组件来替换掉 URLConnection,比如 Apache HttpClient,OkHttp。

2、配置 Apache HttpClient

第一步:引入依赖

<!-- Apache HttpClient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
</dependency>
<!-- feign‐httpclient -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>12.1</version>
</dependency>

第二步:修改 yaml 配置

feign:
  # feign 使用 Apache HttpClient 可以忽略,默认开启
  httpclient:
    enabled: true

3、配置 OkHttp

第一步:引入依赖

<!-- OkHttp -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
    <version>12.1</version>
</dependency>

第二步:修改 yaml 配置

feign:
  # feign 使用 okhttp 作为 http 客户端
  httpclient:
    enabled: false
  okhttp:
    enabled: true

八、GZIP 压缩配置

开启压缩可以有效节约网络资源,提升接口性能,我们可以配置 GZIP 来压缩数据:

feign:
  # 配置 GZIP 来压缩数据
  compression:
    request:
      enabled: true
      # 配置压缩的类型
      mime-types: application/json
      # 配置压缩的最小数据量
      min-request-size: 2048
    response:
      enabled: true

注意:只有当 Feign 的 Http Client 不是 okhttp3 的时候,压缩才会生效,配置源码在 FeignAcceptGzipEncodingAutoConfiguration

image-20230223143017303

核心代码就是 @ConditionalOnMissingBean(type="okhttp3.OkHttpClient"),表示 Spring BeanFactory 中不包含指定的 bean 时条件匹配,也就是没有启用 okhttp3 时才会进行压缩配置。