引入pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
OpenFeign接口
- 先使用@EnableFeignClients启用
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignMain80.class,args);
}
}
- Controller
@RestController
@Slf4j
public class OrderFeignController {
@Autowired
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout() {
//openfeign-ribbon 客户端默认等待1S
return paymentFeignService.paymentFeignTimeout();
}
}
- Service(@FeignClient以微服务别名形式调用此服务下的接口)
@Component
@FeignClient("CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout();
}
OpenFeign超时设置
以yml形式设置超时(openfeign-ribbon 客户端默认等待1S,超时会报相关错误 java.net.SocketTimeoutException: Read timed out)
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的实际
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
OpenFeign日志打印
OpenFeign日志级别有:
- 配置日志级别
@Configuration
public class FeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
- 声明输出日志的接口(yml形式)
logging:
level:
com.agile.springcloud.service.PaymentFeignService: debug