springcloud - openFeign的简单配置和使用

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

posted on   你就学个JVAV?  阅读(121)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示