依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign<artifactId>
<dependency>
OpenFeign 使用步骤
微服务调用接口+注解(@FeignClient)
1:主启动类添加@enableFeignClients注解
2.业务逻辑接口+@FeignClient(value = “服务名称”)配置调用服务
举例:
@FeignClient(value = "nacos-payment-provider" fallback = PaymentFallbackService.class)
public interface PaymentService
{
@GetMapping(value = "/paymentSQL/{id}")
public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
}
public class PaymentFallbackService implements PaymentService
{
@Override
public CommonResult<Payment> paymentSql(Long id)
{
return new CommonResult<>("aaa")
}
}
OpenFeign超时控制
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的境况下,两端连接所用的时间
ReadTimeout:5000
#知道是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout:5000
OpenFeign日志增强
Feign提供了日志打印功能,通过配置日志级别可以对Feign接口的调用情况进行监控和输出
日志级别:
NONE:默认的,不显示任何日志
BASIC:仅记录请求方法、URL、响应状态码及执行时间;
HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息;
FULL:除了HEADERS中定义的信息之外,还有请求头和响应正文及元数据;
配置Bean
@Configuration
public class FeignConfig{
@Bean
Logger.Level feignLoggerLevel()
{
return Logger.level.FULL;
}
}
修改YML
logging:
level
#feign日志以什么级别监控哪个接口
com.aaaa.springcloud.service.PaymentFeignService:debug