目录
温馨提示:全套教程请查看 教程总览
前言
在Spring Cloud微服务架构中,大部分公司都是利用 OpenFeign 进行服务间的调用,而比较简单的业务使用默认配置是不会有多大问题的,但是如果业务比较复杂,服务要进行比较繁杂的业务计算,那定制化配置就有必要了。
一、超时控制
默认 OpenFeign 客户端等待60秒钟,这会导致等待时间过长而阻塞调用者,并且也会让应用体验很差,所以我们要根据自己的业务自定义超时控制。
我们可以在默认客户端和命名客户端上配置超时。OpenFeign 使用两个超时参数:
- connectTimeout 防止因服务器处理时间过长而阻塞调用者。
- readTimeout 从连接建立时开始应用,当返回响应的时间过长时就会被触发。
在配置文件中开启配置:
- connectTimeout:连接超时时间(单位:毫秒)
- readTimeout:请求处理超时时间(单位:毫秒)
示例如下:
spring:
cloud:
openfeign:
client:
config:
default:
#连接超时时间
connectTimeout: 5000
#读取超时时间
readTimeout: 5000
二、重试机制
A bean of
Retryer.NEVER_RETRY
with the typeRetryer
is created by default, which will disable retrying.
默认 OpenFeign 是将重试机制关闭的,我们可以根据自己的系统业务开启重试机制,并配置重试次数。
1.改造cloud-consumer-feign服务
cloud-consumer-feign
服务搭建详见 6 - OpenFeign服务接口调用
1.1 新增配置类
新建 FeignConfig.java
内容如下:
package org.ash.consumer.feign.config;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
@Bean
public Retryer myRetryer() {
// 初始间隔时间为100ms,最大间隔时间为1s,重试2次(总共请求3次,包括第1次,所以重试次数为2次)
return new Retryer.Default(100, 1, 3);
}
}
1.2 改造API
改造 ConsumerFeignController.java
的API,内容如下:
package org.ash.consumer.feign.controller;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.ash.consumer.feign.service.FeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class ConsumerFeignController {
@Resource
private FeignService feignService;
@GetMapping("consumer/feign/test/{message}")
public String test(@PathVariable String message) {
long start = System.currentTimeMillis();
log.info("调用provider开始,{}", start);
String result = null;
try {
result = feignService.getProviderTest(message);
} catch (Exception e) {
log.info("调用provider结束,{}ms", System.currentTimeMillis()-start);
throw new RuntimeException(e);
}
return result;
}
}
1.3 添加配置
添加OpenFeign超时配置,设置超时时间为5s,内容如下:
server:
port: 9003
spring:
application:
# 服务名称
name: cloud-consumer-feign
cloud:
nacos:
# nacos注册中心
discovery:
# 服务ip:port
server-addr: 127.0.0.1:8848
openfeign:
client:
config:
default:
#连接超时时间
connectTimeout: 5000
#读取超时时间
readTimeout: 5000
management:
endpoints:
web:
exposure:
include: '*'
2.改造cloud-provider服务
cloud-provider
服务搭建详见 3 - 服务注册与发现 —— 服务提供者
改造 ProviderController.java
的测试API,为了体现响应超时,则改造如下:
package org.ash.provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@GetMapping(value = "/provider/test/{message}")
public String test(@PathVariable String message) throws InterruptedException {
Thread.sleep(6000);
return "服务提供者:" + message;
}
}
3.运行测试
3.1 启动项目
启动服务提供者 cloud-provider
服务
启动服务消费者 cloud-consumer-feign
服务
注意:如果启动控制台报错,请检查 Nacos server 是否已启动
3.2 调用测试接口
打开浏览器调用我们之前写的测试接口 http://localhost:9003/consumer/feign/test/open-feign
,并且查看消费者 cloud-consumer-feign
服务的控制台
查看日志可以看到,调用耗时大概是15s
,正好是设置的5s
超时的3倍,则表示设置重试机制成功
三、HttpClient替换
OpenFeign 中 http client
如果不做特殊配置,OpenFeign默认使用JDK自带的 HttpURLConnection
发送HTTP请求,由于默认 HttpURLConnection
没有连接池,性能和效率比较低,所以我们可以替换 http client
。
这里我们可以替换为 Apache HttpClient 5。
1.引入依赖
<!-- httpclient5-->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3</version>
</dependency>
<!-- feign-hc5-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hc5</artifactId>
<version>13.1</version>
</dependency>
2.添加配置
spring:
cloud:
openfeign:
httpclient:
hc5:
enabled: true
四、请求响应压缩
Spring Cloud OpenFeign支持对请求和响应进行GZIP压缩,以减少通信过程中的性能损耗。
通过下面的两个参数设置,就能开启请求与相应的压缩功能:
spring.cloud.openfeign.compression.request.enabled=true
spring.cloud.openfeign.compression.response.enabled=true
还可以对请求压缩做一些更细致的设置,比如下面的配置内容可以指定压缩的请求数据类型,并设置了请求压缩的大小下限,只有超过这个大小的请求才会进行压缩:
spring.cloud.openfeign.compression.request.enabled=true
#触发压缩数据类型
spring.cloud.openfeign.compression.request.mime-types=text/xml,application/xml,application/json
#最小触发压缩的大小
spring.cloud.openfeign.compression.request.min-request-size=2048
五、日志打印
Feign 提供了日志打印功能,默认情况下,Feign 的日志只响应 DEBUG
级别。我们可以通过配置来调整日志级别,从而了解 Feign 中 Http 请求的细节,对Feign接口的调用情况进行监控和输出。
NONE:默认的,不显示任何日志;
BASIC:仅记录请求方法、URL、响应状态码及执行时间;
HEADERS:除了 BASIC 中定义的信息之外,还有请求和响应的头信息;
FULL:除了 HEADERS 中定义的信息之外,还有请求和响应的正文及元数据。
配置类 FeignConfig.java
中新增配置,内容如下:
package org.ash.consumer.feign.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel() {
// 日志级别配置
return Logger.Level.FULL;
}
}
至此,OpenFeign高级特性讲解完成!!!
温馨提示:全套教程请查看 教程总览