OpenFeign-远程调用工具

打印 上一主题 下一主题

主题 889|帖子 889|积分 2667

介绍

声明式的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客户端接口时,强烈建议遵守如下几点要求:

  • 使用@RequestMapping注解
    古早版本只能识别@RequestMapping,便于与古早版本兼容

  • 请求参数需要指定参数名称
  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(非必须)


  • A微服务中导入依赖
    1. <dependency>
    2.     <groupId>io.github.openfeign</groupId>
    3.     <artifactId>feign-httpclient</artifactId>
    4. </dependency>
    复制代码
  • 配置文件
    1. feign:
    2.   httpclient:
    3.     enabled: true # 开启feign对HttpClient的支持
    4.     max-connections: 200 # 最大的连接数
    5.     max-connections-per-route: 50 # 每个路径的最大连接数
    复制代码



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

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表