ToB企服应用市场:ToB评测及商务社交产业平台

标题: 声明式调用 —— SpringCloud OpenFeign [打印本页]

作者: 莱莱    时间: 2023-10-10 17:56
标题: 声明式调用 —— SpringCloud OpenFeign
Feign 简介

Spring Cloud Feign 是一个 HTTP 请求调用的轻量级框架,可以以 Java 接口注解的方式调用 HTTP 请求,而不用通过封装 HTTP 请求报文的方式直接调用
Feign 通过处理注解,将请求模板化,当实际调用的时候传入参数,根据参数再应用到请求上,进而转化成真正的请求

第一个 Feign 程序

本小节介绍如何通过 Nacos+Feign 实现服务之间的调用,新建 server-01、server-02 项目,并分别注册 Nacos
server-01 引入依赖
  1. <dependency>
  2.     <groupId>org.springframework.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>
  5. <dependency>
  6.     <groupId>org.springframework.cloud</groupId>
  7.     <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  8. </dependency>
复制代码
新建 Server02FeignClient 接口,用于调用 server-02 提供的对外接口
  1. // name:要调用的服务名
  2. FeignClient(name = "server-02")
  3. public interface Server02FeignClient {
  4.     @GetMapping("/test/getConfig")
  5.     void getConfig();
  6. }
复制代码
启动类加上注解 @EnableFeignClients
  1. @EnableFeignClients
  2. @EnableDiscoveryClient
  3. @SpringBootApplication
  4. public class Server01Application {
  5.     public static void main(String[] args) {
  6.         SpringApplication.run(Server01Application.class, args);
  7.     }
  8. }
复制代码
使用 Server02FeignClient 调用 server-02 的接口
  1. @Slf4j
  2. @RestController
  3. public class TestCon {
  4.    
  5.     @Autowired
  6.     private Server02FeignClient server02FeignClient;
  7.     @GetMapping("/test/getConfigByFeign")
  8.     public void getConfigByFeign() {
  9.         server02FeignClient.getConfig();
  10.     }
  11. }
复制代码
在 server-02 创建接口
  1. @Slf4j
  2. @RestController
  3. public class TestCon {
  4.     @Value("${test.value}")
  5.     private String testValue;
  6.     @Value("${spring.application.name}")
  7.     private String applicationName;
  8.     @Value("${server.port}")
  9.     private String port;
  10.     @GetMapping("/test/getConfig")
  11.     public void getConfig() {
  12.         log.info("testValue: {} by {}-{}", testValue, applicationName, port);
  13.     }
  14. }
复制代码
调用 server-01 的 /test/getConfigByFeign 接口,就会通过 Feign 调用 server-02 的 /test/getConfig 接口
@FeignClient  注解可作用在类、接口、枚举上,主要包含如下属性:
  1. @FeignClient(contextId = "fooClient", name = "stores", configuration=FooConfiguration.class)
  2. public interface FooClient {...}
  3. @FeignClient(contextId = "barClient", name = "stores", configuration=BarConfiguration.class)
  4. public interface BarClient {...}
复制代码
spring-cloud-starter-openfeign 支持 spring-cloud-starter-netflix-ribbon 和 spring-cloud-starter.loadbalancer,如果 Ribbon 在类路径中且已启用,则 Client feignClient 是 LoadBalancerFeignClient,如果 SpringCloud LoadBalancer 在类路径中,则使用 FeignBlockingLoadBalancerClient
默认情况下,Spring Cloud OpenFeign 不会为 Feign 提供以下 bean 对象,但是仍然会从应用程序上下文中查找这些类型的 bean 以创建 Feign 客户端:
以上是通过注解 @FeignClient 的配置属性进行配置的,我们也可以使用配置文件进行配置
  1. feign:
  2.     client:
  3.         config:
  4.             feignName:
  5.                 connectTimeout: 5000
  6.                 readTimeout:5000
  7.                 loggerLevel: full
  8.                 errorDecoder: com.example.SimpleErrorDecoder
  9.                 retryer: com.example,SimpleRetryer
  10.                 requestInterceptors:
  11.                     - com.example.FooRequestInterceptor
  12.                     - com.example,BarRequestInterceptor
  13.                 decode404: false
  14.                 encoder: com.example.SimpleEncoder
  15.                 decoder: com.example.SimpleDecoder
  16.                 contract: com.example.SimpleContract
复制代码
可以在 @EnableFeignClients 属性 defaultConfiguration 中指定默认配置,不同之处在于此配置将适用于所有 Feign 客户端
如果希望使用配置文件来配置所有 @FeignClient,则可以使用默认 Feign 名称创建配置属性,例如:
  1. feign:
  2.     client:
  3.         config:
  4.             default:
  5.                 connectTimeout: 5000
  6.                 readTimeout: 5000
  7.                 loggerLevel: basic
复制代码
如果同时创建 @Configuration bean 和配置文件,则配置文件将覆盖 @Configuration 值,如果要将优先级更改为 @Configuration,就可以将 feign.client.default-to-properties 更改为 false

Feign 传参

以下服务端接口可通过 Get 或 Post 请求调用并接收参数
  1. @RequestMapping("/test/testFeignWithParam")
  2. public void testFeignWithParam(@RequestParam String name,
  3.                                @RequestParam int age) {
  4.     log.info("testFeignWithParam: name-{}, age-{}", name, age);
  5. }
复制代码
通过在 Url 拼接请求传参如下:
  1. @FeignClient(name = "server-02", path = "server-02")
  2. public interface Server02FeignClient {
  3.     @GetMapping("/test/testFeignWithParam?name=zhanghsan&age=66")
  4.     //@PostMapping("/test/testFeignWithParam?name=zhanghsan&age=66")
  5.     void testFeignWithParam();
  6. }
复制代码
使用 @RequestParam 传参如下:
  1. @FeignClient(name = "server-02", path = "server-02")
  2. public interface Server02FeignClient {
  3.    
  4.     //@GetMapping("/test/testFeignWithParam")
  5.     @PostMapping("/test/testFeignWithParam")
  6.     void testFeignWithParam(@RequestParam("name") String name,
  7.                             @RequestParam("age") int age);
  8. }
复制代码
也可以使用 OpenFeign 的 @QueryMap 将请求实体作为参数的映射,不过由于 @QueryMap 注解与 Spring 不兼容,所以 OpenFeign 提供了等效的 @SpringQueryMap 注解
  1. @FeignClient(name = "server-02", path = "server-02")
  2. public interface Server02FeignClient {
  3.    
  4.     //@GetMapping("/test/testFeignWithQueryMap")
  5.     @PostMapping("/test/testFeignWithQueryMap")
  6.     void testFeignWithQueryMap(@SpringQueryMap FeignParam param);
  7. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4