服务调用 OpenFeign
1.pom引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
2.主启动类添加 @EnableFeignClients 注解
@SpringBootApplication @EnableFeignClients public class OrderFeignMain80 { public static void main(String[] args) { SpringApplication.run(OrderFeignMain80.class, args); } }
3.服务层创建 FeignService 接口并使用 @FeignClient 注解并指定调用的服务名
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentFeignService { @PostMapping("/payment/insert") CommonResult insertPayment(@RequestBody Payment payment); @GetMapping("/payment/getPayment/{id}") CommonResult getPaymentById(@PathVariable("id") Long id); @GetMapping("/payment/feign/timeout") String paymentTimeout(); @GetMapping("/payment/feign/exception") String paymentException(); }
注意:这里的接口就和服务提供方的接口一致
4.Feign服务调用超时控制
ribbon: ReadTimeout: 5000 ConnectTimeout: 5000
Feign底层是包含Ribbon的,所以超时控制交给Ribbon来做
注意:首字母是大写 ConnectTimeout ReadTimeout
5.Feign开启日志增强
@Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
作者:[一柒微笑]