Feign 是Netfilx开发的声明式、模板化的HTTP客户端,Feign可帮助我们更加便捷,优雅的调用HTTP API。Feign支持多种注解,例如Feign自带的注解或JAX-RS注解等等。Spring Cloud OpenFeign 对Feign进行了增强,使Feign支持了SpringMVC注解,另外还整合了Ribbon和Nacos,从而使得Feign的使用更加方便
可以做到使用HTTP请求远程服务时就像调用本地方法一样的体验。解决了让开发者调用远程接口就跟调用本地方法一样,无需关注与远程交互的细节,更无需关注分布式环境开发。
<!-- openfeign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
package com.wanfeng.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author
* @date 2022/10/17 16:48
* @Description feign接口方法
* name 指定调用rest接口所对应的服务名称
* path 指定调用rest接口所在的StockController指定的@ReQuestNapping
*/
@FeignClient(name = "stock" , path = "/stock")
public interface StockFeignService {
@RequestMapping("/reduct")
String reduct();
}
@EnableFeignClients
在遇到BUG时,比如接口调用失败、参数没有接收到等问题,或者需要查看调用性能时,可以通过Feign日志进行查看
- NONE:性能最佳,用于生产环境,不记录任何日志(默认)
- BASIC:适用于生产环境追踪问题,仅记录请求方法、URL、响应码及执行时间
- HEADERS:记录BASIC级别的基础上记录请求和相应的header
- FULL:比较适用于开发及测试环境定位问题,记录请求和响应的header、body和元数据
package com.wanfeng.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author
* @date 2022/11/18 1:04
* @Description OpenFeign日志配置
*/
@Configuration
public class OpenFeignLogConfig {
@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
logging:
level:
com.wanfeng.feign: debug # 指定包设置日志级别
2022-11-18 01:25:37.284 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] ---> GET http://stock-service/stock/reduct HTTP/1.1
2022-11-18 01:25:37.285 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] ---> END HTTP (0-byte body)
2022-11-18 01:25:37.876 INFO 14836 --- [nio-8030-exec-4] c.netflix.config.ChainedDynamicProperty : Flipping property: stock-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2022-11-18 01:25:37.898 INFO 14836 --- [nio-8030-exec-4] c.netflix.loadbalancer.BaseLoadBalancer : Client: stock-service instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=stock-service,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2022-11-18 01:25:37.904 INFO 14836 --- [nio-8030-exec-4] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2022-11-18 01:25:37.924 INFO 14836 --- [nio-8030-exec-4] c.netflix.config.ChainedDynamicProperty : Flipping property: stock-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2022-11-18 01:25:37.925 INFO 14836 --- [nio-8030-exec-4] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client stock-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=stock-service,current list of Servers=[192.168.31.98:8021],Load balancer stats=Zone stats: {unknown=[Zone:unknown; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:192.168.31.98:8021; Zone:UNKNOWN; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:com.alibaba.cloud.nacos.ribbon.NacosServerList@55905102
2022-11-18 01:25:38.217 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] <--- HTTP/1.1 200 (930ms)
2022-11-18 01:25:38.218 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] connection: keep-alive
2022-11-18 01:25:38.218 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] content-length: 22
2022-11-18 01:25:38.218 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] content-type: text/plain;charset=UTF-8
2022-11-18 01:25:38.218 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] date: Thu, 17 Nov 2022 17:25:38 GMT
2022-11-18 01:25:38.218 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] keep-alive: timeout=60
2022-11-18 01:25:38.218 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct]
2022-11-18 01:25:38.219 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] 扣减库存成功8021
2022-11-18 01:25:38.219 DEBUG 14836 --- [nio-8030-exec-4] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] <--- END HTTP (22-byte body)
2022-11-18 01:25:38.918 INFO 14836 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: stock-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
package com.wanfeng.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author
* @date 2022/11/18 1:04
* @Description OpenFeign日志配置
*/
public class OpenFeignLogConfig {
@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
@FeignClient(name = "stock-service" , path = "/stock" , configuration = OpenFeignLogConfig.class)
2022-11-18 01:31:49.409 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] ---> GET http://stock-service/stock/reduct HTTP/1.1
2022-11-18 01:31:49.409 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] ---> END HTTP (0-byte body)
2022-11-18 01:31:49.413 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] <--- HTTP/1.1 200 (3ms)
2022-11-18 01:31:49.413 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] connection: keep-alive
2022-11-18 01:31:49.413 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] content-length: 22
2022-11-18 01:31:49.414 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] content-type: text/plain;charset=UTF-8
2022-11-18 01:31:49.414 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] date: Thu, 17 Nov 2022 17:31:49 GMT
2022-11-18 01:31:49.414 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] keep-alive: timeout=60
2022-11-18 01:31:49.414 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct]
2022-11-18 01:31:49.414 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] 扣减库存成功8021
2022-11-18 01:31:49.414 DEBUG 10812 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] <--- END HTTP (22-byte body)
# Feign 局部日志配置
feign:
client:
config:
# 需要输入日志的服务名称
product-service:
# 日志级别
loggerLevel: BASIC
2022-11-18 01:54:02.613 DEBUG 12852 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] ---> GET http://stock-service/stock/reduct HTTP/1.1
2022-11-18 01:54:02.617 DEBUG 12852 --- [nio-8030-exec-2] com.wanfeng.feign.StockFeignService : [StockFeignService#reduct] <--- HTTP/1.1 200 (3ms)
- 配置类中使用
@Configuration
为全局,不使用为局部- 局部日志需要在@FeignClient添加configuration配置指定日志配置类
主要作用于低版本的升级后所导致的原生Feign注解无法使用的问题(高版本不可能降低,直接使用Spring MVC注解)
package com.wanfeng.config;
import feign.Contract;
import org.springframework.context.annotation.Bean;
/**
* @author lh
* @date 2022/11/18 2:04
* @Description 契约配置
*/
public class FeignContract {
/**
* 修改Feign契约配置 支持Feign原生注解
* @return
*/
@Bean
public Contract feignContract(){
return new Contract.Default();
}
}
# Feign 配置
feign:
client:
config:
default:
# 设置为默认契约 还原成原生注解
contract: feign.Contract.Default
package com.wanfeng.config;
import feign.RequestInterceptor;
import feign.RequestTemplate;
/**
* @author
* @date 2022/11/18 2:16
* @Description 自定义Feign拦截器
*/
public class CustomFeignInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
// TODO: 2022/11/18
}
}
# Feign 配置
feign:
client:
config:
# 服务名称
default:
# 配置拦截器
requestInterceptors[0]:
# 完整类路径
com.wanfeng.config.CustomFeignInterceptor