SpringCloud(六):OpenFeign服务接口调用

概述

在这里插入图片描述

在这里插入图片描述

OpenFeign使用步骤

  1. 接口+注解:微服务调用接口+@FeignClient

  2. 新建cloud-consumer-feign-order80

  3. POM

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

    server:
      port: 80
    eureka:
      client:
        register-with-eureka: false
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
  5. 主启动:@EnableFeignClients

    package com.atguigu.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    @SpringBootApplication
    @EnableFeignClients
    public class OrderFeignMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderFeignMain80.class,args);
        }
    }
    
  6. 业务类

    • 业务逻辑接口+@FeignClient配置调用provider服务

    • 新建PaymentFeignService接口并新增注解@FeignClient

      @Component
      @FeignClient(value = "CLOUD-PAYMENT-SERVICE")
      public interface PaymentFeignService {
      
          @GetMapping(value = "/payment/get/{id}")
          public CommonResult getPaymentById(@PathVariable("id") Long id);
      }
      
    • 控制层Controller

      @RestController
      public class OrderFeignController {
      
          @Resource
          private PaymentFeignService paymentFeignService;
      
          @GetMapping(value = "/consumer/payment/get/{id}")
          public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
             return paymentFeignService.getPaymentById(id);
          }
      }
      

OpenFeign超时控制

  • OpenFeign默认等待一秒钟,超过后报错

  • YML文件里需要开启OpenFeign客户端超时控制

    ribbon:
      ReadTimeout:  5000
      ConnectTimeout: 5000
    

OpenFeign日志打印功能

  1. 基本概念

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IlfaMz8c-1655077941249)(F:\1111yjs\笔记\image-20220613014637151.png)]

  2. 日志级别

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z9yyWkgb-1655077941250)(F:\1111yjs\笔记\SpringCloud.assets\image-20220613014739593.png)]

  3. 配置日志bean

    package com.atguigu.springcloud.config;
    
    import feign.Logger;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class FeignConfig {
    
        @Bean
        Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    
  4. YML文件里需要开启日志的Feign客户端

    logging:
      level:
        com.atguigu.springcloud.service.PaymentFeignService: debug
    
  5. 后台日志查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

平什么阿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值