当前位置:   article > 正文

SpringCloud-OpenFeign

SpringCloud-OpenFeign

一 OpenFeign是什么?有什么用?

以往我们是通过 RestTemplate 发起远程调用,如下:

存在问题如下:

  • 代码可读性差,编程体验不统一
  • 参数复杂URL难以维护

Feign  是一个声明式的 http 客户端,其作用就是用来把我们解决上述问题的~

二 OpenFeign 客户端的使用

 2.1 远程调用

1.引入依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>

注意:由于SpringCloud Feign高版本(例如 2020.1.0)不使用Ribbon而是使用spring-cloud-loadbalancer,所以需要引用spring-cloud-loadbalancer或者降版本

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-loadbalancer</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.github.ben-manes.caffeine</groupId>
  7. <artifactId>caffeine</artifactId>
  8. </dependency>

2.在order-service(发起远程调用的微服务)的启动类添加注解开启Feign的功能 

 

3 编写客户端请求

主要是基于SpringMVC的注解来声明远程调用的信息,比如:

  • 服务名称:userservice
  • 请求方式:GET
  • 请求路径:/user/{id}
  • 请求参数:Long id
  • 返回值类型:User
  1. package cn.itcast.feign.clients.clients;
  2. import cn.itcast.feign.pojo.User;
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.stereotype.Component;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. @Component
  8. @FeignClient("userservice")
  9. public interface UserClient {
  10. @GetMapping("/user/{id}")
  11. User finById(@PathVariable Long id);
  12. }

如果是高版本的 SpringCloud,那么就需要使用 SpringCloud LoadBalencer 做负载均衡(也比较建议).

具体的添加@LoadBalancerClient注解:在调用服务的Service类上添加@LoadBalancerClient注解,并指定服务名。这样,负载均衡器将根据配置的属性来选择合适的服务实例进行调用。

4.通过 OpenFeign 客户端发起远程调用

用 Feign 来代替 RestTemplate 是不是十分优雅~

2.2、自定义 OpenFeign 配置

OpenFeign早期版本(例如 Hoxton.SR6 )要求服务提供方在1秒内处理业务逻辑并返回响应。如果超过1秒没有返回,OpenFeign会直接报错,不会等待服务执行。随着版本的更新,OpenFeign已经对此做出了调整或优化(例如 2021.0.1)。

超时报错如下:

修改超时时间: 

1.在远程调用方的 application.yml 中配置,指定某个服务提供方的调用超时时间

  1. feign:
  2. client:
  3. config:
  4. product: # 服务名
  5. connect-timeout: 5000 # 配置指定服务连接超时时间
  6. read-timeout: 5000 # 配置指定服务等待超时时间

2.在远程调用方的 application.yml 中配置,指定所有服务提供方的调用超时时间 

  1. feign:
  2. client:
  3. config:
  4. default: # 所有服务
  5. connect-timeout: 5000 # 配置指定服务连接超时时间
  6. read-timeout: 5000 # 配置指定服务等待超时时间

2.3 Feign的最佳使用

将FeignClient抽取为独立模块,并且把接口有关的POJO(实体类)、默认的Feign配置都放到这个模块中,提供给所有消费者使用

具体步骤:

1.首先创建一个module,命名为feign-api,然后引入feign的starter依赖

  1. <!--feign 客户端依赖-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-openfeign</artifactId>
  5. </dependency>

 2.将order-service中编写的UserClient、User、DefaultFeignConfiguration都复制到feign-api项目中

3.在order-service中引入feign-api的依赖

4.重启测试

项目必然会报以下错误:

 

UserClients 之前有对象是因为扫描到 @FeignClient 注解注入了对象 ,而现在 order-service 扫描包的范围是启动类下的包,但由于我们刚刚把 UserClients 挪到了 feign-api 这个 Module 中,因此,扫描不到该注解,无法注入对象。

总而言之:当定义的FeignClient不在SpringBootApplication的扫描包范围时,这些FeignClient无法使用。

有以下两种解决方式:

方式一:指定FeignClient所在包

@EnableFeignClients(basePackages = "cn.itcast.feign.clients")

这种方式会将指定包下的所有东西都拿过来。

方式二(推荐):指定FeignClient字节码

@EnableFeignClients(clients = {UserClient.class})

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/671350
推荐阅读
相关标签
  

闽ICP备14008679号