当前位置: 首页 > backend >正文

OpenFeign 快速开始

我们今天来学习 OpenFeign,我们将会搭建一个 order-openfeign 服务,通过 openfeign 调用 user 服务。

一、项目准备

在 Nacos 快速开始 的父项目基础上

1.1、从 order 模块复制出 order-openfeign 模块

1.2、修改 order-openfeign pom.xml artifactId

<artifactId>order-openfeign</artifactId>

1.3、修改父工程 pom.xml 

增加 order-openfeign 模块

<modules><module>user</module><module>order</module><module>order-openfeign</module>
</modules>

二、Spring Cloud Alibaba 整合 OpenFeign

2.1、引入 openfeign 依赖

<!--openfeign 场景启动器 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency><!--openfeign 依赖 loadbalancer -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

2.2、编写接口加上 @FeignClient 注解

@FeignClient(value = "user-service", path = "/user")
public interface UserFeignService {@RequestMapping("/findUserById/{id}")String findUserById(@PathVariable("id") String id);}
------------------------------------------------------
-- feign 接口可以参考被调用方的 controller
@RestController
@RequestMapping("/user")
public class UserController {@RequestMapping("/findUserById/{id}")public String findUserById(@PathVariable String id) {// 模拟从数据库获取用户}}

注意:

  • @FeignClient 的 value 填的是被调用方的服务名,path 对应 @RequestMapping("/user")
  • Feign 接口的 @PathVariable 的 value 不能省略,否则会报 IllegalStateException

          PathVariable annotation was empty on param 0.

2.3、调用端添加 @EnableFeignClients 注解

有两种方式:

1)在启动类添加

@SpringBootApplication
@EnableFeignClients
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}
  • 当你在 Spring Boot 项目中使用 @EnableFeignClients 注解时,默认情况下,Spring 会扫描标注了该注解的类所在的包及其子包中的所有 Feign 客户端。
  • 因此,如果你将 @EnableFeignClients 放在启动类上(通常是项目的根包),那么它会自动扫描整个项目中的 Feign 客户端。

2)显式指定要扫描的包路径

// 用 basePackages 显示指定
@Configuration
@EnableFeignClients(basePackages="com.study.order.feign")
public class FeignConfig {
}

2.4、像调用本地方式一样调用远程服务

@RestController
@RequestMapping("/order")
public class OrderController {@Autowiredprivate UserFeignService userFeignService;@RequestMapping("/placeOrderFeign/{userId}")public String placeOrderFeign(@PathVariable String userId) {String response = userFeignService.findUserById(userId);return "用户[" + response + "]下单成功!";}
}

三、调用

启动 order-openfeign 模块跟两个 user 模块。

多次访问,会发现,用户服务在 8020、8021 之间来回切换。

从这能发现 OpenFeign 自动帮我们集成了 loadbalancer,实现负载均衡的调用。

http://www.xdnf.cn/news/2236.html

相关文章:

  • C++:string 1
  • YTJ笔记——FFT、NCC
  • Maven的聚合工程与继承
  • Pygame动画实战:让游戏角色动起来!
  • Java24 抗量子加密:后量子时代的安全基石
  • 华为盘古OS深度评测:构建AI自进化系统的实践密码
  • 第一性原理 | 从哲学本源到多领域实践的探索与应用
  • 腾讯二面:TCC分布式事务 | 图解TCC|用Go语言实现一个TCC
  • MyBatis 常用扩展组件详解(含代码示例)
  • 有源晶振与无源晶振详解:区别、应用与选型指南
  • 计算机视觉中的二值马尔科夫随机场
  • 代码随想录算法训练营第五十九天 | 1.ford算法精讲 卡码网94.城市间货物运输
  • 长短板理论——AI与思维模型【83】
  • 如何在 Windows 10 中使用 WSL 和 Debian 安装 Postgresql 和 Postgis
  • Vue3的内置组件 -实现过渡动画 TransitionGroup
  • 计算机二级MS Office第九套演示文稿
  • 隐私守护者的觉醒——大数据时代,我们如何对抗“透明人”危机?
  • 单链表专题(1)
  • 豆包,Kim,deepseek对比
  • Java——令牌技术
  • Oracle EBS 零金额的AP付款无法过账数据修复
  • 蓝桥杯赛场反思:技术与心态的双重修炼
  • 基于libdxfrw库读取样条曲线并离散为点
  • 在Linux虚拟机下使用vscode,#include无法跳转问题
  • 前端开发中shell的使用场景
  • 部署yolo到k230教程
  • cloud项目同一个服务,执行不同业务需求,nacos进行分组
  • 数据结构之单链表C语言
  • 论人际关系发展的阶段
  • Scratch——第19课 正话反说问题