openFeign
一.概述
2.OpenFeign是什么?
Feign是一个声明式的web服务客户端,让编写web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可
3.Feign和OpenFeign两者区别
二.openfeign的使用(feign就是指的openfeign)
1.基础使用
(1).建module
略
(2).改pom
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
(3).写yml
feign配置的很少,基本可以说没有
#开启feign自带的日志
logging:
level:
#指定feign调用哪个接口,开启debug全日制功能
com.atguigu.springcloud.service.PaymentFeignService: debug
(4).主启动
@EnableFeignClients //启动feign客户端
@SpringBootApplication
@EnableFeignClients //启动feign客户端
public class PaymentHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain80.class,args);
}
}
(5).业务类
- 编写Feign的接口
@Component
//标注本接口为openfeign的接口,接口的实现类为其他微服务暴露的controller层地址.
//value = "CLOUD-PAYMENT-SERVICE"这个值,表名的是调用哪个微服务暴露的接口
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
/**
* 调用CLOUD-PAYMENT-SERVICE服务中的哪个api接口
* @param id
* @return
*/
@GetMapping("/payment/{id}") //url路劲
CommonResult findPaymentById(@PathVariable("id") Long id);
}
- 调用接口
@RestController
@Slf4j
public class OrderFeignController {
@Autowired //本接口的实现类是Feign帮我们使用代理对象事项的.
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/payment/{id}")
public CommonResult findPaymentById(@PathVariable("id") Long id){
return paymentFeignService.findPaymentById(id);
}
}
2.超时控制
1.feign默认超时
- 默认超时时间为1秒
- 超时页面,错误
2.配置feign的超时时间
由于我们可能根据业务的需求,需要修改超时时间,所以我们在yml中配置;并且由于feign默认集成了Ribbon做为负载均衡的调用,所以我们使用Ribbon来控制超时时间.
yml配置
#feign默认集成了Ribbon,所以我们就可通过Ribbon来控制超时时间
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
3.OpenFeign日志打印功能
feign自带了的日志框架,我们只需要在配置中启动日志功能即可.
#开启feign自带的日志
logging:
level:
#指定feign调用哪个接口,开启debug调试功能,这里可以指定多个接口
com.atguigu.springcloud.service.PaymentFeignService: debug
com.atguigu.springcloud.service.OrderFeignService: info