Feign是SpringCloud中负责服务调用的组件,跟Ribbon+RestTemplate做一样的事情。Feign底层默认使用Ribbon进行负载均衡。与Ribbon+RestTemplate调用服务接口不一样的是,使用Feign客户端,只需要在接口加上注解,对应的要调用的服务方法,则可调用服务,底层调用过程不需要开发者考虑,在controller层像调用自己本地service一样实现业务调用。OpenFeign对原本的Feign做了封装和加强。
OepnFeign的使用
-
pom引入依赖
<!--openFeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
server: port: 80 #eureka client eureka: client: #是否将自己注册进eurekaServer,默认true register-with-eureka: false service-url: #服务注册地址 defaultZone: http://localhost:7001/eureka
-
SpringBoot启动类添加注解@EnableFeignClients
@SpringBootApplication @EnableFeignClients public class OrderFeignApplication80 { public static void main(String[] args) { SpringApplication.run(OrderFeignApplication80.class,args); } }
-
OpenFeign使用(已有对应的服务提供者)
step1.创建service接口
// 1.添加spirng注解 注入到spring容器 @Service // 2.添加oepnFeign注解,传入微服务名称 @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentService { //3. 构建方法,对应的服务提供者 @GetMapping(value = "/payment/get/{id}") BaseResult findById(@PathVariable(value = "id")String id); }
step2.编写服务客户端的controller
@RestController @RequestMapping(value = "/order") public class OrderFeignController { //1.注入,这里就跟正常项目一样 @Resource private PaymentService paymentService; //2.controller里的方法 @GetMapping(value = "/get/{id}") public BaseResult findById(@PathVariable(value = "id")String id){ return paymentService.findById(id); } }
step3.调用测试。
OpenFeign超时控制
默认Feign客户端只等待1秒,如果服务端响应时间超过1秒,Feign客户端会直接返回保存,为了避免这种情况,有时候我们需要设置Feign客户端的超时控制。
Feign底层用Ribbon实现,设置Feign超时时间也就是设置Ribbon超时时间:
扫描二维码关注公众号,回复:
12753758 查看本文章
#设置feign客户端超时时间,OpenFeign默认支持Ribbon,也就是设置Ribbon的超时时间
ribbon:
#建立连接所用的时间,5s
ReadTimeout: 5000
#连接建立后从服务器读取资源所用时间(连接建立等服务器返回的时间)5s
ConnectTimeout: 5000
OpenFeign日志打印功能
Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节。对Feign接口的调用情况进行监控和输出。
Feign的日志级别:
- NONE:默认的,不显示日志
- BASIC:仅记录请求方法、URL、响应状态码及执行时间
- HEADERS:除BASIC中定义的信息外,还有请求和响应头信息
- FULL:除了HEADERS中定义的信息外,还有请求和响应的正文及元数据
设置feign日志监听接口调用
-
向spring容器中注入日志配置类
@Configuration public class FeignConfig { @Bean Logger.Level feignLogggerLevel(){ return Logger.Level.FULL; } }
-
添加日志监听配置
logging: level: #feign日志以什么级别监听接口 com.zqq.springcloud.service.PaymentService: debug
-
调用测试
2021-03-13 00:32:23.987 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] <--- HTTP/1.1 200 (3688ms) 2021-03-13 00:32:23.988 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] connection: keep-alive 2021-03-13 00:32:23.989 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] content-type: application/json 2021-03-13 00:32:23.989 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] date: Fri, 12 Mar 2021 16:32:23 GMT 2021-03-13 00:32:23.989 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] keep-alive: timeout=60 2021-03-13 00:32:23.990 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] transfer-encoding: chunked 2021-03-13 00:32:23.990 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] 2021-03-13 00:32:23.997 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] {"code":200,"msg":"成功","data":{"id":4,"amount":22.10,"comment":"备注2"}} 2021-03-13 00:32:23.997 DEBUG 1992 --- [p-nio-80-exec-1] c.z.springcloud.service.PaymentService : [PaymentService#findById] <--- END HTTP (78-byte body)