微服务SpringCloud开弓之OpenFeign服务接口调用「七」

1、概述

在这里插入图片描述
在这里插入图片描述

Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需 创建一个接口并在接口上添加注解即可

GitHub
在这里插入图片描述

Feign和OpenFeign两者区别
在这里插入图片描述

2、OpenFeign使用步骤

  • 接口+注解

    微服务调用接口+@FeignClient

  • 新建cloud-consumer-feign-order80

    Feign在消费端使用
    在这里插入图片描述

  • POM

       <dependencies>
            <!--openfeign-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <!--eureka client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
            <dependency>
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--监控-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!--热部署-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
  • YML

    server:
      port: 80
    eureka:
      client:
        register-with-eureka: false
    #      检索服务选项,当设置为True(默认值)时,会进行服务检索,注册中心不负责检索服务。
    #    fetch-registry: true
    
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
  • 主启动

    @SpringBootApplication
    //@EnableEurekaClient
    @EnableFeignClients
    public class OrderFeignMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderFeignMain80.class, args);
        }
    }
    
  • 业务类

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

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

    @Component
    @FeignClient(value = "CLOUD-PROVIDER-SERVICE")
    public interface PaymentFeignService {
    
        @PostMapping(value = "/payment/create")
        CommonResult<Payment> create(@RequestBody Payment payment);
    
        @GetMapping(value = "/payment/get/{id}")
        CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
    }
    
    

    2、控制层Controller

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

在这里插入图片描述

Feign自带负载均衡配置项

  • 小结
    在这里插入图片描述

3、OpenFeign超时控制

  • 超时设置,故意设置超时演示出错情况

    1、服务提供方8001故意写暂停程序

    ​ PaymentController

          @GetMapping(value = "/payment/feign/timeout")
        public  String   paymentFeignTimeout(){
    
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return  serverPort;
        }
    

    2、服务消费方80添加超时方法

    ​ PaymentFeignService

     @GetMapping(value = "/payment/feign/timeout")
        String paymentFeignTimeout();
    

    3、服务消费方80添加超时方法

    ​ OrderFeignController

     @GetMapping(value = "/consumer/payment/feign/timeout")
        public  String   paymentFeignTimeout(){
            return  paymentFeignService.paymentFeignTimeout();
        }
    

    4、报错

    http://localhost/consumer/payment/feign/timeout 延迟访问

    http://localhost:8001/payment/feign/timeout报错

在这里插入图片描述

  • OpenFeign默认等待1秒钟,超过后报错
    在这里插入图片描述

OpenFeign默认支持Ribbon

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

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

4、OpenFeign日志打印功能

  • 日志级别

在这里插入图片描述

  • 配置日志been

    @Configuration
    public class FeignConfig {
    
        /**
         * feignClient配置日志级别
         *
         * @return
         */
        @Bean
        public Logger.Level feignLoggerLevel() {
            // 请求和响应的头信息,请求和响应的正文及元数据
            return Logger.Level.FULL;
        }
    }
     
    
  • YML文件里需要开启日志的Feign客户端

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.atguigu.springcloud.service.PaymentFeignService: debug
  • 后台日志查看
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值