OpenFegin使用
openFegin引入
官网:https://docs.spring.io/spring-cloud-openfeign/docs/3.1.4/reference/html/
github:
需要引入的关键配置,使用eureka做了配置中心,openfegin 使用的是3.1.4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
openFegin和fegin之间的区别
fegin不再维护的情况下,spring团队自己维护了一个openFegin,并且支持springmvc等注解;
配置
超时时间配置
feign:
client:
config:
default: #默认情况下的配置
connect-timeout: 1000 #单位毫秒
read-timeout: 5000 #单位毫秒
openfeign-2011-1: #这里对应规则有contextId的对应contextId,无contextId的对应name或者value, 有openfeign-2011-1配置的情况下,openfeign-2011-1配置会覆盖default配置
connect-timeout: 1000 #单位毫秒
read-timeout: 10000 #单位毫秒
openfeign-2011-2:
connect-timeout: 1000 #单位毫秒
read-timeout: 10000 #单位毫秒
目前仅这样配置生效,当然没有尝试代码配置;
重试机制
#重试机制
feign:
client:
config:
# default: # 全局生效
# retryer: com.itchun.retryer.DefaultFeignRetry
openfeign-2011-2: # 指定 【openfeign-2011-2】
retryer: com.itchun.retryer.FeginRetry2011_2
需要指定 retryer 配置类(com.itchun.retryer.FeginRetry2011_2)的地址:
package com.itchun.retryer;
import feign.RetryableException;
import feign.Retryer;
public class FeginRetry2011_2 implements Retryer {
@Override
public void continueOrPropagate(RetryableException e) {
throw e;
}
@Override
public Retryer clone() {
/**
* period:周期,重试间隔时间,毫秒
* maxPeriod:最大周期,重试间隔时间按照一定的规则逐渐增大,但不能超过最大周期
* maxAttempts:最大尝试次数,重试次数,这里2次,是指加上本身主动请求的一次,重试一次,一共两次,所以这里设置1的话,其实就不会重试
*/
return new Default(5000, Integer.MAX_VALUE, 3);
}
}
日志配置
feign:
client:
config:
# defalut: # defalut配置的未生效,全局的使用FeginLoggerConfig配置可以生效
# logger-level: FULL
openfeign-2011-1: # 可用
logger-level: FULL
logging:
level:
com.itchun.feignClient.ProductSuccessFeign: debug
#NONE: 没有日志记录
#BASIC: 日志仅记录请求方法、url、响应状态和执行时间
#HEADERS: 日志记录基本信息(同BASIC)以及请求头和响应头信息
#FULL: 日志记录请求和响应的头(headers)、体(body)、元数据(metadata)信息
如果要进行全局配置,发现使用 feign.client.config.defalut 不生效,需要使用如下代码:
package com.itchun.logger;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置全局可以用
*/
@Configuration
public class FeginLoggerConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
负载均衡
- Spring Cloud 2020版本以后,默认移除了对Netflix的依赖,其中就包括Ribbon,官方默认推荐使用Spring Cloud Loadbalancer正式替换Ribbon,并成为了Spring Cloud负载均衡器的唯一实现。
- 默认是轮询机制
- 还有根据xx算法的随机机制,待深入了解
熔断(hystrix)
涉及到fallback ,fallbackFactory 的配置,待了解
客户端
默认HttpURLConnection
在不引入 httpclient 工具情况下,默认使用 httpURLConnection,
配置 httpClient
听说只要引入 httpclient 配置就默认使用了 httpClient
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>12.0</version>
</dependency>
配置如下
#设置了。超时时间啊,都没有什么卵子用,但是确实使用httpclient了
feign:
httpclient: #httpclient客户端配置(全局)
enabled: true
connection-timeout: 1000 #连接超时时间(单位:毫秒)
time-to-live: 900 #线程存活时间(单位:秒)
max-connections: 200 #线程池最大连接数(全局)
max-connections-per-route: 50 #线程池最大连接数(单个HOST)
配置 okHttp
去除httpClient的pom配置,引入okHttp配置,这里没有指定版本,因为发现手动指定版本如果不对的话,发现会报classNotFound的错误,所以索性就直接使用springcloud默认指定的版本,我这里看了一下是11.8
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
配置
#设置了,okhttp启用了,但是设置的超时时间啊,都没起作用
feign:
okhttp:
enabled: true
httpclient:
enabled: false
connection-timeout: 10000 #连接超时时间(单位:毫秒)
ok-http:
read-timeout: 12
项目参考地址:https://github.com/itchun/springcloud/tree/master/openfeign