文章目录
1 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2 编写要调用的service
比如order模块要调用nacos中名称为stock的服务,需要
注意name需要为nacos中注册的service名,path为对应前缀
@FeignClient(name = "stock",path = "/stock")
public interface StockFeignService {
@RequestMapping("/reduct")
public String reduct();
}
对应的stock中的controller
3 调用方在启动类上加注解
@EnableFeignClients
4 直接调用即可
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private StockFeignService stockFeignService;
@RequestMapping("/add")
public String add(){
stockFeignService.reduct();
return "add";
}
5 日志配置
5.1 方式一
5.1.1 编写日志配置类
// 此处配置@Configuration注解就会全局生效,如果想指定对应微服务生效,就不 能配置
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
//NONE【性能最佳,适用于生产】:不记录任何日志(默认值)。
//BASIC【适用于生产环境追踪问题】:仅记录请求方法、URL、响应状态代码以及 执行时间
//HEADERS:记录BASIC级别的基础上,记录请求和响应的header。
//FULL【比较适用于开发及测试环境定位问题】:记录请求和响应的header、body 和元数据
}
}
· NONE【性能最佳,适用于生产】:不记录任何日志(默认值)。
· BASIC【适用于生产环境追踪问题】:仅记录请求方法、URL、响应状态代码以及 执行时间
· HEADERS:记录BASIC级别的基础上,记录请求和响应的header。
· FULL【比较适用于开发及测试环境定位问题】:记录请求和响应的header、body 和元数据
5.1.2 在yml配置文件中执行 Client 的日志级别才能正常输出日志,格式是"logging.level.feign接口包路径 =debug"
logging:
level:
com.sshc.springDemo.order.myfeign: debug
# 如果直接loggin.level=debug,那么所有的都会显示debug
5.1.3 在指定的service上,使用配置类
@FeignClient(name = "stock",path = "/stock",configuration = FeignConfig.class)
public interface StockFeignService {
@RequestMapping("/reduct")
public String reduct();
}
5.2 方式二 在yml配置文件中配置
feign:
client:
config:
stock: #对应服务名
loggerLevel: FULL
6 feign拦截器
通常有的接口需要加token,加特殊请求头
需要加拦截器
首先需要编写一个拦截器
package com.sshc.springDemo.order.config;
import feign.RequestInterceptor;
import feign.RequestTemplate;
public class FeignAuthRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
//TODO 业务逻辑
requestTemplate.header("token","mytoken");
}
}
6.1 方式一 使用配置类
6.1.1 配置类如下
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;
//NONE【性能最佳,适用于生产】:不记录任何日志(默认值)。
//BASIC【适用于生产环境追踪问题】:仅记录请求方法、URL、响应状态代码以及 执行时间
//HEADERS:记录BASIC级别的基础上,记录请求和响应的header。
//FULL【比较适用于开发及测试环境定位问题】:记录请求和响应的header、body 和元数据
}
//配置拦截器
@Bean
public FeignAuthRequestInterceptor interceptor1(){
return new FeignAuthRequestInterceptor();
}
}
6.1.2在对应feignClient中加入配置
@FeignClient(name = "stock",path = "/stock",configuration = FeignConfig.class)
public interface StockFeignService {
@RequestMapping("/reduct")
public String reduct();
}
效果如图
6.2 方式二 在yml中配置
feign:
client:
config:
stock:
loggerLevel: FULL
# requestInterceptors: ##可以这种方式配置
# - com.sshc.springDemo.order.config.FeignAuthRequestInterceptor
requestInterceptors[0]:
com.sshc.springDemo.order.config.FeignAuthRequestInterceptor
7 超时时间配置
通过 Options 可以配置连接超时时间和读取超时时间,Options 的第一个参数是连接的超时 时间(ms),默认值是 2s;第二个是请求处理的超时时间(ms),默认值是 5s
7.1 方式一 配置类
// 此处配置@Configuration注解就会全局生效,如果想指定对应微服务生效,就不 能配置
public class FeignConfig {
/**
* 配置超时时间
* @return
*/
@Bean
public Request.Options options(){
return new Request.Options(500,700);
}
}
之后再使用就好了
@FeignClient(name = "stock",path = "/stock",configuration = FeignConfig.class)
7.2 方式二 yml配置
feign:
client:
config:
stock: #服务名
loggerLevel: FULL
# requestInterceptors: ##可以这种方式配置
# - com.sshc.springDemo.order.config.FeignAuthRequestInterceptor
requestInterceptors[0]:
com.sshc.springDemo.order.config.FeignAuthRequestInterceptor
connectTimeout: 5000 #连接超时时间,默认2s
readTimeout: 10000 #请求处理超时时间,默认5s
补充说明: Feign的底层用的是Ribbon,但超时时间以Feign配置为准
8 如果同时配置了配置类,和bootstrap.yml方式,那么yml方式优先。
9 客户端组件配置
Feign 中默认使用 JDK 原生的 URLConnection 发送 HTTP 请求,我们可以集成别的组件来 替换掉 URLConnection,比如 Apache HttpClient,OkHttp。
Feign发起调用真正执行逻辑:feign.Client#execute (扩展点)
9.1 配置Apache HttpClient
9.1.1添加依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>10.1.0</version>
</dependency>
配置文件可写
feign:
httpclient: #可以忽略,默认开启
enabled: true
9.2 配置OkHttp
9.2.1 添加依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
9.2.2 修改yml文件配置
feign:
httpclient: #可以忽略,默认开启
enabled: false
okhttp:
enabled: true
9.3 GZIP压缩配置
开启压缩可以有效节约网络资源,提升接口性能,我们可以配置 GZIP 来压缩数据:
feign:
#配置GZIP压缩数据
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true
注意:只有当 Feign 的 Http Client 不是 okhttp3 的时候,压缩才会生效,配置源码在 FeignAcceptGzipEncodingAutoConfiguration