当前位置:网站首页>OpenFeign服务接口调用
OpenFeign服务接口调用
2022-06-27 12:25:00 【RB_VER】
概述
Feign是一个声明式WebService客户端。使用Feign能让编写WebService客户端更加简单。
它的使用方法是定义一个服务接口然后在上面添加注解。Feign也支持可插拔式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
一句话总结:Feign是一个声明式的Web服务客户端,让编写web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可。
Feign旨在使编写Java Http客户端变得更容易。
使用Ribbon+RestTemplate时,利用RestTemplate对Http请求的封装处理,形成了一套模板化的调用方法,但是实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由它来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它,即可完成对服务提供方的接口绑定,简化了使用spring cloud ribbon时,自动封装服务调用客户端的开发量。
Feign集成了Ribbon,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而简单的实现了服务调用。
Feign已经被OpenFeign取代。
| Feign | OpenFeign |
|---|---|
| Feign是spring cloud组件中的一个轻量级RESTful的HTTP服务客户端Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。 | OpenFeign是spring cloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。 |
使用步骤
新建cloud-consumer-feign-order80模块。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2022</artifactId>
<groupId>com.qrxqrx.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-feign-order80</artifactId>
<dependencies>
<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>
<dependency>
<groupId>com.qrxqrx.springcloud</groupId>
<artifactId>cloud-api-commons</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
OrderFeignMain80
package com.qrxqrx.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);
}
}
PaymentFeignService
package com.qrxqrx.springcloud.service;
import com.qrxqrx.springcloud.entities.CommonResult;
import com.qrxqrx.springcloud.entities.Payment;
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(value = "/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
OrderFeignController
package com.qrxqrx.springcloud.controller;
import com.qrxqrx.springcloud.entities.CommonResult;
import com.qrxqrx.springcloud.entities.Payment;
import com.qrxqrx.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
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(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymenyById(@PathVariable("id") Long id) {
return paymentFeignService.getPaymentById(id);
}
}
OpenFeign超时控制
默认feign客户端只等待1秒,但是服务端处理需要超过1秒,导致feign客户端不想等待了,直接返回报错。为了避免这样的情况,有时候需要设置feign客户端的超时控制。
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
# 设置feign客户端超时时间
ribbon:
# 指的是建立连接后从服务器读取到可用资源所用的时间
ReadTimeout: 5000
# 指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ConnectTimeout: 5000
OpenFeign日志打印功能
Feign提供了日志打印功能,可以通过配置来调整日志级别,从而了解Feign中Http请求的细节。对Feign接口调用情况进行监控和输出。
日志级别:
- NONE:默认的,不显示任何日志。
- BASIC:仅记录请求方法、URL、响应状态码及执行时间。
- HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息。
- FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据。
添加配置类:
FeignConfig
package com.qrxqrx.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;
}
}
修改配置:
application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
# 设置feign客户端超时时间
ribbon:
# 指的是建立连接后从服务器读取到可用资源所用的时间
ReadTimeout: 5000
# 指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ConnectTimeout: 5000
logging:
level:
# feign日志以什么级别监控哪个接口
com.qrxqrx.springcloud.service.PaymentFeignService: debug
边栏推荐
- Master formula
- 本地可视化工具连接阿里云centOS服务器的redis
- 微服务之配置管理中心
- Snipaste, the world's strongest screenshot software
- Interview shock 60: what will cause MySQL index invalidation?
- It is so simple to remove the payment restrictions on VIP, YuQue and Zhihu in Baidu Library
- xxl-job学习梳理
- How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line
- 解压 .log.gz 文件
- Object serialization
猜你喜欢

让学指针变得更简单(一)

ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络

【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(三)

gcc编译动态库和静态库

MySQL learning 1: installing MySQL

Comment modifier Node Fichiers dans les modules

uniapp下拉弹层选择框效果demo(整理)

What is the next step in the recommendation system? Alispacetime aggregates GNN, and the effect is to sling lightgcn!

解开C语言的秘密《关键字》(第六期)

Secyun won the "2022 AI analysis · it operation and maintenance vendor panorama report" as the representative vendor of intelligent operation and maintenance aiops Market
随机推荐
本地可视化工具连接阿里云centOS服务器的redis
Dynamic programming [III] (interval DP) stone merging
PyCharm汉化
AI for Science:科研范式、开源平台和产业形态
Topic38——56. Consolidation interval
Thymeleaf的配置
一个有趣的网络掩码的实验
Three traversal methods of binary tree
Self taught ADT and OOP
Building crud applications in golang
大小端字节序
对象序列化
号称史上最难618,淘宝数据盘点你做对了吗?
MySQL高阶语句(一)
First encounter with dynamic programming
Unzip log. GZ file
Interview shock 60: what will cause MySQL index invalidation?
Airbnb复盘微服务
解除百度文库VIP、语雀、知乎付费限制,原来这么简单
DM8:达梦数据库-锁超时