【Spring cloud】OpenFeign详解(超详细)

本文涉及的产品
应用型负载均衡 ALB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
传统型负载均衡 CLB,每月750个小时 15LCU
简介: 【Spring cloud】OpenFeign详解(超详细)

一、概述



「1. OpenFeign是什么?」


下面是关于OpenFeign的一段官方介绍:


Feign is a declarative web service client. It makes writing web service clients e
asier. To use Feign create an interface and annotate it. It has pluggable annotat
ion support including Feign annotations and JAX-RS annotations. Feign also suppor
ts pluggable encoders and decoders. Spring Cloud adds support for Spring MVC anno
tations and for using the same HttpMessageConverters used by default in Spring We
b. Spring Cloud integrates Eureka, Spring Cloud CircuitBreaker, as well as Spring
Cloud LoadBalancer to provide a load-balanced http client when using Feign.


这段话说的是:OpenFeign是一个显示声明式的WebService客户端。使用OpenFeign能让编写Web Service客户端更加简单。使用时只需定义服务接口,然后在上面添加注解。

OpenFeign也支持可拔插式的编码和解码器。spring cloud对feign进行了封装,使其支持MVC注解和HttpMessageConverts。和eureka(服务注册中心)和ribbon组合可以实现负载均衡。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求,非常的方便。「2. OpenFeign能干啥?」


  • OpenFeign的设计宗旨式简化Java Http客户端的开发。Feign在restTemplate的基础上做了进一步的封装,由其来帮助我们定义和实现依赖服务接口的定义。在OpenFeign的协助下,我们只需创建一个接口并使用注解的方式进行配置(类似于Dao接口上面的Mapper注解)即可完成对服务提供方的接口绑定,大大简化了Spring cloud Ribbon的开发,自动封装服务调用客户端的开发量。
  • OpenFeign集成了Ribbon,利用ribbon维护了服务列表,并且通过ribbon实现了客户端的负载均衡。与ribbon不同的是,通过OpenFeign只需要定义服务绑定接口且以申明式的方法,优雅而简单的实现了服务调用。


二、OpenFeign入门使用



1. pom引入


<!--Open feign-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
<!--服务发现客户端-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <!--web服务-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>


2. yml配置


server:
  port: 81 #端口
spring:
  application:
    name: cloud-consumer #服务别名
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/ #服务中心地址
  instance:
    prefer-ip-address: true #显示ip地址


3. 主类编写


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableEurekaClient //启用服务注册客户端
@SpringBootApplication //springboot启动注解
@EnableFeignClients //启用OpenFeign
public class OpenFeignMain {
    public static void main(String[] args) {
        SpringApplication.run(OpenFeignMain.class,args);
    }
}


4. 编写接口


640.png


import com.yuyue.springcloud.common.dto.ResultDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
@Component //注册为spring组件,交予IOC容器管理
@FeignClient(value = "cloud-payment") //添加FeignClient注解,绑定服务提供者。
public interface TestService {
    @GetMapping("/payment/list")
    ResultDto list();
}

接口加注解即可绑定服务提供者。


5. controller层


import com.yuyue.online.springcloud.consumer81.service.TestService;
import com.yuyue.springcloud.common.dto.ResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/consumer")
public class TestController {
    @Autowired
    private TestService testService;
    @GetMapping("/list")
    public ResultDto list(){
        return testService.list();
    }
}


和普通的controller层没有区别,和mapper访问数据库很类似。


6. 测试


640.gif


测试结果为:轮询访问8080和8081两个服务提供者,实现了调用和负载均衡。


三、OpenFeign进阶



1. 超时控制


OpenFeign默认超时时间为1s,超过1s就会返回错误页面。如果我们的接口处理业务确实超过1s,就需要对接口进行超时配置,如下:


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


2. 日志增强


「1. yml配置」

logging:
  level:
    com.yuyue.online.springcloud.consumer81.service.TestService: debug #指定openfeign日志以什么级别监控哪个接口(可多个)


「2. 配置日志bean」

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


import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenFeignConfig {
    @Bean
    Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }
}

640.png


相关实践学习
小试牛刀,一键部署电商商城
SAE 仅需一键,极速部署一个微服务电商商城,体验 Serverless 带给您的全托管体验,一起来部署吧!
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
打赏
0
0
0
0
15
分享
相关文章
Spring Cloud OpenFeign详解与实践
总结起来说,Spring Cloud OpenFeign提供了一种简单易懂且高效的方式去实现微服务之间通信.它隐藏了许多复杂性,并且允许开发者以声明式方式编写HTTP客户端代码.如果你正在开发基于Spring Cloud 的微服务架构系统,Spring Cloud Open Feign是一个非常好用且强大工具.
215 33
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
15543 91
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
102 6
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
139 5
|
7月前
|
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
130 5
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
1305 15
|
12月前
|
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
246 3
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等