当前位置:网站首页>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
边栏推荐
- 消息隊列的使用
- 秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
- MySQL high level statements (I)
- log4j. Detailed configuration of properties
- Snipaste, the world's strongest screenshot software
- Unlock the secret of C language key words (issue 6)
- [high frequency interview questions] difficulty 1.5/5, LCS template questions
- 【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
- Self taught ADT and OOP
- yml的配置
猜你喜欢

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

今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献

What's the matter with Amazon's evaluation dropping and failing to stay? How to deal with it?

Word text box page feed
![[fans' welfare] today, I'd like to introduce a method to collect money for nothing - convertible bonds. I personally verified that each person can earn 1500 yuan a year](/img/b8/8de207734e4aa85778ca1f0609ddf2.png)
[fans' welfare] today, I'd like to introduce a method to collect money for nothing - convertible bonds. I personally verified that each person can earn 1500 yuan a year

Unlock the secret of C language key words (issue 6)

Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
![[on Nacos] get started quickly](/img/cc/af4ab640952b880595a89f66688ff5.jpg)
[on Nacos] get started quickly

TiDB 6.0:让 TSO 更高效丨TiDB Book Rush

行业洞察 | 新零售业态下,品牌电商应如何重塑增长?
随机推荐
全球最强截图软件 Snipaste
Convn-n dimensional convolution
Interview shock 60: what will cause MySQL index invalidation?
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
Hands on API development
Utilisation de la file d'attente des messages
Getting started with go web programming: validators
关闭windows defender安全中心的方法
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
Talk about go language and cloud native technology
Script defer async mode
和动态规划的第一次相遇
PyQt,PySide-槽函数被执行了两次
ssh工作流程及原理
带你认识图数据库性能和场景测试利器LDBC SNB
MySQL high level statements (I)
LeetCode_ Fast power_ Recursion_ Medium_ 50.Pow(x, n)
printf不定长参数原理
Quanzhi A13 tossing memo
Word text box page feed