pursue wind pursue wind
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
  • mybatis

  • mybatis-plus

  • Spring

  • SpringBoot

  • SpringSecurity

  • SpringCloud

    • 文档

      • SpringCloud-广告系统实战(七)----广告检索系统(加载全量索引)
      • SpringCloud-广告系统实战(三)---通用模块的开发
      • SpringCloud-广告系统实战(二)---搭建脚手架
      • SpringCloud-广告系统实战(五)---广告检索系统(微服务调用)
      • SpringCloud-广告系统实战(六)----广告检索系统(广告数据索引的设计与实现)
      • SpringCloud-广告系统实战(四)---投放系统的开发
      • SpringCloud-广告系统设计与实现(一)---概述
      • SpringCloud微服务实战(一)-简介
      • SpringCloud微服务实战(七)-异步和消息
      • SpringCloud微服务实战(七)-消息服务在电商中的实践
      • SpringCloud微服务实战(三)-服务注册与发现
      • SpringCloud微服务实战(二)-微服务介绍
      • SpringCloud微服务实战(六)-统一配置中心
      • SpringCloud微服务实战(十)-Hystrix
      • SpringCloud微服务实战(四)-微服务中的服务拆分
    • Gateway

    • Spring Cloud Stream

    • Alibaba Sentinel 规则参数总结
    • Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】
    • Feign常见问题总结
      • 一、使用案例
      • 二、FeignClient接口如使用@PathVariable ,必须指定value属性
      • 三、Java代码自定义Feign Client的注意点与坑
      • 四、@FeignClient 注解属性
      • 五、类级别的@RequestMapping会被Spring MVC加载
      • 六、首次请求失败
      • 七、如需产生Hystrix Stream监控信息,需要做一些额外操作
      • 本文首发
    • SentinelResource注解 属性总结
    • Spring Cloud Alibaba Sentienl相关配置项
    • SpringCloudAlibaba
    • SpringCloud入门
    • 使用Spring Cloud Feign上传文件
    • 如何使用Feign构造多参数的请求
    • 实用技巧:Hystrix传播ThreadLocal对象(两种方案)
    • 扩展Ribbon支持Nacos权重的三种方式
    • 扩展Ribbon支持基于元数据的版本管理
    • 搭建生产可用的Nacos集群
  • 单元测试框架Mockito
  • 框架
  • SpringCloud
pursuewind
2020-11-23
目录

Feign常见问题总结

# Feign常见问题总结

TIPS

本文基于Spring Clou Greenwich SR1,理论支持Spring Cloud Finchley及更高版本。

本文总结Feign常见问题及解决方案。

# 一、使用案例

  • 如何使用Feign构造多参数的请求 (opens new window)
  • 使用Spring Cloud Feign上传文件 (opens new window)
  • 使用Feign实现Form表单提交 (opens new window)

# 二、FeignClient接口如使用@PathVariable ,必须指定value属性

代码示例:

@FeignClient("microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)
  public User findById(@PathVariable("id") Long id);
  ...
}
1
2
3
4
5
6

在一些早期版本中,其中的@PathVariable("id") 中的"id",也就是value属性,必须指定,不能省略。

# 三、Java代码自定义Feign Client的注意点与坑

代码示例:

@FeignClient(name = "microservice-provider-user", configuration = UserFeignConfig.class)
public interface UserFeignClient {
  @GetMapping("/users/{id}")
  User findById(@PathVariable("id") Long id);
}

/**
 * 该Feign Client的配置类,注意:
 * 1. 该类可以独立出去;
 * 2. 该类上也可添加@Configuration声明是一个配置类;
 * 配置类上也可添加@Configuration注解,声明这是一个配置类;
 * 但此时千万别将该放置在主应用程序上下文@ComponentScan所扫描的包中,
 * 否则,该配置将会被所有Feign Client共享,无法实现细粒度配置!
 * 个人建议:像我一样,不加@Configuration注解
 *
 * @author zhouli
 */
