Feign
简介
(Feign = RestTemplate+Ribbon+Hystrix )
Feign是Netflix开发的?个轻量级RESTful的HTTP服务客户端(?它来发起请求,远程调?的)
类似dubbo,服务消费者拿到提供者的接口,像调用本地方法一样调用接口中的方法,实际发出的是远程的请求。
本质:封装了http调用流程,更符合面向接口化的编程习惯
@FeignClient(name = "lagou-service-resume")
public interface ResumeServiceApi {
@RequestMapping(value = "/resume/state/{userId}",method = RequestMethod.GET)
Integer check(@PathVariable(value = "userId") Long userId);
}
复杂均衡
Feign集成了Ribbon依赖和自动配置,可以通过ribbon配置修改负载均衡策略。
熔断器
熔断器默认是关闭,需要在配置文件中开启
feign:
hystrix:
enabled: true
注意点:
- 开启熔断器后,feign中所有方法都会被加入容错机制,一旦出现错误,就会执行回退逻辑
- feign、hystrix都有超时时间设置,同时设置时以两者之间的值为准。
Fegin集成hystrix
1.编写Fallbak类,实现FeignClient接口
@Component
public class ResumeServiceFallBack implements ResumeServiceApi {
@Override
public Integer check(Long userId) {
return -6;
}
}
2.在接口中引入fallback类
@FeignClient(name = "lagou-service-resume" ,fallback = ResumeServiceFallBack.class)
@RequestMapping("/resume")
@RequestMapping("/resume")
public interface ResumeServiceApi {
@RequestMapping(value = "/resume/state/{userId}",method = RequestMethod.GET)
Integer check(@PathVariable(value = "userId") Long userId);
}
请求和响应压缩
Feign ?持对请求和响应进?GZIP压缩,以减少通信过程中的性能损耗。
feign:
compression:
request:
enabled: true
mime-types: text/html,application/xml,application/json
min-request-size: 2048
response:
enabled: true
日志配置
Feign是http请求客户端,在请求和响应的时候,可以打印出比较详细的日志。(请求头、请求体等)
默认日志是没有开启的。
1.开启Feign?志功能及级别
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLevel() {
return Logger.Level.FULL;
}
}
2.配置系统log?志级别为debug
logging:
level:
com.lagou.edu.api.ResumeServiceApi:
debug
源码分析
采用jdk动态代理,对方法增强。底层请求发起是HttpURLConnection
//todo
|