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

openFeign远程调用

  由于RestTemplate的灵活性不是很高因此我们有更好的方法事项远程调用,OpenFeign是一个声明式的Web Services客户端,类似于Controller调用Services,因此灵活性较高。

  提前声明一下,由于Feign有两个starter我们在这里使用的是:spring-cloud-starter-openfeign。

  在使用之前我们需要引入相关依赖:

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

     由于此处我们是想要更好的进行”远程调用" 因此我们只需要在调用的一段加入这个依赖就行,服务端就没有必要加入依赖了。

  添加依赖之后我么还需要使用@EnableFeignClients开启openfeign的功能,最后在调用端设置一个接口,这个接口就是进行远程调用的接口,并且在这个接口上添加@FeignClient注解但是任然需要设置一些参数,之后我们会进行介绍。

@FeignClient(value = "product-service",path = "/product")   //进行远程调用
public interface ProductApi {@RequestMapping("/{productId}")ProductInfo getProductById( @PathVariable("productId") Integer productId);
}

  接下来我们对@FeignClient中的参数进行讲解:

 value:表示你想要调用的服务端的namespace,也就是微服务的名称。

 path:定义当前FeignClient的统⼀前缀,也就是表示你想要调用的服务端的类似于作用在类之外的@RestMapping(“##”)中的“##”。

  通过上述方式就可以在客户端进行远程调用,调用服务端product-Service的数据。

  

  openfeign参数传递 

    传递单个参数:

@FeignClient(value = "product-service", path = "/product")
public interface ProductApi {
@RequestMapping("/p1")
String p1(@RequestParam("id") Integer id);
}

    当然这里的@RequestParam并不是必须要添加的,根据响应的情况进行添加。设置之后就可以在调用端中对应的Service引入ProductApi并且调用p1方法就可以或去掉响应的返回值。

    传递多个参数:

     @RequestMapping("/p2")
String p2(@RequestParam("id")Integer id,@RequestParam("name")String name);

    由此我们可以看出传递多个参数和传递单个参数十分相似,当然事实也是这样的,相较于传递单个参数传递多个参数只不过得将参数的个数增加了,其他的并没有什么改变。

    传递对象:

@RequestMapping("/p3")
String p3(@SpringQueryMap ProductInfo productInfo);

    传递对象就和前两者有所不同了 需要使用到@SpringQueryMap注解,使用时我们也就只需要将对象传递进去即可进行远程调用。

  传递Json格式的数据:

@RequestMapping("/p4")
public String p4(@RequestBody ProductInfo productInfo){
return "接收到对象, productInfo:"+productInfo;
}

  传递Json的数据是需要添加@RequestBody注解。

 

 


文章转载自:
http://audacious.hyyxsc.cn
http://amie.hyyxsc.cn
http://bouquetiere.hyyxsc.cn
http://bias.hyyxsc.cn
http://bicomponent.hyyxsc.cn
http://airdrome.hyyxsc.cn
http://bywalk.hyyxsc.cn
http://bevy.hyyxsc.cn
http://boh.hyyxsc.cn
http://abiding.hyyxsc.cn
http://accidentproof.hyyxsc.cn
http://alone.hyyxsc.cn
http://chorten.hyyxsc.cn
http://anger.hyyxsc.cn
http://beg.hyyxsc.cn
http://battement.hyyxsc.cn
http://choush.hyyxsc.cn
http://agadir.hyyxsc.cn
http://aus.hyyxsc.cn
http://ceraceous.hyyxsc.cn
http://bedstand.hyyxsc.cn
http://caelian.hyyxsc.cn
http://backmost.hyyxsc.cn
http://antichurch.hyyxsc.cn
http://antitail.hyyxsc.cn
http://anomie.hyyxsc.cn
http://barytron.hyyxsc.cn
http://acapriccio.hyyxsc.cn
http://actomyosin.hyyxsc.cn
http://biliverdin.hyyxsc.cn
http://www.dtcms.com/a/187748.html

相关文章:

  • 中国版Cursor:基于CodeBuddy与EdgeOne Pages的在线键盘测试工具开发方案
  • 【C++】多线程和多进程
  • 【实战】基于 ABP vNext 构建高可用 S7 协议采集平台(西门子 PLC 通信全流程)
  • VR和眼动控制集群机器人的方法
  • Flannel Host-gw模式的优缺点
  • 生成式图像水印研究综述
  • 一键转换上百文件 Word 批量转 PDF 软件批量工具
  • C程序的存储空间分配
  • 【OpenCV】网络模型推理的简单流程分析(readNetFromONNX、setInput和forward等)
  • 大容量存储的高性能 T-BOX 方案对智能网联汽车的支撑
  • 汽车工厂数字孪生实时监控技术从数据采集到三维驱动实现
  • 数字孪生实时监控汽车零部件工厂智能化巡检新范式
  • 修改(替换)文件中的指定内容并保留文件修改前的时间(即修改前后文件的最后修改时间保持不变)
  • [学习] RTKLib详解:qzslex.c、rcvraw.c与solution.c
  • matlab多智能体网络一致性研究
  • Linux(1)编译链接和gcc
  • 动态域名服务ddns怎么设置?如何使用路由器动态域名解析让外网访问内网?
  • Vitrualbox完美显示系统界面(只需三步)
  • 【源码+文档+调试讲解】党员之家服务系统小程序1
  • 数据治理域——日志数据采集设计
  • k8s之k8s集群部署
  • 【漫话机器学习系列】257.填补缺失值(Imputing Missing Values)
  • 【LeetCode 热题 100】二叉树的最大深度 / 翻转二叉树 / 二叉树的直径 / 验证二叉搜索树
  • 机器学习——聚类算法练习题
  • 我们该如何使用DeepSeek帮我们减负?
  • flowable
  • 芦康沙妥珠单抗说明书摘要
  • MUSE Pi Pro 编译kernel内核及创建自动化脚本进行环境配置
  • Azure 应用的托管身份与服务主体
  • STM32 启动文件分析