九、OpenFeign 服务接口调用

1、概述

Feign是一个声明式的WebService客户端。使用Feign能让编写Web Service客户端更加简单

它的使用方法是定义一个服务接口,然后在上面添加注解。Feign也支持可拔插式的编码器和解码器。SpringCloud对Feign进行了封装,使其支持Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡


Feign能干什么?(为什么要使用Feign)

Feign旨在使编写Java Http客户端变得更容易。

在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用,这样代码往往很冗余。所以Feign在此基础上做了进一步的封装,由它来帮助我们实现依赖服务接口的定义。在Feign的实现下,我们只需要创建一个接口并使用注解的方式(@Feign)来配置它,即可完成对服务提供方的

接口绑定。简化了使用 ribbon + restTemplate,自动封装调用客户端的开发量


Feign集成了Ribbon


2、openFeign的使用步骤

1)新建module cloud-consumer-feign-order80

clipboard


2) 添加依赖

<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-client</artifactId>
    </dependency>
    <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
    <dependency>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <!--web-->
    <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>


3)编写application.xml

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka


4)主启动类

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


5)编写业务类

@RestController
@Slf4j
public class OrderFeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/feign/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        return paymentFeignService.getPaymentById(1L);
    }
}

================================================================================================================
@Component
// 这个注解是及其重要的,表明了远程调用的服务
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

    //通过这个注解找远程服务 controller下带有该注解的接口
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);
}


6)测试

clipboard


3、OpenFeign的超时控制

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

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


4、OpenFeign的日志打印功能

1)是什么

clipboard


2)日志级别

clipboard


3)如何实现日志打印功能

① 添加日志bean配置类

@Configuration
public class FeignConfig
{
    @Bean
    Logger.Level feignLoggerLevel()
    {
        return Logger.Level.FULL;
    }
}


②、在yml中添加如下配置

已debug模式打印PaymentFeignService接口Full 级别的日志

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.atguigu.springcloud.service.PaymentFeignService: debug


③、效果

clipboard

原文地址:https://www.cnblogs.com/houchen/p/14833284.html