SpringCloud——初级(三)OpenFeign服务接口调用

OpenFeign服务接口调用

一、概述

1、OpenFeign是什么

官网
Feign是一个声明式WebSerrvice客户端。使用Feign能让编写Web Serrvice客户端更加简单。
它的使用方法是定义一个服务接口然后再上面添加注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
总结:Feign是一个声明式的Web服务客户端,让编写Web服务客户端变得非常容易,只需创建接口并在接口上添加注解即可
GitHub官网地址

2、OpenFeign能干什么

Feign旨在使编写Java Http客户端变得更加容易
在这里插入图片描述

3、Feign和OpenFeign两者的区别

Feign OpenFeign
Feign是Spring Cloud 组件中的一个轻量级RESTful的HTTP服务客户端Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务 OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC 的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

二、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:7002/eureka/

5、主启动类

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、业务类

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

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

package com.atguigu.springcloud.service;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import feign.Param;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

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

3)控制层Controller

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

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

7、测试

http://localhost/consumer/payment/get/35

8、小总结

在这里插入图片描述

三、OpenFeign超时控制

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

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

    @GetMapping("/payment/feign/timeout")
    public String paymentFeignTimeout(){
        //暂停几秒钟线程
        try{
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return serverPort;
    }

2)服务消费方80添加超时方法(接口)

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

3)服务消费方80添加超时方法(Controller)

    @GetMapping("/consumer/payment/feign/timeout")
    public String paymentFeignTimeout(){
       //openfeign-ribbon;客户端一般默认等待一秒钟
        return paymentFeignService.paymentFeignTimeout();
    }

4)测试

http://localhost/consumer/payment/feign/timeout
错误页面:
在这里插入图片描述

2、OpenFeign默认等待1秒,超过后报错

3、是什么——默认支持Ribbon

默认Feign客户端只等待1秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了,直接返回报错。为了避免这种情况,有时候我们需要设置Feign客户端的超时控制。

4、YML文件里开启OpenFeign客户端超时控制

#设置feign客户端超时时间(OpenFeign默认支持Ribbon)
ribbon:
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ReadTimeout: 5000  #5000表示5秒钟
  #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ConnectTimeout: 5000

四、OpenFeign日志打印功能

1、是什么

Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节。
说白了就是对Feign接口的调用情况进行监控和输出

2、日志级别

1、NONE:默认的,不显示任何日志
2、BASIC:仅记录请求方法、URL、响应状态码及执行时间
3、HEADERS:除了BASIC中定义的信息之外,还有请求的响应的头信息
4、FULL:出了HEADERS中定义的信息之外,还有请求和响应的正文以及元数据

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:
    #feign日志以什么级别监控哪个接口
    com.atguigu.springcloud.service.PaymentFeignService: debug

5、后台日志查看

测试:http://localhost/consumer/payment/get/35
结果:

发布了19 篇原创文章 · 获赞 4 · 访问量 7176

猜你喜欢

转载自blog.csdn.net/qq_41307492/article/details/105308442