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

在微服务中,如何使用feign在各个微服务中进行远程调用

在微服务中,如何使用feign在不同微服务中进行远程调用

在微服务中,如何使用feign在不同微服务中进行远程调用

步骤:

第一步:

引入feign依赖

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>

这里没有指定版本号是因为…中的spring-cloud指定了版本号,如下

    <dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

第二步:在启动类中添加@EnableFeignClients注解

@EnableFeignClients //开启远程调用
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

第三步:编写远程调用接口。现search服务需要调用product服务下/product/attr/info/{attrId}路径下的方法,该被调用的方法为:

    @RequestMapping("/product/attr/info/{attrId}")public R info(@PathVariable("attrId") Long attrId){AttrRespVo respVo = attrService.getAttrInfo(attrId);return R.ok().put("attr", respVo);}

那么在search服务中编写的接口如下:我们需要保证接口中的方法与product中被调用的方法,方法名可以不同,但是他们的参数返回值类型请求路径要一致。接口方法发送get请求.

@FeignClient(name = "product",url = "http://localhost:10000")
public interface ProductFeignService {
@GetMapping("/product/attr/info/{attrId}")public R attrInfo(@PathVariable("attrId") Long attrId);}

第四步:在业务中远程调用,如下:

@Autowired 
ProductFeignService productFeignService;// 远程调用
Long attrId=1;
R r = productFeignService.attrInfo(attrId);
http://www.lryc.cn/news/534818.html

相关文章:

  • Kafka中的KRaft算法
  • vue3 -- 集成 amap(高德地图)
  • 基于用户的协同过滤算法推荐
  • 4.python+flask+SQLAlchemy+达梦数据库
  • 神经网络常见激活函数 4-LeakyReLU函数
  • PHP盲盒商城系统源码 晒图+免签+短信验证+在线回收 thinkphp框架
  • 单例模式详解(Java)
  • 2025年度Python最新整理的免费股票数据API接口
  • 2.10学习总结
  • 原生鸿蒙版小艺APP接入DeepSeek-R1,为HarmonyOS应用开发注入新活力
  • 从Word里面用VBA调用NVIDIA的免费DeepSeekR1
  • 【SpringBoot篇】基于Redis分布式锁的 误删问题 和 原子性问题
  • 【JVM详解三】垃圾回收机制
  • MySQL的字符集(Character Set)和排序规则(Collation)
  • 2025影视泛目录站群程序设计_源码二次开发新版本无缓存刷新不变实现原理
  • 常用的python库-安装与使用
  • array_walk. array_map. array_filter
  • 数据仓库和商务智能:洞察数据,驱动决策
  • Vue设计模式到底多少种?
  • HTML 属性
  • oracle如何查询历史最大进程数?
  • SpringBoot单机模式,能否支持一万用户请求并发?
  • [前端]CRX持久化
  • 模型 替身决策
  • 【系统架构设计师】体系结构文档化
  • Python Pandas(5):Pandas Excel 文件操作
  • 区块链技术:Facebook 重塑社交媒体信任的新篇章
  • 跨平台App开发,有哪些编程语言和工具,比较一下优劣势?
  • Windows逆向工程入门之汇编环境搭建
  • 网络安全溯源 思路 网络安全原理