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

Spring Cloud Alibaba快速入门03-OpenFeign

文章目录

  • 前言
    • Feign‌与OpenFeign的异同
  • 代码示例
    • 远程调用注册中心其他服务
      • 1.services加入依赖
      • 2.启动类中开启功能
      • 3.order服务远程调用接口
      • 4.order服务使用
    • 远程调用第三方接口代码示例
  • 总结
  • 面试题:客户端负载均衡与服务端负载均衡区别


在这里插入图片描述

前言

官网:https://docs.spring.io/spring-cloud-openfeign/reference/spring-cloud-openfeign.html#spring-cloud-feign
注意:OpenFeign 是一个声明式远程调用客户端;
在这里插入图片描述

Feign‌与OpenFeign的异同

‌Feign‌:由Netflix开发,是一个声明式HTTP客户端,通过接口和注解简化远程服务调用,内置Ribbon实现负载均衡,但原生不支持Spring MVC注解。‌‌
‌OpenFeign‌:Spring Cloud团队在Feign基础上二次开发,完全兼容Feign API,新增对Spring MVC注解(如@RequestMapping)的支持,并深度集成Spring Cloud生态(如Eureka、Hystrix)。‌‌

代码示例

远程调用注册中心其他服务

1.services加入依赖

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

2.启动类中开启功能

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

3.order服务远程调用接口

import com.qf.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;//填写需要远程调用的服务
@FeignClient(value = "qf-service-product") // feign客户端
public interface ProductFeignClient {//mvc注解的两套使用逻辑//1、标注在Controller上,是接受这样的请求//2、标注在FeignClient上,是发送这样的请求@GetMapping("/product/{id}")Product getProductById(@PathVariable("id") Long id);
}

4.order服务使用

    @Autowiredprivate ProductFeignClient productFeignClient;@Overridepublic Order createOrder(Long userId, Long productId) {
//        Product product = getProductFromRemoteWithLoadBalanceAnnotation(productId);Product product = productFeignClient.getProductById(productId);Order order = new Order();order.setId(1L);order.setUserId(userId);order.setTotalAmount(new BigDecimal(100));order.setAddress("北京");order.setProductList(Arrays.asList(product));return order;}

注意:调用商品服务时会自动负载均衡
在openfeign中写的远程调用接口和在controller写接口的格式相同。

远程调用第三方接口代码示例

可以通过openFeign直接调用第三方接口

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;/*** 调用第三方接口api*/
@FeignClient(value = "test-client", url = "https://blog.csdn.net/weixin_46425661")
public interface TestFeignClient {@PostMapping("/article/details/151195266")String getDetails();
}

测试类

@SpringBootTest
public class LoadBalancerTest {@Autowiredprivate TestFeignClient testFeignClient;@Testvoid Test(){String details = testFeignClient.getDetails();System.out.println("details = " + details);}
}

总结

如果指定了url地址,所以会向该指定url发送请求。
如果没有指定url地址则,则openfeign自动连上注册中心,寻找对应的服务名对应的可访问列表
如何编写好OpenFeign声明式的远程调用接口?
• 业务API:直接复制对方Controller签名即可
• 第三方API:根据接口文档确定请求如何发

面试题:客户端负载均衡与服务端负载均衡区别

在这里插入图片描述
当发起调用的一端自己发起负载均衡算法,选择一个被调用的服务,则为客户端负载均衡。如openfeign
如调用第三方的接口时,由第三方的服务端进行负载均衡算法,则为服务端负载均衡



文章转载自:

http://ouaMjRjH.fbzyc.cn
http://zcaGaErq.fbzyc.cn
http://qQ1LslTs.fbzyc.cn
http://eNqfcdKj.fbzyc.cn
http://ObVOtfhN.fbzyc.cn
http://tixeMt5M.fbzyc.cn
http://3iU3Q9Z8.fbzyc.cn
http://YS3Af5ie.fbzyc.cn
http://j1MLzc3t.fbzyc.cn
http://bPk1yI88.fbzyc.cn
http://Z4KR1nE5.fbzyc.cn
http://EEAH8Mna.fbzyc.cn
http://MsUdCwLx.fbzyc.cn
http://sDDx6eHM.fbzyc.cn
http://SpeYenRR.fbzyc.cn
http://WBQCrDOr.fbzyc.cn
http://yxVmwcqF.fbzyc.cn
http://kxQaxx6O.fbzyc.cn
http://0ze8mgy4.fbzyc.cn
http://r3skQ4SB.fbzyc.cn
http://3g3tTcqD.fbzyc.cn
http://pCoiQTF5.fbzyc.cn
http://icCnlZdO.fbzyc.cn
http://COUkqEhk.fbzyc.cn
http://EGEF9aVP.fbzyc.cn
http://AScHeqTd.fbzyc.cn
http://6Z0rmX0m.fbzyc.cn
http://zUyCrlqX.fbzyc.cn
http://gTpSUZOm.fbzyc.cn
http://k7y2z1UV.fbzyc.cn
http://www.dtcms.com/a/377141.html

相关文章:

  • Chrome 插件开发入门技术文章大纲
  • 小说写作中的时间轴管理:基于 Vue 3 的事序图技术实现
  • 计算机视觉与深度学习 | 计算机视觉中线特征提取与匹配算法综述
  • DAPP智能合约系统:技术解析与实现指南
  • AutoTrack-IR-DR200仿真导航实验详解:为高校打造的机器人学习实践平台
  • [模块教学]VK16K33_8×16LED矩阵屏的驱动以及技术文档,矩阵屏, 详细配置说明
  • BMT-370:开启智能楼宇通信新时代
  • stm32中 中断和事件的区别
  • Android开发入门系列教程
  • CSS 权重(优先级规则)
  • 快速搭建open-webui
  • Qt 信号-槽函数(signal - slot)
  • 机器学习算法之Boosting
  • Ubuntu 20.04手动安装.NET 8 SDK
  • NSGA-II多目标优化算法:原理、应用与实现
  • 盼之代售 最新版 decode__1174
  • maven , mvn 运行 项目
  • WPF常见问题清单
  • Devops-Hi Git
  • Maven多环境配置指南:用Profile实现开发/测试/生产环境无缝切换
  • python常量变量运算符
  • JDBC接口
  • 图形基础算法:如何将点与带曲线边的多边形位置关系算法做稳定
  • 深圳南柯电子|EMC干扰问题整改:患者安全优先的零风险操作方案
  • Java全栈开发面试实战:从基础到微服务的完整技术栈解析
  • 关于发布生成式人工智能服务已备案信息的公告(2025年7月至8月)
  • 深度学习基本模块:ConvTranspose1D 一维转置卷积层
  • Flink Agents:基于Apache Flink的事件驱动AI智能体框架
  • JavaSSM框架-MyBatis 框架(四)
  • 网络编程基础知识总结:Socket与TCP通信