springcloud - openFeign的简单配置和使用

发布时间 2023-07-06 22:33:36作者: 你就学个JVAV?

openFeign 第一步:导入依赖

         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-openfeign</artifactId>
         </dependency>

第一步:进行配置

 server:
   port: 80
 ​
 eureka:
   client:
     register-with-eureka: false
     service-url:
       defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

第三步:开启注解

 @SpringBootApplication
 @EnableFeignClients
 public class OrderFeignMain80 {
     public static void main(String[] args) {
         SpringApplication.run(OrderFeignMain80.class,args);
     }
 }

第四步:对接口进行注解

 @Component
 @FeignClient(value = "CLOUD-PAYMENT-SERVICE")
 public interface PaymentService {
 ​
     @GetMapping(value = "payment/get/{id}")
     CommonResult<Payment> getPaymentById(@PathVariable(value = "id") Long id);
 ​
     @GetMapping(value = "payment/timeout")
     String getPaymentTimeout();
 }

第五步:调用接口

 @RestController
 public class PaymentController {
     @Resource
     private PaymentService paymentService;
 ​
     @GetMapping(value = "consumer/payment/get/{id}")
     public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
         return paymentService.getPaymentById(id);
     }
 ​
     @GetMapping(value = "consumer/payment/timeout")
     public String getPaymentTimeout(){
         return paymentService.getPaymentTimeout();
     }
 }

feign日志增强

   #日志增强
 logging:
   level:
     com.atguigu.springcloud.service.PaymentService: debug

日志增强配置类

 @Configuration
 public class FeignConfig {
     @Bean
     Logger.Level feignLevelInfo(){
         return Logger.Level.FULL; //full表示一个详细日志
     }
 }

feign 超时等待配置 设置指定的时间,等待返回结果,超时报错,默认是1s

 ribbon:
   # 值得是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
   ReadTimeout: 5000
   # 指的是建立连接后从服务端读取到可用资源所用的时间
   ConnectTimeout: 5000