SpringCloud集成OpenFeign

一、简介

OpenFeign客户端是一个web声明式http远程调用工具,直接可以根据服务名称去注册中心拿到指定的服务IP集合,提供了接口和注解方式进行调用,内嵌集成了Ribbon本地负载均衡器。

二、SpringCloud集成OpenFeign

java 复制代码
版本说明:
Spring Cloud Version:Hoxton.SR12
Spring Boot Version:2.3.12.RELEASE

不同版本源码可能会有差异,具体参考版本对照

1、引入依赖

java 复制代码
<!--openfeign-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、在启动类或者配置类上加@EnableFeignClients注解

java 复制代码
@SpringBootApplication
@EnableFeignClients
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3、声明Feign接口

java 复制代码
/**
 * 用户 远程服务
 */
      
//1.1、如果是注册到nacos服务,则填写nacos服务名称,contextId:唯一标识
@FeignClient(value = "user",contextId = "userInfoClient01")
public interface UserFeignApi{
    /** 获取用户信息 */
    @GetMapping("/user/info")
    User getInfo(Long id);
}

//2、如果调用第三方接口,直接通过url调用 (value服务名,url远程调用地址,path统一前缀)
@FeignClient(value = "自定义服务名",url = "http://127.0.0.1:8080",path = "/user")
public interface UserFeignApi{
    /** 获取用户信息 */
    @GetMapping("/info")
    User getInfo(Long id);
}

相同服务名的坑:在使用了Feign的SpringCloud项目启动时,当存在多个FeignClient接口且name值相同 ,就会报错: A bean with that name has already been defined and overriding is disabled。这是因为SpringBoot会为每个FeginClient创建Bean,但当name值相同时SpringBoot会认为是有两个相同名字的Bean需要被创建,由此抛出异常。给每个@FeignClient都设置自己唯一的contextId

4、测试feign接口

java 复制代码
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private UserFeignApi userFeignApi;
 
    @GetMapping("/getInfo")
    public User getInfo(Long id) {
        return userFeignApi.getInfo(id);
    }

5、日志配置(OpenFeign)

java 复制代码
feign:  
  client:
    config: 
      default: 
        loggerLevel: FULL  

# 这里用default就是全局配置,如果是写服务名称user或者唯一标识contextId,则是针对某个微服务的配置
# 日志级别 NONE:默认不打印  BASIC:打印简单信息 HEADERS:打印头信息 FULL:打印全部信息(默认 NONE)

6、OpenFeign超时配置

使用配置文件配置是最推荐的,也是在项目中使用最多的。

java 复制代码
feign:
  client:
    config:
      default:   # 默认全局配置,也可配置单独FeignClient,default替换对应contextId
        connectTimeout: 2000 # 连接超时时间 默认值:10000毫秒
        readTimeout: 5000 # 读取超时时间 默认值:60000毫秒

7、OpenFeign拦截器

拦截器是OpenFeign可用的一种强大的工具,它可以被用来在请求和响应前后进行一些额外的处理

1、通过OpenFeign请求拦截器设置统一请求头

java 复制代码
public class MyHeaderInterceptor implements RequestInterceptor {
    private static String headerName = "token";
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 在这里添加额外的处理逻辑,添加请求头
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes instanceof ServletRequestAttributes) {
            ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = attributes.getRequest();
            String value = request.getHeader(headerName);
            requestTemplate.header(headerName, value);
        }
    }
}

在配置文件中添加拦截器配置

java 复制代码
feign:
  client:
    config:
      # 默认配置 如果不单独配置每个服务会走默认配置
      default:
        request-interceptors:
          - com.config.MyHeaderInterceptor 
相关推荐
风象南10 分钟前
SpringBoot的5种日志输出规范策略
java·spring boot·后端
cccc来财16 分钟前
Go中的协程并发和并发panic处理
开发语言·后端·golang
邪恶的贝利亚34 分钟前
从webrtc到janus简介
后端·asp.net·webrtc
Livingbody35 分钟前
Whisper 使用简单实例教程【1】
后端
花月C2 小时前
Mysql-定时删除数据库中的验证码
数据库·后端·mysql·spring
XMYX-07 小时前
Spring Boot + Prometheus 实现应用监控(基于 Actuator 和 Micrometer)
spring boot·后端·prometheus
记得开心一点嘛9 小时前
使用MinIO搭建自己的分布式文件存储
分布式·spring cloud·minio
@yanyu6669 小时前
springboot实现查询学生
java·spring boot·后端
酷爱码10 小时前
Spring Boot项目中JSON解析库的深度解析与应用实践
spring boot·后端·json
AI小智10 小时前
Google刀刃向内,开源“深度研究Agent”:Gemini 2.5 + LangGraph 打造搜索终结者!
后端