Python 官方文档:入门教程 => 点击学习
目录OpenFeign概述OpenFeign使用引入依赖编写服务调用Service接口编写被调用的方法启动服务调用模块测试OpenFeign概述 OpenFeign 可以声明式的实现
OpenFeign 可以声明式的实现微服务之间的调用,我们只需要向调用接口一样,对 FeignClient 直接调用即可。
若不采用 OpenFeign,不同微服务之间的远程 api 调用通过Ribbon + RestTemplate
实现;而采用 OpenFeign 后直接采用接口 + 注解的方式即可实现。
本文仍然使用SpringCloud – Nacos中所创建的 order 订单微服务与 payment 支付微服务为例,在订单微服务中调用支付微服务的方法。
首先在 order 微服务中引入 OpenFeign 的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
创建 Service 接口,在接口中定义方法实现对其他微服务的调用
注意:
@FeignClient
括号里的值为被调用的微服务在 nacos 注册中心的注册服务名;@FeignClient("nacos-payment-provider")
public interface PaymentService {
@GetMapping("/paymentFeign/test")
String feignTest();
}
在两个被调用的 payment 服务中编写业务处理逻辑,此处为方便,直接返回相应的字符串
paymeng8001
@RestController
@RequestMapping("/paymentFeign")
public class PaymentFeignController8001 {
@GetMapping("/test")
public String test() {
return "8001支付模块test()方法被调用~";
}
}
payment8002
@RestController
@RequestMapping("/paymentFeign")
public class PaymentFeignController8002 {
@GetMapping("/test")
public String test() {
return "8002支付模块test()方法被调用~";
}
}
支付模块修改完成后,直接启动服务
在 order8005 模块的启动类上添加注解,标明 FeignClient 包的位置
@SpringBootApplication
@EnableFeignClients(basePackages = "com.zqf.service")
public class OrderStart8005 {
public static void main(String[] args) {
SpringApplication.run(OrderStart8005.class);
}
}
启动订单微服务
所有微服务启动成功后,使用 postman 进行测试
多次发送请求,可以看到两个支付微服务 payment 被轮流调用,即 openfeign 自带负载均衡的功能行测试
到此这篇关于SprinGCloud OpenFeign概述与使用的文章就介绍到这了,更多相关SpringCloud OpenFeign内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringCloudOpenFeign概述与使用
本文链接: https://www.lsjlt.com/news/193729.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0