SpringCloud OpenFeign服务接口调用
介绍
OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。
Feign和OpenFeign区别
- Feign
Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端
Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。
- OpenFeign
OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。
实战一
1.新建模块
新建模块:cloud-consumer-feign-order80
2.修改pom
<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.sgtech.springcloud</groupId>
<artifactId>cloud-api-common</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.修改配置
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
4.添加启动类
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80
{
public static void main(String[] args)
{
SpringApplication.run(OrderFeignMain80.class,args);
}
}
5.添加业务类
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService
{
@GetMapping(value = "/payment/get/{id}")
CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
@RestController
public class OrderFeignController
{
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
{
return paymentFeignService.getPaymentById(id);
}
}
测试
- 启动服务注册中心:7001和7002
- 启动支付服务:8001和8002
- 启动订单服务:80
- 浏览器访问:
http://localhost/consumer/payment/get/22

实战二
默认Feign客户端只等待一秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了,直接返回报错。
为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制。
1.支付模块添加超时接口
@GetMapping(value = "/feign/timeout")
public String paymentFeignTimeOut() {
System.out.println("*****paymentFeignTimeOut from port: " + serverPort);
//暂停几秒钟线程
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return serverPort;
}
2.订单模块添加超时接口
PaymentFeignService添加方法
@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeOut();
OrderFeignController添加方法:
@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeOut()
{
return paymentFeignService.paymentFeignTimeOut();
}
3.测试
浏览器访问: http://localhost/consumer/payment/feign/timeout
发现超时了,因为我们在支付模块中设置了睡眠3秒钟后返回!!

4.修改订单模块超时控制
添加以下配置,表示把超时时间拉长到5秒:
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
再次访问接口,等待几秒后正常返回了结果。

项目代码
https://gitee.com/indexman/cloudstudy
SpringCloud OpenFeign服务接口调用的更多相关文章
- SpringCloud项目,接口调用返回http 500 - Internal Server Error的错误
今天上班的时候,自己正在参与的Spring Cloud项目出现了问题,原本上周五还正常的项目突然所有接口调用都是返回http 500的错误. 项目的状态是在Eureka上可以看到对应微服务是在线状态, ...
- SpringCloud实现服务间调用(RestTemplate方式)
上一篇文章<SpringCloud搭建注册中心与服务注册>介绍了注册中心的搭建和服务的注册,本文将介绍下服务消费者调用服务提供者的过程. 本文目录 一.服务调用流程二.服务提供者三.服务消 ...
- 微服务架构 | 4.2 基于 Feign 与 OpenFeign 的服务接口调用
目录 前言 1. OpenFeign 基本知识 1.1 Feign 是什么 1.2 Feign 的出现解决了什么问题 1.3 Feign 与 OpenFeign 的区别与对比 2. 在服务消费者端开启 ...
- SpringCloud微服务框架复习笔记
SpringCloud微服务框架复习笔记 什么是微服务架构? 微服务是一种软件开发技术,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.每个服务运行在其独立的进 ...
- SpringCloud微服务:基于Nacos组件,整合Dubbo框架
源码地址:GitHub·点这里 || GitEE·点这里 一.基础组件简介 1.Dubbo框架 Dubbo服务化治理的核心框架,之前几年在国内被广泛使用,后续由于微服务的架构的崛起,更多的公司转向微服 ...
- Slickflow.NET 开源工作流引擎基础介绍(一) -- 引擎基本服务接口API介绍
1. 工作流术语图示 图1 流程图形的BPMN图形元素表示 1) 流程模型定义说明流程(Process):是企 ...
- 引擎基本服务接口API介绍
Slickflow.NET 开源工作流引擎基础介绍(一) -- 引擎基本服务接口API介绍 https://www.cnblogs.com/slickflow/p/4807227.html 工作流术语 ...
- SpringCloud Alibaba实战(8:使用OpenFeign服务调用)
源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在上一个章节,我们已经成功地将服务注册到了Nacos注册中心,实现了服务注册和服务发 ...
- SpringCloud微服务服务间调用之OpenFeign介绍
开发微服务,免不了需要服务间调用.Spring Cloud框架提供了RestTemplate和FeignClient两个方式完成服务间调用,本文简要介绍如何使用OpenFeign完成服务间调用. Op ...
- SpringCloud微服务之跨服务调用后端接口
SpringCloud微服务系列博客: SpringCloud微服务之快速搭建EurekaServer:https://blog.csdn.net/egg1996911/article/details ...
随机推荐
- [转帖]字符集 AL32UTF8 和 UTF8
https://blog.51cto.com/comtv/383254# 文章标签职场休闲字符集 AL32UTF8 和 UTF8文章分类数据库阅读数1992 The difference betwee ...
- [转帖]关于UNDO
原文地址:https://www.modb.pro/db/70802?xzs= 一:请描述什么是Oracle Undo. 二:请描述UNDO的作用. 三:请谈谈你对Manual Undo Manage ...
- [转帖]原创经典:SQLSERVER SendStringParametersAsUnicode引发的疑案 推荐
https://developer.aliyun.com/article/429563 简介: 上周五碰到开发的请求协助解决数据预定程序中对单头等几个表检索数据时检索条件尾数是9的数据特别慢.第一时间 ...
- Oracle数据库权限学习--表或者是视图不存在
Oracle数据库权限学习--表或者是视图不存在 摘要 本文写于: 12.10 01:00 巴西踢的太烂了 帮同事看一下补丁执行报错的问题. 问题原因很简单. user_all_table能够后去本用 ...
- expect 的简单学习与使用
背景 最近工作中总有很多重复的事项. 比较繁琐,想着能够简单一点是一点的角度 准备采用expect来建华部分工作量. 其实还可以使用其他方式来处理. 但是感觉expect还是能够简单明了的. 所以暂时 ...
- Redis 菜鸟进阶
Redis 菜鸟进阶 背景 最近产品一直要优化性能,加强高可用. 有一个课题是Redis高可用与性能调优. 我这边其实获取到的内容很有限. 最近济南疫情严重,自己锁骨骨折. 然后通勤时间基本上都用来查 ...
- 容器方式运行Mysql8.0.26的方法
容器化运行Mysql8.0.26测试环境的方法 1. 前言 之前为了好处理,都是二进制包的方式安装mysql,但是有时候需要下载和安装也比较费时费力, 今天中午在弄Oracle RAC时想着以后能够容 ...
- Vite 按需引入 Ant Design Vue 3.0
Vite 按需引入 Ant Design Vue 3.0 第一步下载: npm i unplugin-vue-components -D 需要注意的是:Vite你可以用 unplugin-vue-co ...
- 我对computed的理解-以及computed的传参
computed 传参 <template> <div> <p>computed传参的写法:{{ who1Params('--我是传参的内容') }}</p& ...
- 学习React中的jsx-保证你看的明明白白
安装react的脚手架 1==>行 npx create-react-app myreactdemo01 解释一下: npx create-react-app 你的项目名称 npx create ...