class UserFeignConfig {
  @Bean
  public Logger.Level logger() {
    return Logger.Level.FULL;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  • 配置类上也可添加@Configuraiton 注解,声明这是一个配置类;但此时千万别将该放置在主应用程序上下文@ComponentScan 所扫描的包中,否则,该配置将会被所有Feign Client共享(相当于变成了通用配置,其实本质还是Spring父子上下文扫描包重叠导致的问题),无法实现细粒度配置!
  • 个人建议:像我一样,不加@Configuration注解,省得进坑。
  • 最佳实践:尽量用配置属性自定义Feign的配置!!!详见:跟我学Spring Cloud(Finchley版)-10-Feign深入 (opens new window)

# 四、@FeignClient 注解属性

@FeignClient(name = "microservice-provider-user")
1

在早期的Spring Cloud版本中,无需提供name属性,从Brixton版开始,@FeignClient必须提供name属性,否则应用将无法正常启动!

另外,name、url等属性支持占位符。例如:

@FeignClient(name = "${feign.name}", url = "${feign.url}")
1

# 五、类级别的@RequestMapping会被Spring MVC加载

@RequestMapping("/users")
@FeignClient(name = "microservice-user")
public class TestFeignClient {
    // ...
}
1
2
3
4
5

类上的@RequestMapping 注解也会被Spring MVC加载。该问题现已经被解决,早期的版本有两种解决方案:

方案1:不在类上加@RequestMapping 注解;

方案2:添加如下代码:

@Configuration
@ConditionalOnClass({ Feign.class })
public class FeignMappingDefaultConfiguration {
    @Bean
    public WebMvcRegistrations feignWebRegistrations() {
        return new WebMvcRegistrationsAdapter() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return new FeignFilterRequestMappingHandlerMapping();
            }
        };
    }

    private static class FeignFilterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
        @Override
        protected boolean isHandler(Class<?> beanType) {
            return super.isHandler(beanType) && !beanType.isInterface();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

相关Issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/466

# 六、首次请求失败

详见:Spring Cloud中,如何解决Feign/Ribbon第一次请求失败的问题? (opens new window)

# 七、如需产生Hystrix Stream监控信息,需要做一些额外操作

Feign本身已经整合了Hystrix,可直接使用@FeignClient(value = "microservice-provider-user", fallback = XXX.class) 来指定fallback类,fallback类继承@FeignClient所标注的接口即可。

但是假设如需使用Hystrix Stream进行监控,默认情况下,访问http://IP:PORT/actuator/hystrix.stream 是会返回404,这是因为Feign虽然整合了Hystrix,但并没有整合Hystrix的监控。如何添加监控支持呢?需要以下几步:

第一步:添加依赖,示例:

<!-- 整合hystrix,其实feign中自带了hystrix,引入该依赖主要是为了使用其中的hystrix-metrics-event-stream,用于dashboard -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
1
2
3
4
5

第二步:在启动类上添加@EnableCircuitBreaker 注解,示例:

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableCircuitBreaker
public class MovieFeignHystrixApplication {
  public static void main(String[] args) {
    SpringApplication.run(MovieFeignHystrixApplication.class, args);
  }
}
1
2
3
4
5
6
7
8
9

第三步:在application.yml中添加如下内容,暴露hystrix.stream端点:

management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream'
1
2
3
4
5

这样,访问任意Feign Client接口的API后,再访问http://IP:PORT/actuator/hystrix.stream ,就会展示一大堆Hystrix监控数据了。

# 本文首发

http://www.itmuch.com/spring-cloud-sum/feign-problems/

Last Updated: 2023/01/30, 11:01:00
Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】
SentinelResource注解 属性总结

← Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】 SentinelResource注解 属性总结→

Theme by Vdoing | Copyright © 2019-2023 pursue-wind | 粤ICP备2022093130号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
  • 飙升榜
  • 新歌榜
  • 云音乐民谣榜
  • 美国Billboard榜
  • UK排行榜周榜
  • 网络DJ