IT评测·应用市场-qidao123.com

标题: OpenFeign-远程调用工具 [打印本页]

作者: 熊熊出没    时间: 2023-2-15 11:42
标题: OpenFeign-远程调用工具
介绍

声明式的http客户端,底层还是HttpClient,可以解决RestTemplate硬编码进行远程服务调用的缺点
官网:https://github.com/OpenFeign/feign
入门

以A微服务对B微服务远程调用为例
若无多个微服务对B微服务调用的情况,第1、2步可在A微服务中完成
1.建立Feign模块并导入依赖
  1. <dependency>
  2.     <groupId>org.springframework.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>
复制代码
2.编写Feign的客户端BClient

BClient添加注解:@B
@B为B在nacos中的注册名
在使用Feign客户端接口时,强烈建议遵守如下几点要求:
  1. @FeignClient("BClient")
  2. public interface BClient {
  3.     @RequestMapping("/user/{id}")
  4.     void findById(@PathVariable("id") Long id);
  5. }
复制代码
3.A引导类增加注解

@EnableFeignClients(basePackages = {"BClient所在目录绝对路径"})
4.远程调用

注入BClient便可直接调用
  1. @Autowired
  2. private BClient bClient;
  3. public void findById(Long id){
  4.     bClient.findById(id);
  5. }
复制代码
#.Feign集成HttpClient(非必须)




免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4