OpenFeign的小总结
先说明一下,我是在test-openfeign模块设置port为9003,服务注册名为openfeign-test
在test-openfeign模块设置port为9004,服务注册名为openfeign-test-consumer
要实现的是在9004接口通过openfeign来调用9003的接口
首先OpenFeign是什么: 作用是简化微服务架构中服务之间的调用
初级使用:首先加入相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
注意点1:如果不加以下依赖会报错,bean无法注入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
Feign客户端默认使用Spring Cloud的负载均衡器来处理对服务实例的调用,大家可以试下,不加的结果。
将其注册进consul或者nacos 我这里选的是nacos
关于服务注册我别的文章里面有
在主启动类上加上以下注解
@SpringBootApplication
@EnableFeignClients//开启feign客户端
@EnableDiscoveryClient//关于向使用nacos为注册中心时注册服务
public class OpenFeign {
public static void main(String[] args) {
SpringApplication.run(OpenFeign.class, args);
}
}
我们在9003创建一个接口
@RestController
public class OrderController {
@GetMapping("/order")
public String accTest(){//若括号里有属性则下面的两个也要添加,保证方法重载能找到 accTest(LoginDTO loginDto),则下面的也要加
return "这是一个订单类";
}
}
再然后,创建领一个模块 9004,在service层
@FeignClient(name = "openfeign-test")//这个是在nacos中注册的名字
public interface TestFeign {
@GetMapping("/order")
public String accTest();
}
在此解释作用,如上图,@FeignClient(name= “需要使用openfeign的moudle的application.name”),也就是服务注册名
server:
port: 9003
spring:
application:
name: openfeign-test
cloud:
nacos:
discovery:
server-addr: localhost:8848
接着,在9004中创建相关的类进行接口调用,这里简单测试一下,在controller层进行调用
@RestController
public class ConsumerController {
@Autowired
private TestFeign testFeign;
@GetMapping("consumer")
public String con(){
String accTest=testFeign.accTest();
return accTest;
}
}
这里看到访问成功
初级阶段完成
深入
- 超时控制
什么是超时控制,我们要求服务提供方处理业务逻辑时间必须在1S内返回,如果超过1S没有返回则OpenFeign会直接报错,不会等待服务执行,但是往往在处理复杂业务逻辑是可能会超过1S,因此需要修改OpenFeign的默认服务调用超时时间。假设OpenFeign客户端默认等待时间是60秒,现在我们认为60秒不合适,想定义一个我们自己认为合适的时间,叫超时控制。主要修改的是两点,
spring:
spring:
profiles:
active: dev #多环境加载默认dev,不写默认default
application:
name: service-user-openfeign-test
main:
web-application-type: reactive
cloud:
openfeign:
client:
config:
#default:此为全局配置,即所有都生效如果想使某个生效,则使用如下
service-user-test: #此为单独配置只有user这个moudle的功能视线下面的超时控制
#连接超时时间
connect-timeout: 3000
#读取超时时间
read-timeout: 2000
或者如下图,则会出现没有配置的默认跟全局配置,有单独配置的覆盖全局配置,跟自己
spring:
profiles:
active: dev #多环境加载默认dev,不写默认default
application:
name: service-user-openfeign-test
main:
web-application-type: reactive
cloud:
openfeign:
client:
config:
default: #此为全局配置,即所有都生效如果想使某个生效,则使用如下
#连接超时时间
connect-timeout: 4000
#读取超时时间
read-timeout: 4000
service-user-test: #此为单独配置只有user这个moudle的功能视线下面的超时控制
#连接超时时间
connect-timeout: 3000
#读取超时时间
read-timeout: 2000
- 重试机制
Openfeign默认是没有重试的即,调用失败直接返回调用失败,在这里我们修改为重试三次调用,三次之后还是失败,再返回失败,这里可以和上文联系,service-user-test连接3次,每次最多三秒,共9秒,读取做多六秒
@Configuration
public class FeignConfig {
@Bean
//重写retryer
public Retryer retryer(){
//return Retryer.NEVER_RETRY;//默认不走重试策略
return new Retryer.Default(100,1,3);
//初始间隔时间是100ms,重试间最大间隔时间是1s,尝试3次连接
}
}
- 性能优化
由httpclient4改为httpclient5
添加依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5-fluent -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5-fluent</artifactId>
<version>5.1.3</version>
</dependency>
改yml 在openfeign下,和client平级
openfeign:
httpclient:
hc5:
enabled: true
- 请求回应压缩
spring:
profiles:
active: dev #多环境加载默认dev,不写默认default
application:
name: service-user-openfeign-test
main:
web-application-type: reactive
cloud:
openfeign:
httpclient:
hc5:
enabled: true
#对请求和响应进行压缩,减少性能损耗
compression:
request:
enabled: true
mime-types: application/json, application/xml, text/xml, text/html, text/plain #出发压缩的数据类型
min-request-size: 1024 # 触发压缩的最小阈值
response:
enabled: true
- 日志打印feign日志
在yml文件中添加以下内容 和spring同级
logging:
level:
com:
vinta:
cloud:
apis:
PayFeignAPI: debug
修改config配置类
@Configuration
public class FeignConfig {
@Bean
//重写retryer
public Retryer retryer(){
//return Retryer.NEVER_RETRY;//默认不走重试策略
return new Retryer.Default(100,1,3);
//初始间隔时间是100ms,重试间最大间隔时间是1s,尝试3次连接
}
@Bean
Logger.Level logger() {
return Logger.Level.FULL; //此为feign的日志级别,在此我们设置的是全面打印
}
}
四个级别 NONE 默认不显示任何日志
Basic 仅记录请求方法,url,响应状态码及执行时间
HEADERS 除Basic中的信息外还由响应和请求头信息
FULL 上述代码中的注释